1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Guzzle HTTP JSON-RPC |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2014 Nature Delivered Ltd. <http://graze.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @see http://github.com/graze/guzzle-jsonrpc/blob/master/LICENSE |
11
|
|
|
* @link http://github.com/graze/guzzle-jsonrpc |
12
|
|
|
*/ |
13
|
|
|
namespace Graze\GuzzleHttp\JsonRpc; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use Graze\GuzzleHttp\JsonRpc\Message\MessageFactory; |
17
|
|
|
use Graze\GuzzleHttp\JsonRpc\Message\RequestInterface; |
18
|
|
|
use Graze\GuzzleHttp\JsonRpc\Message\ResponseInterface; |
19
|
|
|
use GuzzleHttp\Client as HttpClient; |
20
|
|
|
use GuzzleHttp\ClientInterface as HttpClientInterface; |
21
|
|
|
use GuzzleHttp\Message\MessageFactoryInterface; |
22
|
|
|
use GuzzleHttp\Utils as GuzzleUtils; |
23
|
|
|
|
24
|
|
|
class Client implements ClientInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var HttpClientInterface |
28
|
|
|
*/ |
29
|
|
|
protected $httpClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param HttpClientInterface $httpClient |
33
|
|
|
*/ |
34
|
10 |
|
public function __construct(HttpClientInterface $httpClient) |
35
|
|
|
{ |
36
|
10 |
|
$this->httpClient = $httpClient; |
37
|
10 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $url |
41
|
|
|
* @param array $config |
42
|
|
|
* @return Client |
43
|
|
|
*/ |
44
|
1 |
|
public static function factory($url, array $config = []) |
45
|
|
|
{ |
46
|
1 |
|
$client = new HttpClient(array_replace_recursive([ |
47
|
1 |
|
'base_url' => $url, |
48
|
1 |
|
'message_factory' => self::createMessageFactory(), |
49
|
|
|
'defaults' => [ |
50
|
|
|
'headers' => [ |
51
|
|
|
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3' |
52
|
1 |
|
] |
53
|
1 |
|
] |
54
|
1 |
|
], $config)); |
55
|
1 |
|
if (isset($config['subscribers'])) { |
56
|
|
|
array_map([$client->getEmitter(), 'attach'], $config['subscribers']); |
57
|
1 |
|
} |
58
|
1 |
|
return new self($client); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
* |
64
|
|
|
* @link http://www.jsonrpc.org/specification#notification |
65
|
|
|
* @param string $method |
66
|
|
|
* @param array $params |
|
|
|
|
67
|
|
|
* @return RequestInterface |
68
|
|
|
*/ |
69
|
2 |
View Code Duplication |
public function notification($method, array $params = null) |
|
|
|
|
70
|
|
|
{ |
71
|
2 |
|
return $this->createRequest(RequestInterface::NOTIFICATION, array_filter([ |
72
|
2 |
|
'jsonrpc' => static::SPEC, |
73
|
2 |
|
'method' => $method, |
74
|
|
|
'params' => $params |
75
|
2 |
|
])); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
* |
81
|
|
|
* @link http://www.jsonrpc.org/specification#request_object |
82
|
|
|
* @param mixed $id |
83
|
|
|
* @param string $method |
84
|
|
|
* @param array $params |
|
|
|
|
85
|
|
|
* @return RequestInterface |
86
|
|
|
*/ |
87
|
4 |
View Code Duplication |
public function request($id, $method, array $params = null) |
|
|
|
|
88
|
|
|
{ |
89
|
3 |
|
return $this->createRequest(RequestInterface::REQUEST, array_filter([ |
90
|
4 |
|
'jsonrpc' => static::SPEC, |
91
|
3 |
|
'method' => $method, |
92
|
3 |
|
'params' => $params, |
93
|
|
|
'id' => $id |
94
|
3 |
|
])); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
* |
100
|
|
|
* @param RequestInterface $request |
101
|
|
|
* @return ResponseInterface|null |
|
|
|
|
102
|
|
|
*/ |
103
|
2 |
|
public function send(RequestInterface $request) |
104
|
|
|
{ |
105
|
2 |
|
$response = $this->httpClient->send($request); |
106
|
|
|
|
107
|
2 |
|
return $request->getRpcId() ? $response : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @link http://www.jsonrpc.org/specification#batch |
114
|
|
|
* @param RequestInterface[] $requests |
115
|
|
|
* @return ResponseInterface[] |
116
|
|
|
*/ |
117
|
1 |
|
public function sendAll(array $requests) |
118
|
|
|
{ |
119
|
1 |
|
$response = $this->httpClient->send($this->createRequest( |
120
|
1 |
|
RequestInterface::BATCH, |
121
|
1 |
|
$this->getBatchRequestOptions($requests) |
122
|
1 |
|
)); |
123
|
|
|
|
124
|
1 |
|
return $this->getBatchResponses($response); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return MessageFactoryInterface |
129
|
|
|
*/ |
130
|
1 |
|
protected static function createMessageFactory() |
131
|
|
|
{ |
132
|
1 |
|
return new MessageFactory(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $method |
137
|
|
|
* @param array $options |
138
|
|
|
* @return RequestInterface |
139
|
|
|
*/ |
140
|
6 |
|
protected function createRequest($method, array $options) |
141
|
|
|
{ |
142
|
6 |
|
return $this->httpClient->createRequest($method, null, [ |
143
|
|
|
'jsonrpc' => $options |
144
|
6 |
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return MessageFactoryInterface |
149
|
|
|
*/ |
150
|
1 |
|
protected function getMessageFactory() |
151
|
|
|
{ |
152
|
|
|
// This is pretty lame, but we need the factory from the client |
153
|
|
|
$factoryExtractor = Closure::bind(function () { |
154
|
1 |
|
return $this->messageFactory; |
|
|
|
|
155
|
1 |
|
}, $this->httpClient, $this->httpClient); |
156
|
|
|
|
157
|
1 |
|
return $factoryExtractor(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param RequestInterface[] $requests |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
1 |
|
protected function getBatchRequestOptions(array $requests) |
165
|
|
|
{ |
166
|
|
|
return array_map(function (RequestInterface $request) { |
167
|
1 |
|
return GuzzleUtils::jsonDecode((string) $request->getBody()); |
168
|
1 |
|
}, $requests); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param ResponseInterface $response |
173
|
|
|
* @return ResponseInterface[] |
174
|
|
|
*/ |
175
|
1 |
|
protected function getBatchResponses(ResponseInterface $response) |
176
|
|
|
{ |
177
|
1 |
|
$factory = $this->getMessageFactory(); |
178
|
1 |
|
$results = GuzzleUtils::jsonDecode((string) $response->getBody(), true); |
179
|
|
|
|
180
|
1 |
|
return array_map(function (array $result) use ($factory, $response) { |
181
|
1 |
|
return $factory->createResponse( |
182
|
1 |
|
$response->getStatusCode(), |
183
|
1 |
|
$response->getHeaders(), |
184
|
1 |
|
Utils::jsonEncode($result) |
185
|
1 |
|
); |
186
|
1 |
|
}, $results); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.