php-json-rpc/src/Client.php

147 lines
3.8 KiB
PHP
Raw Normal View History

2019-01-04 03:59:05 +00:00
<?php
namespace JsonRpc;
use GuzzleHttp\Exception\ServerException;
use JsonRpc\Exception\RpcServerException;
2019-01-30 08:06:41 +00:00
use JsonRpc\Server\JsonRpcBase;
2019-01-11 07:05:38 +00:00
use Monolog\Handler\StreamHandler;
2019-01-10 06:17:13 +00:00
use Monolog\Logger;
2019-01-04 03:59:05 +00:00
2019-01-30 08:06:41 +00:00
class Client extends JsonRpc
2019-01-04 03:59:05 +00:00
{
2019-01-11 09:26:01 +00:00
/**
* all configuration information
* @var array
*/
2019-01-05 07:18:54 +00:00
protected $config;
2019-01-04 03:59:05 +00:00
2019-01-11 09:26:01 +00:00
/**
* request id
* @var string
*/
2019-01-05 07:18:54 +00:00
protected $id;
2019-01-10 06:17:13 +00:00
2019-01-07 08:10:26 +00:00
/**
* @var \GuzzleHttp\Client
*/
2019-01-04 03:59:05 +00:00
protected $http;
2019-01-11 10:17:52 +00:00
/**
* which server rpc call choose
* @var array
*/
2019-01-11 09:26:01 +00:00
protected $server_config;
2019-01-11 07:10:05 +00:00
public function __construct($config)
2019-01-04 03:59:05 +00:00
{
2019-01-19 08:53:19 +00:00
$this->config = $config;
2019-01-23 09:07:46 +00:00
$this->id = 1;
2019-01-05 07:18:54 +00:00
}
2019-01-11 09:26:01 +00:00
/**
*
* @param $k
* @return $this
*/
2019-01-05 07:18:54 +00:00
public function endpoint($k)
{
2019-01-11 10:17:52 +00:00
$this->server_config = $this->config['client'][$k];
2019-01-05 07:18:54 +00:00
$default = [
2019-01-04 03:59:05 +00:00
'timeout' => 3,
'allow_redirects' => false,
2019-01-05 07:18:54 +00:00
];
2019-01-11 09:26:01 +00:00
$this->http = new \GuzzleHttp\Client(array_merge($default, $this->server_config));
2019-01-05 08:00:27 +00:00
return $this;
2019-01-04 03:59:05 +00:00
}
/**
* @param $name
* @param $arguments
* @throws RpcServerException
* @return array
*/
2019-03-11 08:02:00 +00:00
public function call($name, $arguments, $options = [])
2019-01-04 03:59:05 +00:00
{
$payload = [
'jsonrpc' => '2.0',
'method' => $name,
'params' => $arguments,
'id' => $this->id(),
];
2019-03-11 08:02:00 +00:00
return $this->post($payload, $options);
2019-01-04 03:59:05 +00:00
}
/**
* @param $name
* @param $arguments
* @return array
* @throws RpcServerException
*/
public function __call($name, $arguments)
{
return $this->call($name, $arguments);
}
/**
* @param $payload
* @throws RpcServerException
* @return array
*/
2019-03-11 08:02:00 +00:00
protected function post($payload, $options = [])
2019-01-04 03:59:05 +00:00
{
try {
2019-01-11 09:26:01 +00:00
$headers = [
2019-01-21 06:32:47 +00:00
'X-Client-App' => $this->config['app'],
2019-03-15 06:02:20 +00:00
'X-Request-Id' => app('request')->header('X-Request-Id')
2019-01-11 09:26:01 +00:00
];
2019-01-30 04:56:40 +00:00
app('rpc.logger')->info("client_request", array_merge($this->server_config, $payload));
2019-03-11 08:02:00 +00:00
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', array_merge([
'headers' => $headers,
2019-01-07 08:10:26 +00:00
'json' => $payload,
2019-03-11 08:02:00 +00:00
], $options));
2019-01-04 03:59:05 +00:00
} catch (ServerException $e) {
2019-01-30 08:06:41 +00:00
$ex = new RpcServerException(self::ErrorMsg[JsonRpc::Rpc_Error_Internal_Error], JsonRpc::Rpc_Error_Internal_Error);
if (env("APP_DEBUG") == true) {
$resp = $e->getResponse();
$ex->setResponse($e->getResponse());
}
throw $ex;
2019-01-04 03:59:05 +00:00
}
try {
$body = \GuzzleHttp\json_decode($resp->getBody(), true);
2019-01-30 04:56:40 +00:00
app('rpc.logger')->info("client_response", $body);
2019-01-23 06:34:18 +00:00
if (empty($body)) {
2019-01-30 08:06:41 +00:00
throw new RpcServerException('http response empty', JsonRpc::Rpc_Error_System_Error);
2019-01-23 06:34:18 +00:00
}
2019-01-04 03:59:05 +00:00
if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) {
2019-01-17 07:58:12 +00:00
$message = is_array($body['error']['message']) ? json_encode($body['error']['message']) : $body['error']['message'];
2019-01-30 09:39:25 +00:00
$e = new RpcServerException($message, $body['error']['code']);
throw $e;
2019-01-04 03:59:05 +00:00
}
2019-01-23 06:34:18 +00:00
2019-01-04 03:59:05 +00:00
return $body['result'];
} catch (\InvalidArgumentException $e) {
2019-01-30 04:56:40 +00:00
app('rpc.logger')->error('client_decode_error', array_merge($this->server_config, $payload));
2019-01-30 08:06:41 +00:00
$ex = new RpcServerException($e->getMessage(), JsonRpc::Rpc_Error_Parse_Error);
if (env("APP_DEBUG") == true) {
$ex->setResponse($resp);
}
throw $ex;
2019-01-04 03:59:05 +00:00
}
}
/**
* request id
* @return int
*/
protected function id()
{
2019-01-23 09:07:46 +00:00
return $this->id++;
2019-01-04 03:59:05 +00:00
}
}