scaytrase / json-rpc-client
JSON-RPC 2.0 客户端实现
1.0.2
2017-04-27 13:53 UTC
Requires
- php: ~5.5|~7.0
- guzzlehttp/guzzle: ~6.0
- paragonie/random_compat: ^1|^2
- psr/log: ~1.0
- scaytrase/rpc-common: ~1.0
Requires (Dev)
- phpunit/phpunit: ^4.8.35|^5.4|^6.0
- psr/cache: ~1.0
Suggests
- psr/cache: For CacheableRpcClient decorator
README
JSON-RPC 2.0 客户端实现
是 scaytrase/rpc-common
的扩展
- JSON RPC 接口
- JSON RPC 客户端
- 使用 Guzzle 异步
- 使用多个请求或
LazyClientDecorator
自动批量处理
用法
- 配置一个 Guzzle 客户端。
- 实例化客户端
use ScayTrase\Api\JsonRpc\JsonRpcClient; $client = new JsonRpcClient($guzzleClient, 'http://endpoint/url/');
- 可选地,将 ID 生成器作为第三个参数传递,将 PSR-3 日志记录器作为第四个参数传递
默认使用简单的 UUID 生成器和 PSR-3 NullLogger
。为 RpcRequestInterface
实例生成 ID。如果请求是 JsonRpcRequestInterface
的实例且未分配 ID,则请求被视为通知请求,将不会从服务器收到响应。
- 创建一个
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;
- 调用客户端
/** @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