Simple server SDK to convert a json-rpc request string into json-rpc response string
Sdk requires only two things :
- A method resolver : must implement MethodResolverInterface, resolving logic's is your own.
- Methods : JsonRpc methods that implement JsonRpcMethodInterface
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; class DummyMethod implements JsonRpcMethodInterface { /** * {@inheritdoc} */ public function validateParams(array $paramList) : array { $violationList = []; //If case your app require a specific param for instance if (!isset($paramList['my-required-key')) { $violationList[] = [ 'path' => 'my-required-key', 'error' => 'Key is required' ] } return $violationList; } /** * {@inheritdoc} */ public function apply(array $paramList = null) { // Handle the request ... // Then return a result return [ 'status' => 'done', ]; // Or return null; // Or return 12345; } }
You could take example on the one used for behat tests
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcMethodInterface; use Yoanm\JsonRpcServer\Domain\Model\MethodResolverInterface; class ArrayMethodResolver implements MethodResolverInterface { /** @var JsonRpcMethodInterface[] */ private $methodList = []; /** * @param string $methodName * * @return JsonRpcMethodInterface|null */ public function resolve(string $methodName) { return $this->methodList[$methodName]; } /** * @param JsonRpcMethodInterface $method * @param string $methodName */ public function addMethod(JsonRpcMethodInterface $method, string $methodName) { $this->methodList[$methodName] = $method; } }
Then add your method to the resolver and create the endpoint :
use Yoanm\JsonRpcServer\App\Creator\CustomExceptionCreator; use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; use Yoanm\JsonRpcServer\App\Manager\MethodManager; use Yoanm\JsonRpcServer\App\RequestHandler; use Yoanm\JsonRpcServer\App\Serialization\RequestDenormalizer; use Yoanm\JsonRpcServer\App\Serialization\ResponseNormalizer; use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; use Yoanm\JsonRpcServer\Infra\Serialization\RawRequestSerializer; use Yoanm\JsonRpcServer\Infra\Serialization\RawResponseSerializer; $resolver = new ArrayMethodResolver(); $resolver->addMethod( 'dummy-method' new DummyMethod() ); $responseCreator = new ResponseCreator(); $endpoint = new JsonRpcEndpoint( new RawRequestSerializer( new RequestDenormalizer() ), new RequestHandler( new MethodManager( $resolver, new CustomExceptionCreator() ), $responseCreator ), new RawResponseSerializer( new ResponseNormalizer() ), $responseCreator );
Once endpoint is ready, you can send it request string :
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint; $requestString = <<<JSONRPC { "jsonrpc": "2.0", "id": 1 "method": "dummy-method", "params": { "my-required-key": "a-value" } } JSONRPC; $responseString = $endpoint->index($requestString);
$responseString
will be the following string depending of method returned value :
-
{"jsonrpc":"2.0","id":1,"result":{"status":"done"}}
-
{"jsonrpc":"2.0","id":1,"result":null}
-
{"jsonrpc":"2.0","id":1,"result":12345}