Completed
Push — master ( 9575c3...983a32 )
by Elf
03:32
created

HttpClient::requestJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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