2019-01-04 03:59:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace JsonRpc;
|
|
|
|
|
|
|
|
use GuzzleHttp\Exception\ServerException;
|
|
|
|
use JsonRpc\Exception\RpcServerException;
|
|
|
|
|
|
|
|
class Client
|
|
|
|
{
|
2019-01-05 07:18:54 +00:00
|
|
|
protected $config;
|
2019-01-04 03:59:05 +00:00
|
|
|
|
2019-01-05 07:18:54 +00:00
|
|
|
protected $id;
|
2019-01-07 08:10:26 +00:00
|
|
|
/**
|
|
|
|
* @var \GuzzleHttp\Client
|
|
|
|
*/
|
2019-01-04 03:59:05 +00:00
|
|
|
protected $http;
|
|
|
|
|
2019-01-05 07:18:54 +00:00
|
|
|
public function __construct($config)
|
2019-01-04 03:59:05 +00:00
|
|
|
{
|
2019-01-07 08:10:26 +00:00
|
|
|
$default = [
|
|
|
|
'app' => '***',
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->config = array_merge($default, $config);
|
2019-01-04 03:59:05 +00:00
|
|
|
$this->id = 0;
|
2019-01-05 07:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function endpoint($k)
|
|
|
|
{
|
|
|
|
$config = $this->config[$k];
|
|
|
|
|
|
|
|
$default = [
|
2019-01-04 03:59:05 +00:00
|
|
|
'timeout' => 3,
|
|
|
|
'allow_redirects' => false,
|
2019-01-05 07:18:54 +00:00
|
|
|
];
|
|
|
|
|
2019-01-05 08:00:27 +00:00
|
|
|
$this->http = new \GuzzleHttp\Client(array_merge($default, $config));
|
|
|
|
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-05 07:18:54 +00:00
|
|
|
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [
|
2019-01-07 08:10:26 +00:00
|
|
|
'headers' => [
|
|
|
|
'hwmc_app' => $this->config['app'],
|
|
|
|
],
|
|
|
|
'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);
|
|
|
|
|
|
|
|
if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) {
|
|
|
|
throw new RpcServerException($body['error']['message'], $body['error']['code']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $body['result'];
|
|
|
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
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()
|
|
|
|
{
|
|
|
|
$this->id++;
|
|
|
|
return $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|