-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85eceae
commit 77edbb6
Showing
18 changed files
with
330 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.