Completed
Pull Request — master (#1)
by Elf
04:20
created

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