Skip to content

Commit

Permalink
6.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
smortensen committed Jul 26, 2019
1 parent 85eceae commit 77edbb6
Show file tree
Hide file tree
Showing 18 changed files with 330 additions and 321 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [6.0.0] - 2019-07-25
### Changed
- There are now two types of responses--an "ErrorResponse" and a "ResultResponse"--
which both derive from a base "Response" class. The "Client::decode" method
returns a list of these "Response" objects. You can use the "instanceof"
type operator to see which type of response you have received.

## [5.0.0] - 2018-05-16
### Added
- The "Client::decode" method now throws an "ErrorException" when the input is not a valid JSON-RPC 2.0 reply string
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ $client = new Client();

$client->query(1, 'add', array(1, 2));

$message = $client->encode(); // {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}
$message = $client->encode();

// message: {"jsonrpc":"2.0","method":"add","params":[1,2],"id":1}
```

### Server
Expand All @@ -47,15 +49,17 @@ $api = new Api();

$server = new Server($api);

$reply = $server->reply($message); // {"jsonrpc":"2.0","id":1,"result":3}
$reply = $server->reply($message);

// reply: {"jsonrpc":"2.0","result":3,"id":1}
```

*See the [examples](https://github.com/datto/php-json-rpc/tree/master/examples) folder for full working examples.*


## Requirements

* PHP >= 5.3
* PHP >= 7.0


## License
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=7.0.0"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "^6.5"
},
"autoload": {
"psr-4": {
Expand Down
110 changes: 57 additions & 53 deletions examples/client.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,88 @@
<?php

use Datto\JsonRpc\Client;
use Datto\JsonRpc\Responses\ErrorResponse;
use Datto\JsonRpc\Responses\ResultResponse;

require_once dirname(__DIR__) . '/vendor/autoload.php';



// Example 1. Single query
// Example 1. Send a single query:
$client = new Client();

$client->query(1, 'add', array(1, 2));

$message = $client->encode();

echo "Example 1. Single query:\n{$message}\n\n";
// {"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}

echo "Example 1. Send a single query:\n",
" \$client = new Client();\n",
" \$client->query(1, 'add', array(1, 2));\n",
" \$message = \$client->encode();\n",
" // message: {$message}\n\n";
// message: {"jsonrpc":"2.0","method":"add","params":[1,2],"id":1}


// Example 2. Batch queries
// Example 2. Send multiple queries together in a batch:
$client = new Client();

$client->query(1, 'add', array(1, 2));
$client->query(2, 'add', array('a', 'b'));

$message = $client->encode();

echo "Example 2. Batch queries:\n{$message}\n\n";
// [{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]},{"jsonrpc":"2.0","id":2,"method":"add","params":["a","b"]}]

echo "Example 2. Send multiple queries together in a batch:\n",
" \$client = new Client();\n",
" \$client->query(1, 'add', array(1, 2));\n",
" \$client->query(2, 'add', array('a', 'b'));\n",
" \$message = \$client->encode();\n",
" // message: {$message}\n\n";
// message: [{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]},{"jsonrpc":"2.0","id":2,"method":"add","params":["a","b"]}]


// Example 3. Valid server response
// Example 3. Send a notification (where no response is required):
$client = new Client();
$client->notify('add', array(1, 2));
$message = $client->encode();

$reply = '[{"jsonrpc":"2.0","id":1,"result":3},{"jsonrpc":"2.0","id":2,"error":{"code":-32602,"message":"Invalid params"}}]';

$responses = $client->decode($reply);

echo "Example 3. Valid server response:\n";
foreach ($responses as $response) {
$id = $response->getId();
$isError = $response->isError();

if ($isError) {
$error = $response->getError();

$errorProperties = array(
'code' => $error->getCode(),
'message' => $error->getMessage(),
'data' => $error->getData()
);

echo " * id: {$id}, error: ", json_encode($errorProperties), "\n";
} else {
$result = $response->getResult();
echo "Example 3. Send a notification (where no response is required):\n",
" \$client = new Client();\n",
" \$client->notify('add', array(1, 2));\n",
" \$message = \$client->encode();\n",
" // message: {$message}\n\n";
// message: {"jsonrpc":"2.0","method":"add","params":[1,2]}

echo " * id: {$id}, result: ", json_encode($result), "\n";
}
}
echo "\n";
// * id: 1, result: 3
// * id: 2, error: {"code":-32602,"message":"Invalid params","data":null}

// Example 4. Receive a single response:
$client = new Client();
$reply = '{"jsonrpc":"2.0","id":1,"result":3}';
$responses = $client->decode($reply);

echo "Example 4. Receive a single response:\n",
" \$client = new Client();\n",
" \$reply = '{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":3}';\n",
" \$responses = \$client->decode(\$reply);\n",
" // responses: [new ResultResponse(1, 3)]\n\n";

// Example 4. Invalid server response
// Example 5. Receive a batch of responses:
$client = new Client();
$reply = '[{"jsonrpc":"2.0","id":1,"result":3},{"jsonrpc":"2.0","id":2,"error":{"code":-32602,"message":"Invalid params"}}]';
$responses = $client->decode($reply);

$reply = '{"jsonrpc":"2.0","id":';

try {
$responses = $client->decode($reply);
} catch (ErrorException $exception) {
echo "Example 3. Invalid server response:\n";

$exceptionProperties = array(
'code' => $exception->getCode(),
'message' => $exception->getMessage()
);
echo "Example 5. Receive a batch of responses:\n",
" \$client = new Client();\n",
" \$reply = '[{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":3},{\"jsonrpc\":\"2.0\",\"id\":2,\"error\":{\"code\":-32602,\"message\":\"Invalid params\"}}]';\n",
" \$responses = \$client->decode(\$reply);\n",
" // responses: [new ResultResponse(1, 3), new ErrorResponse(2, 'Invalid params', -32602)]\n";

echo "ErrorException: ", json_encode($exceptionProperties), "\n";
foreach ($responses as $response) {
if ($response instanceof ResultResponse) {
$result = [
'id' => $response->getId(),
'value' => $response->getValue()
];
} elseif ($response instanceof ErrorResponse) {
$error = [
'id' => $response->getId(),
'message' => $response->getMessage(),
'code' => $response->getCode(),
'data' => $response->getData()
];
}
}
9 changes: 6 additions & 3 deletions examples/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

require_once __DIR__ . '/../vendor/autoload.php';


// Example 1. Send a reply:
$server = new Server(new Api());

$reply = $server->reply('{"jsonrpc":"2.0","id":1,"method":"add","params":[1,2]}');

echo $reply, "\n"; // {"jsonrpc":"2.0","id":1,"result":3}
echo "Example 1. Send a reply:\n",
" \$server = new Server(new Api());\n",
" \$reply = \$server->reply('{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"add\",\"params\":[1,2]}');\n",
" // reply: {$reply}\n";
// reply: {"jsonrpc":"2.0","id":1,"result":3}
Loading

0 comments on commit 77edbb6

Please sign in to comment.