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