2019-01-04 03:59:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace JsonRpc;
|
|
|
|
|
|
|
|
use GuzzleHttp\Exception\ServerException;
|
|
|
|
use JsonRpc\Exception\RpcServerException;
|
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
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
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-17 06:10:18 +00:00
|
|
|
$this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id";
|
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-11 09:26:01 +00:00
|
|
|
'app' => $k,
|
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
|
|
|
|
*/
|
|
|
|
public function call($name, $arguments)
|
|
|
|
{
|
|
|
|
$payload = [
|
|
|
|
'jsonrpc' => '2.0',
|
|
|
|
'method' => $name,
|
|
|
|
'params' => $arguments,
|
|
|
|
'id' => $this->id(),
|
|
|
|
];
|
|
|
|
return $this->post($payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $arguments
|
|
|
|
* @return array
|
|
|
|
* @throws RpcServerException
|
|
|
|
*/
|
|
|
|
public function __call($name, $arguments)
|
|
|
|
{
|
|
|
|
return $this->call($name, $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $payload
|
|
|
|
* @throws RpcServerException
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function post($payload)
|
|
|
|
{
|
|
|
|
try {
|
2019-01-16 09:58:22 +00:00
|
|
|
|
2019-01-11 09:26:01 +00:00
|
|
|
$headers = [
|
|
|
|
'client_app' => $this->config['app'],
|
2019-01-17 06:10:18 +00:00
|
|
|
'client_host' => gethostname(),
|
|
|
|
'client_addr' => $_SERVER['SERVER_ADDR'],
|
2019-01-11 09:26:01 +00:00
|
|
|
];
|
2019-01-19 08:53:19 +00:00
|
|
|
app('rpc.logger')->info("client_request",array_merge($this->server_config, $payload));
|
2019-01-05 07:18:54 +00:00
|
|
|
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [
|
2019-01-11 09:26:01 +00:00
|
|
|
'headers' => $headers,
|
2019-01-07 08:10:26 +00:00
|
|
|
'json' => $payload,
|
2019-01-04 03:59:05 +00:00
|
|
|
]);
|
|
|
|
} catch (ServerException $e) {
|
2019-01-05 07:18:54 +00:00
|
|
|
throw new RpcServerException($e->getMessage(), $e->getCode());
|
2019-01-04 03:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$body = \GuzzleHttp\json_decode($resp->getBody(), true);
|
2019-01-19 08:53:19 +00:00
|
|
|
app('rpc.logger')->info("client_response",$body);
|
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'];
|
|
|
|
throw new RpcServerException($message, $body['error']['code']);
|
2019-01-04 03:59:05 +00:00
|
|
|
}
|
|
|
|
return $body['result'];
|
|
|
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
2019-01-19 08:53:19 +00:00
|
|
|
app('rpc.logger')->error('client_decode_error',array_merge($this->server_config, $payload));
|
2019-01-05 08:00:27 +00:00
|
|
|
throw new RpcServerException('json decode error', -32700);
|
2019-01-04 03:59:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* request id
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function id()
|
|
|
|
{
|
2019-01-14 04:03:47 +00:00
|
|
|
// return $this->id.'-'.time();
|
2019-01-17 06:10:18 +00:00
|
|
|
return $this->id;
|
2019-01-04 03:59:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|