2017 © Pedro Peláez
 

library jsonrpc-server-sdk

Server SDK to convert a json-rpc request string into json-rpc response string

image

yoanm/jsonrpc-server-sdk

Server SDK to convert a json-rpc request string into json-rpc response string

  • Sunday, May 20, 2018
  • by yoanm
  • Repository
  • 0 Watchers
  • 0 Stars
  • 286 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 45 Versions
  • 1 % Grown

The README.md

PHP JSON-RPC server sdk

License Code size Dependabot Status, (*1)

Scrutinizer Build Status Scrutinizer Code Quality Codacy Badge, (*2)

CI codecov, (*3)

Latest Stable Version Packagist PHP version, (*4)

Simple server SDK to convert a json-rpc request string into json-rpc response string., (*5)

See yoanm/symfony-jsonrpc-http-server for automatic dependency injection., (*6)

See yoanm/jsonrpc-params-symfony-validator-sdk for params validation., (*7)

See yoanm/jsonrpc-server-doc-sdk for documentation generation., (*8)

How to use

Sdk requires only two things :, (*9)

Sdk optionally provide :, (*10)

  • Events dispatch
  • Params validation

Simple Example

JSON-RPC Method

use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;

class DummyMethod implements JsonRpcMethodInterface
{
    /**
     * {@inheritdoc}
     */
    public function apply(array $paramList = null)
    {
        // Handle the request
        ...
        // Then return a result
        return [
            'status' => 'done',
        ];
        // Or
        return null;
        // Or
        return 12345;
    }
}

Array method resolver (simple example)

You can use the one used for behat tests or this Psr11 method resolver as example, (*11)

use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface;

class ArrayMethodResolver implements JsonRpcMethodResolverInterface
{
    /** @var JsonRpcMethodInterface[] */
    private $methodList = [];

    /**
     * {@inheritdoc}
     */
    public function resolve(string $methodName) : ?JsonRpcMethodInterface
    {
        return array_key_exists($methodName, $this->methodList)
            ? $this->methodList[$methodName]
            : null
        ;
    }

    /**
     * @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 :, (*12)

use Yoanm\JsonRpcServer\App\Creator\ResponseCreator;
use Yoanm\JsonRpcServer\App\Handler\ExceptionHandler;
use Yoanm\JsonRpcServer\App\Handler\JsonRpcRequestHandler;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcCallDenormalizer;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcCallResponseNormalizer;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcCallSerializer;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcRequestDenormalizer;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcResponseErrorNormalizer;
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcResponseNormalizer;
use Yoanm\JsonRpcServer\Infra\Endpoint\JsonRpcEndpoint;

$resolver = new ArrayMethodResolver();
$resolver->addMethod('dummy-method', new DummyMethod());

$jsonRpcSerializer = new JsonRpcCallSerializer(
    new JsonRpcCallDenormalizer(
        new JsonRpcRequestDenormalizer()
    ),
    new JsonRpcCallResponseNormalizer(
        new JsonRpcResponseNormalizer() 
        // Or `new JsonRpcResponseNormalizer(new JsonRpcResponseErrorNormalizer())` for debug purpose
        // To also dump arguments, be sure 'zend.exception_ignore_args' ini option is not at true/1
    )
);
$responseCreator = new ResponseCreator();
$requestHandler = new JsonRpcRequestHandler($resolver, $responseCreator);
$exceptionHandler = new ExceptionHandler($responseCreator);

$endpoint = new JsonRpcEndpoint($jsonRpcSerializer, $requestHandler, $exceptionHandler);

Once endpoint is ready, you can send it request string :, (*13)

$requestString = <<<JSONRPC
{
    "jsonrpc": "2.0",
    "id": 1
    "method": "dummy-method"
}
JSONRPC;

$responseString = $endpoint->index($requestString);

$responseString will be the following string depending of method returned value :, (*14)

  • {"jsonrpc":"2.0","id":1,"result":{"status":"done"}}
    
  • {"jsonrpc":"2.0","id":1,"result":null}
    
  • {"jsonrpc":"2.0","id":1,"result":12345}
    

Events dispatch example

Simple event dispatcher

You can use the one used for behat tests as example, (*15)

use Yoanm\JsonRpcServer\Domain\Event\JsonRpcServerEvent;
use Yoanm\JsonRpcServer\Domain\JsonRpcServerDispatcherInterface;

/**
 * Class SimpleDispatcher
 */
class SimpleDispatcher implements JsonRpcServerDispatcherInterface
{
    /** @var callable[] */
    private $listenerList = [];

    /**
     * {@inheritdoc}
     */
    public function dispatchJsonRpcEvent(string $eventName, JsonRpcServerEvent $event = null) : void
    {
        if (!array_key_exists($eventName, $this->listenerList)) {
            return;
        }

        foreach ($this->listenerList[$eventName] as $listener) {
            $listener($event, $eventName);
        }
    }

    /**
     * {@inheritdoc}
     */
    public function addJsonRpcListener(string $eventName, $listener) : void
    {
        $this->listenerList[$eventName][] = $listener;
    }
}

Then bind your listeners to your dispatcher:, (*16)

use Yoanm\JsonRpcServer\Domain\Event\Acknowledge\OnRequestReceivedEvent;
use Yoanm\JsonRpcServer\Domain\Event\Acknowledge\OnResponseSendingEvent;
use Yoanm\JsonRpcServer\Domain\Event\Action\OnMethodSuccessEvent;

$dispatcher = new SimpleDispatcher();

$listener = function ($event, $eventName) {
    echo sprintf(
        'Received %s with event class "%s"',
        $eventName,
        get_class($event)
    );
};

$dispatcher->addJsonRpcListener(OnRequestReceivedEvent::EVENT_NAME, $listener);
$dispatcher->addJsonRpcListener(OnResponseSendingEvent::EVENT_NAME, $listener);
$dispatcher->addJsonRpcListener(OnMethodSuccessEvent::EVENT_NAME, $listener);

And bind dispatcher like following :, (*17)

$endpoint->setJsonRpcServerDispatcher($dispatcher);
$requestHandler->setJsonRpcServerDispatcher($dispatcher);
$exceptionHandler->setJsonRpcServerDispatcher($dispatcher);

Events dispatched

Basic request lifecycle
  • json_rpc_server_skd.on_request_received / Acknowledge\OnRequestReceivedEvent, (*18)

    Dispatched when a request has been passed to the endpoint and successfully deserialized., (*19)

    N.B. : Lonely cases where this event is not dispatched are when the request string is not a valid JSON-RPC request., (*20)

    It include :, (*21)

    • Parse error exception (malformed json string)
    • For simple request only, in case of Invalid request (not an object / missing required properties / ...)., (*22)

      :warning: For batch request containing Invalid SubRequest, this event will still be dispatched, (*23)

  • Either, (*24)

    • json_rpc_server_skd.on_method_success / Action\OnMethodSuccessEvent, (*25)

      Dispatched only in case JSON-RPC method has been successfully executed., (*26)

    • json_rpc_server_skd.on_method_failure / Action\OnMethodFailureEvent, (*27)

      Dispatched only in case JSON-RPC method throw an exception during execution., (*28)

  • json_rpc_server_skd.on_response_sending / Acknowledge\OnResponseSendingEvent, (*29)

    Dispatched when a response has been successfully serialized by the endpoint and will be returned., (*30)

Additional events
Batch request
Exception

json_rpc_server_skd.on_exception / Action\OnExceptionEvent, (*35)

Dispatched when an exception occurred during sdk execution, (*36)

Action vs Acknowledge events
Acknowledge

They have only an acknowledge purpose., (*37)

They are grouped under Yoanm\JsonRpcServer\Domain\Event\Acknowledge namespace., (*38)

Action

They allow you to override stuffs., (*39)

They are grouped under Yoanm\JsonRpcServer\Domain\Event\Action namespace., (*40)

Here, the list :, (*41)

Params validation example

You can use this JSON-RPC params symfony validator as example, (*42)

To validate params for a given method, do the following :, (*43)

use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodParamsValidatorInterface;
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;

$validator = new class implements JsonRpcMethodParamsValidatorInterface
{
    public function validate(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) : array
    {
        if (!(/** Skip unexpected method */)) {
            return [];
        }

        // Create your violations based on what you want
        $paramList = $jsonRpcRequest->getParamList();
        $violation = "???";

        return [$violation];
    }
};

$requestHandler->setMethodParamsValidator($validator);

Contributing

See contributing note, (*44)

The Versions

20/05 2018

dev-release/3.0.0

dev-release/3.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-polish

dev-release/3.0.0-polish

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-doc

dev-release/3.0.0-doc

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-improve2

dev-release/3.0.0-improve2

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-improve

dev-release/3.0.0-improve

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-removeUseless

dev-release/3.0.0-removeUseless

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcEndpoint

dev-release/3.0.0-JsonRpcEndpoint

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcCallSerializer

dev-release/3.0.0-JsonRpcCallSerializer

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcCallResponseNormalizer

dev-release/3.0.0-JsonRpcCallResponseNormalizer

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcCallDenormalizer

dev-release/3.0.0-JsonRpcCallDenormalizer

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcResponseNormalizer

dev-release/3.0.0-JsonRpcResponseNormalizer

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcRequestNormalizer

dev-release/3.0.0-JsonRpcRequestNormalizer

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-Handler2

dev-release/3.0.0-Handler2

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-Handler

dev-release/3.0.0-Handler

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-AckEvents

dev-release/3.0.0-AckEvents

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-ActionEvents

dev-release/3.0.0-ActionEvents

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-Dispatcher

dev-release/3.0.0-Dispatcher

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-Exception

dev-release/3.0.0-Exception

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcResponse

dev-release/3.0.0-JsonRpcResponse

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcRequest

dev-release/3.0.0-JsonRpcRequest

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcCallResponse

dev-release/3.0.0-JsonRpcCallResponse

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-JsonRpcCall

dev-release/3.0.0-JsonRpcCall

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-dev-partial

dev-release/3.0.0-dev-partial

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-release/3.0.0-dev

dev-release/3.0.0-dev

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

19/05 2018

dev-feature/improve

dev-feature/improve

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

MIT

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-master

9999999-dev

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

v2.0.1

2.0.1.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-hotfix/2.0.1

dev-hotfix/2.0.1

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-dev-hotfix/2.0.1

dev-dev-hotfix/2.0.1

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

v2.0.0

2.0.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-release/2.0.0

dev-release/2.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-feature/params-validation

dev-feature/params-validation

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-feature/params-validation-dev

dev-feature/params-validation-dev

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-feature/event-manager-dev

dev-feature/event-manager-dev

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

21/04 2018

dev-feature/event-manager

dev-feature/event-manager

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

07/04 2018

v1.3.0

1.3.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

04/04 2018

dev-feature/array-method-resolver

dev-feature/array-method-resolver

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

04/04 2018

v1.2.0

1.2.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

03/04 2018

v1.1.0

1.1.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

03/04 2018

v1.0.1

1.0.1.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

03/04 2018

v1.0.0

1.0.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

03/04 2018

v0.1.1

0.1.1.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

  • php >7.0

 

The Development Requires

by Avatar yoanm

02/04 2018

v0.1.0

0.1.0.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

 

The Development Requires

by Avatar yoanm

01/04 2018

v0.0.1

0.0.1.0

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

 

The Development Requires

by Avatar yoanm

01/04 2018

v0.0.1-alpha

0.0.1.0-alpha

Server SDK to convert a json-rpc request string into json-rpc response string

  Sources   Download

GPL-3.0-only

The Requires

 

The Development Requires

by Avatar yoanm