Completed
Push — master ( 5a7880...f74871 )
by Elf
02:52
created

HttpClient::getJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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