@@ -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
0 commit comments