1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use Psr\Http\Message\UriInterface; |
12
|
|
|
|
13
|
|
|
class HttpClient |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The default request options. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected static $defaultOptions = [ |
21
|
|
|
'connect_timeout' => 5, |
22
|
|
|
'timeout' => 25, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The Guzzle client. |
27
|
|
|
* |
28
|
|
|
* @var \GuzzleHttp\Client |
29
|
|
|
*/ |
30
|
|
|
protected $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Guzzle response. |
34
|
|
|
* |
35
|
|
|
* @var \GuzzleHttp\Psr7\Response |
36
|
|
|
*/ |
37
|
|
|
protected $response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The request options. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $options = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Indicate whether to catch Guzzle exceptions. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $catchExceptions = true; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the default request options. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
24 |
|
public static function defaultOptions() |
59
|
|
|
{ |
60
|
24 |
|
return static::$defaultOptions; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Set the default request options. |
65
|
|
|
* |
66
|
|
|
* @param array $options |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
1 |
|
public static function setDefaultOptions(array $options) |
70
|
|
|
{ |
71
|
1 |
|
static::$defaultOptions = $options; |
72
|
1 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create a http client instance. |
76
|
|
|
* |
77
|
|
|
* @param array|string|\Psr\Http\Message\UriInterface $options base URI or any request options |
78
|
|
|
* |
79
|
|
|
* @throws \InvalidArgumentException |
80
|
|
|
*/ |
81
|
23 |
|
public function __construct($options = []) |
82
|
|
|
{ |
83
|
23 |
|
if (is_string($options) || $options instanceof UriInterface) { |
84
|
1 |
|
$options = ['base_uri' => $options]; |
85
|
22 |
|
} elseif (! is_array($options)) { |
86
|
|
|
throw new InvalidArgumentException('config must be a string, UriInterface, or an array'); |
87
|
|
|
} |
88
|
|
|
|
89
|
23 |
|
$this->client = new Client( |
90
|
23 |
|
$this->options = $options + static::defaultOptions() |
91
|
|
|
); |
92
|
23 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the Guzzle client instance. |
96
|
|
|
* |
97
|
|
|
* @return \GuzzleHttp\Client |
98
|
|
|
*/ |
99
|
2 |
|
public function getClient() |
100
|
|
|
{ |
101
|
2 |
|
return $this->client; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get whether to catch Guzzle exceptions or not. |
106
|
|
|
* |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
|
|
public function areExceptionsCaught() |
110
|
|
|
{ |
111
|
|
|
return $this->catchExceptions; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Set whether to catch Guzzle exceptions or not. |
116
|
|
|
* |
117
|
|
|
* @param bool $catch |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function catchExceptions($catch) |
121
|
|
|
{ |
122
|
|
|
$this->catchExceptions = (bool) $catch; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the request options. |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
9 |
|
public function getOptions() |
133
|
|
|
{ |
134
|
9 |
|
return $this->options; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Set the request options. |
139
|
|
|
* |
140
|
|
|
* @param array $options |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function setOptions(array $options) |
144
|
|
|
{ |
145
|
|
|
$this->options = $options; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Merge the given options to the request options. |
152
|
|
|
* |
153
|
|
|
* @param array $options |
154
|
|
|
* @return $this |
155
|
|
|
*/ |
156
|
|
|
public function mergeOptions(array $options) |
157
|
|
|
{ |
158
|
|
|
return $this->setOptions($options + $this->options); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Remove options using "dot" notation. |
163
|
|
|
* |
164
|
|
|
* @param string|array|null $key |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
16 |
|
public function removeOptions($key = null) |
168
|
|
|
{ |
169
|
16 |
|
if (is_null($key)) { |
170
|
16 |
|
$this->options = []; |
171
|
|
|
} else { |
172
|
1 |
|
Arr::forget($this->options, is_array($key) ? $key : func_get_args()); |
173
|
|
|
} |
174
|
|
|
|
175
|
16 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get a request option using "dot" notation. |
180
|
|
|
* |
181
|
|
|
* @param string $key |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
3 |
|
public function getOption($key) |
185
|
|
|
{ |
186
|
3 |
|
return Arr::get($this->options, $key); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set a request option using "dot" notation. |
191
|
|
|
* |
192
|
|
|
* @param string|array $key |
193
|
|
|
* @param mixed $value |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
10 |
|
public function option($key, $value = null) |
197
|
|
|
{ |
198
|
10 |
|
$keys = is_array($key) ? $key : [$key => $value]; |
199
|
|
|
|
200
|
10 |
|
foreach ($keys as $key => $value) { |
201
|
10 |
|
Arr::set($this->options, $key, $value); |
202
|
|
|
} |
203
|
|
|
|
204
|
10 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Set the request header. |
209
|
|
|
* |
210
|
|
|
* @param string $name |
211
|
|
|
* @param mixed $value |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
4 |
|
public function header($name, $value) |
215
|
|
|
{ |
216
|
4 |
|
return $this->option('headers.'.$name, $value); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Set the request content type. |
221
|
|
|
* |
222
|
|
|
* @param string $type |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
1 |
|
public function contentType($type) |
226
|
|
|
{ |
227
|
1 |
|
return $this->header('Content-Type', $type); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set the request accept type. |
232
|
|
|
* |
233
|
|
|
* @param string $type |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
2 |
|
public function accept($type) |
237
|
|
|
{ |
238
|
2 |
|
return $this->header('Accept', $type); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Set the request accept type to JSON. |
243
|
|
|
* |
244
|
|
|
* @return $this |
245
|
|
|
*/ |
246
|
1 |
|
public function acceptJson() |
247
|
|
|
{ |
248
|
1 |
|
return $this->accept('application/json'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Specify where the body of a response will be saved. |
253
|
|
|
* Set the "sink" option. |
254
|
|
|
* |
255
|
|
|
* @param mixed $dest |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
1 |
|
public function saveTo($dest) |
259
|
|
|
{ |
260
|
1 |
|
return $this->removeOptions('save_to')->option('sink', $dest); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Get the Guzzle response instance. |
265
|
|
|
* |
266
|
|
|
* @return \GuzzleHttp\Psr7\Response|null |
267
|
|
|
*/ |
268
|
8 |
|
public function getResponse() |
269
|
|
|
{ |
270
|
8 |
|
return $this->response; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Get data from the response. |
275
|
|
|
* |
276
|
|
|
* @param string|\Closure $callback |
277
|
|
|
* @param array $parameters |
278
|
|
|
* @param mixed $default |
279
|
|
|
* @return mixed |
280
|
|
|
*/ |
281
|
2 |
|
protected function getResponseData($callback, array $parameters = [], $default = null) |
282
|
|
|
{ |
283
|
2 |
|
if ($this->response) { |
284
|
2 |
|
return $callback instanceof Closure |
285
|
|
|
? $callback($this->response, ...$parameters) |
286
|
2 |
|
: $this->response->$callback(...$parameters); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $default; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Get response content. |
294
|
|
|
* |
295
|
|
|
* @return string|null |
296
|
|
|
*/ |
297
|
2 |
|
public function getContent() |
298
|
|
|
{ |
299
|
2 |
|
if ($this->response) { |
300
|
2 |
|
return (string) $this->getBody(); |
|
|
|
|
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get JSON decoded response content. |
306
|
|
|
* |
307
|
|
|
* @param bool $assoc |
308
|
|
|
* @return mixed |
309
|
|
|
*/ |
310
|
1 |
|
public function getJson($assoc = true) |
311
|
|
|
{ |
312
|
1 |
|
if ($this->response) { |
313
|
1 |
|
return json_decode($this->getContent(), $assoc); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Make request to a URI. |
319
|
|
|
* |
320
|
|
|
* @param string $uri |
321
|
|
|
* @param string $method |
322
|
|
|
* @param array $options |
323
|
|
|
* @return $this |
324
|
|
|
*/ |
325
|
9 |
|
public function request($uri, $method = 'GET', array $options = []) |
326
|
|
|
{ |
327
|
|
|
try { |
328
|
9 |
|
$this->response = $this->client->request( |
329
|
9 |
|
$method, $uri, $options += $this->options |
330
|
|
|
); |
331
|
1 |
|
} catch (Exception $e) { |
332
|
1 |
|
if (! $this->catchExceptions) { |
333
|
|
|
throw $e; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
9 |
|
return $this; |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Make request to a URI, expecting JSON content. |
342
|
|
|
* |
343
|
|
|
* @param string $uri |
344
|
|
|
* @param string $method |
345
|
|
|
* @param array $options |
346
|
|
|
* @return $this |
347
|
|
|
*/ |
348
|
3 |
|
public function requestJson($uri, $method = 'GET', array $options = []) |
349
|
|
|
{ |
350
|
3 |
|
Arr::set($options, 'headers.Accept', 'application/json'); |
351
|
|
|
|
352
|
3 |
|
return $this->request($uri, $method, $options); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Request the URI and return the response content. |
357
|
|
|
* |
358
|
|
|
* @param string $uri |
359
|
|
|
* @param string $method |
360
|
|
|
* @param array $options |
361
|
|
|
* @return string|null |
362
|
|
|
*/ |
363
|
1 |
|
public function fetchContent($uri, $method = 'GET', array $options = []) |
364
|
|
|
{ |
365
|
1 |
|
return $this->request($uri, $method, $options)->getContent(); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Request the URI and return the JSON decoded response content. |
370
|
|
|
* |
371
|
|
|
* @param string $uri |
372
|
|
|
* @param string $method |
373
|
|
|
* @param array $options |
374
|
|
|
* @param bool $assoc |
375
|
|
|
* @return mixed |
376
|
|
|
*/ |
377
|
1 |
|
public function fetchJson($uri, $method = 'GET', array $options = [], $assoc = true) |
378
|
|
|
{ |
379
|
1 |
|
return $this->requestJson($uri, $method, $options)->getJson($assoc); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get the dynamic response methods. |
384
|
|
|
* |
385
|
|
|
* @return array |
386
|
|
|
*/ |
387
|
6 |
|
protected function getDynamicResponseMethods() |
388
|
|
|
{ |
389
|
|
|
return [ |
390
|
6 |
|
'getStatusCode', 'getReasonPhrase', 'getProtocolVersion', |
391
|
|
|
'getHeaders', 'hasHeader', 'getHeader', 'getHeaderLine', 'getBody', |
392
|
|
|
]; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Dynamically methods to set request option, send request, or get |
397
|
|
|
* response properties. |
398
|
|
|
* |
399
|
|
|
* @param string $method |
400
|
|
|
* @param array $args |
401
|
|
|
* @return mixed |
402
|
|
|
*/ |
403
|
8 |
|
public function __call($method, $args) |
404
|
|
|
{ |
405
|
|
|
// Handle magic request methods |
406
|
8 |
|
if (in_array($method, ['get', 'head', 'put', 'post', 'patch', 'delete'])) { |
407
|
2 |
|
if (count($args) < 1) { |
408
|
|
|
throw new InvalidArgumentException('Magic request methods require an URI and optional options array'); |
409
|
|
|
} |
410
|
|
|
|
411
|
2 |
|
$url = $args[0]; |
412
|
2 |
|
$options = isset($args[1]) ? $args[1] : []; |
413
|
|
|
|
414
|
2 |
|
return $this->request($url, $method, $options); |
415
|
|
|
} |
416
|
|
|
|
417
|
6 |
|
if (in_array($method, $this->getDynamicResponseMethods())) { |
418
|
2 |
|
return $this->getResponseData($method, $args); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
// Handle setting request options |
422
|
4 |
|
return $this->option(Str::snake($method), $args[0]); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: