Skip to content

Commit b091a1e

Browse files
authored
Merge pull request #16 from scaytrase/feature/readme
README
2 parents 00c875e + e8de041 commit b091a1e

File tree

5 files changed

+91
-8
lines changed

5 files changed

+91
-8
lines changed

JsonRpc/ResponseBodyValidator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ public function validate(\stdClass $response)
4444
if (!property_exists($response->{JsonRpcResponseInterface::ERROR_FIELD}, JsonRpcErrorInterface::ERROR_MESSAGE_FIELD)) {
4545
throw ResponseParseException::noErrorMessagePresent();
4646
}
47-
4847
}
4948
}
5049
}

JsonRpc/Tests/AbstractJsonRpcClientTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\HandlerStack;
99
use GuzzleHttp\Psr7\Response;
10+
use PHPUnit\Framework\TestCase;
1011
use ScayTrase\Api\JsonRpc\JsonRpcNotification;
1112
use ScayTrase\Api\JsonRpc\JsonRpcRequest;
1213
use ScayTrase\Api\Rpc\RpcErrorInterface;
1314

14-
abstract class AbstractJsonRpcClientTest extends \PHPUnit_Framework_TestCase
15+
abstract class AbstractJsonRpcClientTest extends TestCase
1516
{
1617
/** @var MockHandler */
1718
protected $queue;

JsonRpc/Tests/ResponseBodyValidatorTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace ScayTrase\Api\JsonRpc\Tests;
44

5+
use PHPUnit\Framework\TestCase;
6+
use ScayTrase\Api\JsonRpc\JsonRpcClient;
57
use ScayTrase\Api\JsonRpc\JsonRpcErrorInterface;
68
use ScayTrase\Api\JsonRpc\ResponseBodyValidator;
9+
use ScayTrase\Api\JsonRpc\SyncResponse;
710

8-
class ResponseBodyValidatorTest extends \PHPUnit_Framework_TestCase
11+
class ResponseBodyValidatorTest extends TestCase
912
{
1013
public function invalidResponseBodyProvider()
1114
{
@@ -85,7 +88,6 @@ public function testInvalidBody(\stdClass $body)
8588
*/
8689
public function testValidBody(\stdClass $body)
8790
{
88-
$parser = new ResponseBodyValidator();
89-
$parser->validate($body);
91+
self::assertEquals(JsonRpcClient::VERSION, (new SyncResponse($body))->getVersion());
9092
}
9193
}

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,85 @@ Extension of [`scaytrase/rpc-common`](https://github.com/scaytrase/rpc-common)
1818
* Automatic batch with multiple requests or `LazyClientDecorator`
1919

2020
[JSON-RPC 2.0 Specification](http://www.jsonrpc.org/specification)
21+
22+
## Usage
23+
24+
1. Configure a [Guzzle client](http://docs.guzzlephp.org/en/latest/).
25+
2. Configure a Guzzle URI instance
26+
3. Instantiate the client:
27+
28+
```php
29+
use ScayTrase\Api\JsonRpc\JsonRpcClient;
30+
31+
$client = new JsonRpcClient($guzzleClient, new Uri('http://endpoint/url/'));
32+
```
33+
34+
4. Optionally pass the ID generator as third argument and the PSR-3 logger as the fourth argument
35+
36+
Simple UUID generator and PSR-3 `NullLogger` are used by default. ID is generated for `RpcRequestInterface` instances.
37+
If request is instance of `JsonRpcRequestInterface` and does not contain an ID assigned, the request is traited as
38+
notification request and will not receive the response from server.
39+
40+
5. Create a `RpcRequestInterface` instance
41+
42+
With `JsonRpcRequest` class:
43+
44+
```php
45+
$request = new \ScayTrase\Api\JsonRpc\JsonRpcRequest('my/method', ['param1' => 'val1'], 'request_id');
46+
$notification = new \ScayTrase\Api\JsonRpc\JsonRpcRequest('my/method', ['param1' => 'val1']);
47+
```
48+
49+
With `JsonRpcNotification` class:
50+
```php
51+
$notification = new \ScayTrase\Api\JsonRpc\JsonRpcNotification('my/method', ['param1' => 'val1']);
52+
```
53+
54+
With custom `RpcRequestInterface` implementation:
55+
56+
```php
57+
final class MyRpcRequest implements \ScayTrase\Api\Rpc\RpcRequestInterface
58+
{
59+
public function getMethod()
60+
{
61+
return 'my/method';
62+
}
63+
64+
public function getParameters()
65+
{
66+
return ['param1' => 'val1'];
67+
}
68+
}
69+
70+
$request = new MyRpcRequest;
71+
```
72+
73+
6. Call the client
74+
75+
```php
76+
/** @var \ScayTrase\Api\Rpc\RpcClientInterface $client */
77+
/** @var \ScayTrase\Api\Rpc\RpcRequestInterface $request */
78+
79+
$collection = $client->invoke($request);
80+
$collection = $client->invoke([$request]);
81+
```
82+
83+
The collection object contains the mapping between the requests and the responses
84+
85+
```php
86+
/** @var \ScayTrase\Api\Rpc\RpcRequestInterface $request */
87+
/** @var \ScayTrase\Api\Rpc\ResponseCollectionInterface $collection */
88+
89+
$response = $collection->getResponse($request);
90+
```
91+
92+
## Decorating
93+
94+
Refer [`scaytrase/rpc-common`](https://github.com/scaytrase/rpc-common) base library for some
95+
useful decorators, i.e `CacheableRpcClient`, `LazyRpcClient`, `LoggableRpcClient`.
96+
97+
Also some profiling wrappers for Symfony usage were implemented:
98+
99+
* [Profiled client](https://github.com/bankiru/doctrine-api-bundle/blob/master/src/Bankiru/Api/Client/Profiling/ProfiledClient.php)
100+
with DataCollector and Profiler integration
101+
* [Traceable client](https://github.com/bankiru/doctrine-api-bundle/blob/master/src/Bankiru/Api/Client/Profiling/TraceableClient.php)
102+
with Stopwatch integration

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
"scaytrase/rpc-common": "~1.0",
1515
"psr/log": "~1.0",
1616
"guzzlehttp/guzzle": "~6.0",
17-
"paragonie/random_compat": "~1.1@stable"
17+
"paragonie/random_compat": "^1|^2"
1818
},
1919
"require-dev": {
20-
"phpunit/phpunit": "~4.5|~5.1",
21-
"phpunit/php-code-coverage": "~2.1|~3.0",
20+
"phpunit/phpunit": "^4.8.35|^5.4|^6.0",
2221
"psr/cache": "~1.0"
2322
},
2423
"autoload": {

0 commit comments

Comments
 (0)