scaytrase/json-rpc-client

JSON-RPC 2.0 客户端实现

1.0.2 2017-04-27 13:53 UTC

This package is auto-updated.

Last update: 2024-08-26 04:51:18 UTC


README

Build Status Code Coverage Scrutinizer Code Quality SensioLabsInsight

Latest Stable Version Total Downloads Latest Unstable Version

JSON-RPC 2.0 客户端实现

scaytrase/rpc-common 的扩展

  • JSON RPC 接口
  • JSON RPC 客户端
  • 使用 Guzzle 异步
  • 使用多个请求或 LazyClientDecorator 自动批量处理

JSON-RPC 2.0 规范

用法

  1. 配置一个 Guzzle 客户端
  2. 实例化客户端
use ScayTrase\Api\JsonRpc\JsonRpcClient;

$client = new JsonRpcClient($guzzleClient, 'http://endpoint/url/');
  1. 可选地,将 ID 生成器作为第三个参数传递,将 PSR-3 日志记录器作为第四个参数传递

默认使用简单的 UUID 生成器和 PSR-3 NullLogger。为 RpcRequestInterface 实例生成 ID。如果请求是 JsonRpcRequestInterface 的实例且未分配 ID,则请求被视为通知请求,将不会从服务器收到响应。

  1. 创建一个 RpcRequestInterface 实例

使用 JsonRpcRequest

$request = new \ScayTrase\Api\JsonRpc\JsonRpcRequest('my/method', ['param1' => 'val1'], 'request_id');
$notification = new \ScayTrase\Api\JsonRpc\JsonRpcRequest('my/method', ['param1' => 'val1']);

使用 JsonRpcNotification

$notification = new \ScayTrase\Api\JsonRpc\JsonRpcNotification('my/method', ['param1' => 'val1']);

使用自定义 RpcRequestInterface 实现

final class MyRpcRequest implements \ScayTrase\Api\Rpc\RpcRequestInterface 
{
    public function getMethod() 
    {
        return 'my/method';
    }
    
    public function getParameters() 
    {
        return ['param1' => 'val1'];      
    }
}

$request = new MyRpcRequest;
  1. 调用客户端
/** @var \ScayTrase\Api\Rpc\RpcClientInterface $client */
/** @var \ScayTrase\Api\Rpc\RpcRequestInterface $request */
 
$collection = $client->invoke($request);
$collection = $client->invoke([$request]);

集合对象包含请求和响应之间的映射

/** @var \ScayTrase\Api\Rpc\RpcRequestInterface $request */
/** @var \ScayTrase\Api\Rpc\ResponseCollectionInterface $collection */

$response = $collection->getResponse($request);

装饰

参考 scaytrase/rpc-common 基础库以获取一些有用的装饰器

  • CacheableRpcClient
  • LazyRpcClient
  • LoggableRpcClient
  • ProfiledClient
  • TraceableClient