|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of JSON RPC Client. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Transport\Http; |
|
12
|
|
|
|
|
13
|
|
|
use GuzzleHttp\ClientInterface; |
|
14
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
use Strider2038\JsonRpcClient\Exception\RemoteProcedureCallFailedException; |
|
17
|
|
|
use Strider2038\JsonRpcClient\Transport\TransportInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @internal |
|
21
|
|
|
* |
|
22
|
|
|
* @author Igor Lazarev <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class GuzzleTransport implements TransportInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var ClientInterface */ |
|
27
|
|
|
private $client; |
|
28
|
|
|
|
|
29
|
12 |
|
public function __construct(ClientInterface $client) |
|
30
|
|
|
{ |
|
31
|
12 |
|
$this->client = $client; |
|
32
|
12 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @throws RemoteProcedureCallFailedException |
|
36
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
37
|
|
|
*/ |
|
38
|
8 |
|
public function send(string $request): string |
|
39
|
|
|
{ |
|
40
|
8 |
|
$response = $this->sendRequest($request); |
|
41
|
|
|
|
|
42
|
6 |
|
return $this->getResponseContents($response); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws RemoteProcedureCallFailedException |
|
47
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
48
|
|
|
*/ |
|
49
|
8 |
|
private function sendRequest(string $request): ResponseInterface |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
8 |
|
$response = $this->client->request('POST', '', [ |
|
53
|
8 |
|
'body' => $request, |
|
54
|
|
|
'headers' => [ |
|
55
|
|
|
'Content-Type' => 'application/json', |
|
56
|
|
|
], |
|
57
|
|
|
]); |
|
58
|
2 |
|
} catch (RequestException $exception) { |
|
59
|
2 |
|
throw new RemoteProcedureCallFailedException( |
|
60
|
2 |
|
sprintf('JSON RPC request failed with error: %s.', $exception->getMessage()), |
|
61
|
2 |
|
0, |
|
62
|
2 |
|
$exception |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
6 |
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @throws RemoteProcedureCallFailedException |
|
71
|
|
|
*/ |
|
72
|
6 |
|
private function getResponseContents(ResponseInterface $response): string |
|
73
|
|
|
{ |
|
74
|
6 |
|
$statusCode = $response->getStatusCode(); |
|
75
|
6 |
|
$body = $response->getBody(); |
|
76
|
6 |
|
$contents = $body->getContents(); |
|
77
|
|
|
|
|
78
|
6 |
|
if (200 !== $statusCode) { |
|
79
|
1 |
|
throw new RemoteProcedureCallFailedException( |
|
80
|
1 |
|
sprintf('JSON RPC request failed with error %d: %s.', $statusCode, $contents) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
5 |
|
return $contents; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|