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