|
9 | 9 |
|
10 | 10 | Simple server SDK to convert a json-rpc request string into json-rpc response string
|
11 | 11 |
|
| 12 | +## How to use |
| 13 | + |
| 14 | +Sdk requires only two things : |
| 15 | + - A method resolver : must implement [MethodResolverInterface](./src/Domain/Model/MethodResolverInterface.php), resolving logic's is your own. |
| 16 | + - Methods : JsonRpc methods that implement [JsonRpcMethodInterface](./src/Domain/Model/JsonRpcMethodInterface.php) |
| 17 | + |
| 18 | +:warning: No dependency injection is managed in this library |
| 19 | + |
| 20 | +### Example |
| 21 | +#### JSON-RPC Method |
| 22 | +```php |
| 23 | +use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; |
| 24 | + |
| 25 | +class DummyMethod implements JsonRpcMethodInterface |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @param array $paramList |
| 29 | + * |
| 30 | + * @throws \Exception |
| 31 | + */ |
| 32 | + public function validateParams(array $paramList) |
| 33 | + { |
| 34 | + //If case your app require a specific param for instance |
| 35 | + if (!isset($paramList['my-required-key')) { |
| 36 | + throw new \Exception('"my-required-key" is a required key'); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @param array|null $paramList |
| 42 | + * |
| 43 | + * @return array|int|null |
| 44 | + */ |
| 45 | + public function apply(array $paramList = null) |
| 46 | + { |
| 47 | + // Handle the request |
| 48 | + ... |
| 49 | + // Then return a result |
| 50 | + return [ |
| 51 | + 'status' => 'done', |
| 52 | + ]; |
| 53 | + // Or |
| 54 | + return null; |
| 55 | + // Or |
| 56 | + return 12345; |
| 57 | + } |
| 58 | +} |
| 59 | +``` |
| 60 | +#### Array method resolver (simple example) |
| 61 | +*You could take example on [the one used for behat tests](./features/bootstrap/App/BehatMethodResolver.php)* |
| 62 | +```php |
| 63 | +use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException; |
| 64 | +use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; |
| 65 | +use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface; |
| 66 | + |
| 67 | +class ArrayMethodResolver implements MethodResolverInterface |
| 68 | +{ |
| 69 | + /** @var JsonRpcMethodInterface[] */ |
| 70 | + private $methodList = []; |
| 71 | + |
| 72 | + /** |
| 73 | + * @param string $methodName |
| 74 | + * |
| 75 | + * @return JsonRpcMethodInterface |
| 76 | + * |
| 77 | + * @throws JsonRpcMethodNotFoundException |
| 78 | + */ |
| 79 | + public function resolve(string $methodName) : JsonRpcMethodInterface |
| 80 | + { |
| 81 | + if (!isset($this->methodList[$methodName])) { |
| 82 | + throw new JsonRpcMethodNotFoundException($methodName); |
| 83 | + } |
| 84 | + |
| 85 | + return $this->methodList[$methodName]; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param JsonRpcMethodInterface $method |
| 90 | + * @param string $methodName |
| 91 | + */ |
| 92 | + public function addMethod(JsonRpcMethodInterface $method, string $methodName) |
| 93 | + { |
| 94 | + $this->methodList[$methodName] = $method; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +Then add your method to the resolver and create the endpoint : |
| 100 | +```php |
| 101 | +use Yoanm\JsonRpcServer\App\Creator\CustomExceptionCreator; |
| 102 | +use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; |
| 103 | +use Yoanm\JsonRpcServer\App\Manager\MethodManager; |
| 104 | +use Yoanm\JsonRpcServer\App\RequestHandler; |
| 105 | +use Yoanm\JsonRpcServer\App\Serialization\RequestDenormalizer; |
| 106 | +use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer; |
| 107 | +use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; |
| 108 | +use Yoanm\JsonRpcServer\Infra\Serialization\RawRequestSerializer; |
| 109 | +use Yoanm\JsonRpcServer\Infra\Serialization\RawResponseSerializer; |
| 110 | + |
| 111 | +$resolver = new ArrayMethodResolver(); |
| 112 | +$resolver->addMethod( |
| 113 | + 'dummy-method' |
| 114 | + new DummyMethod() |
| 115 | +); |
| 116 | + |
| 117 | +$responseCreator = new ResponseCreator(); |
| 118 | + |
| 119 | +$endpoint = new JsonRpcEndpoint( |
| 120 | + new RawRequestSerializer( |
| 121 | + new RequestDenormalizer() |
| 122 | + ), |
| 123 | + new RequestHandler( |
| 124 | + new MethodManager( |
| 125 | + $resolver, |
| 126 | + new CustomExceptionCreator() |
| 127 | + ), |
| 128 | + $responseCreator |
| 129 | + ), |
| 130 | + new RawResponseSerializer( |
| 131 | + new ResponseNormalizer() |
| 132 | + ), |
| 133 | + $responseCreator |
| 134 | +); |
| 135 | +``` |
| 136 | + |
| 137 | +Once endpoint is ready, you can send it request string : |
| 138 | +```php |
| 139 | +use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; |
| 140 | + |
| 141 | +$requestString = <<<JSONRPC |
| 142 | +{ |
| 143 | + "jsonrpc": "2.0", |
| 144 | + "id": 1 |
| 145 | + "method": "dummy-method", |
| 146 | + "params": { |
| 147 | + "my-required-key": "a-value" |
| 148 | + } |
| 149 | +} |
| 150 | +JSONRPC; |
| 151 | + |
| 152 | +$responseString = $endpoint->index($requestString); |
| 153 | +``` |
| 154 | + |
| 155 | +`$responseString` will be the following string depending of method returned value : |
| 156 | + * ```json |
| 157 | + {"jsonrpc":"2.0","id":1,"result":{"status":"done"}} |
| 158 | + ``` |
| 159 | + * ```json |
| 160 | + {"jsonrpc":"2.0","id":1,"result":null} |
| 161 | + ``` |
| 162 | + |
| 163 | + * ```json |
| 164 | + {"jsonrpc":"2.0","id":1,"result":12345} |
| 165 | + ``` |
| 166 | + |
12 | 167 | ## Contributing
|
13 | 168 | See [contributing note](./CONTRIBUTING.md)
|
0 commit comments