1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace CmdrSharp\GuzzleApi; |
5
|
|
|
|
6
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
class Client implements RequestInterface |
10
|
|
|
{ |
11
|
|
|
/** @var GuzzleClient */ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
/** @var string */ |
15
|
|
|
protected $uri; |
16
|
|
|
|
17
|
|
|
/** @var null|array */ |
18
|
|
|
protected $body; |
19
|
|
|
|
20
|
|
|
/** @var array */ |
21
|
|
|
protected $headers = []; |
22
|
|
|
|
23
|
|
|
/** @var array */ |
24
|
|
|
protected $options = []; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $format = 'json'; |
28
|
|
|
|
29
|
|
|
/** @var bool|resource */ |
30
|
|
|
protected $debug = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Client constructor. |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
$this->client = new GuzzleClient(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new Guzzle Client specifying the Base URI. |
42
|
|
|
* |
43
|
|
|
* @param string $base_uri |
44
|
|
|
* @return RequestInterface |
45
|
|
|
*/ |
46
|
|
|
public function make(string $base_uri): RequestInterface |
47
|
|
|
{ |
48
|
|
|
$this->client = new GuzzleClient(['base_uri' => $base_uri]); |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Specify the URI for the Request. |
55
|
|
|
* |
56
|
|
|
* @param string $uri |
57
|
|
|
* @return RequestInterface |
58
|
|
|
*/ |
59
|
|
|
public function to(string $uri): RequestInterface |
60
|
|
|
{ |
61
|
|
|
$this->uri = $uri; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Specify the payload. |
68
|
|
|
* |
69
|
|
|
* @param array $body |
70
|
|
|
* @param array $headers |
71
|
|
|
* @param array $options |
72
|
|
|
* @return RequestInterface |
73
|
|
|
*/ |
74
|
|
|
public function with(array $body = [], array $headers = [], array $options = []): RequestInterface |
75
|
|
|
{ |
76
|
|
|
$this->body = $body; |
77
|
|
|
$this->headers = $headers; |
78
|
|
|
$this->options = $options; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Specify the body for the request. |
85
|
|
|
* |
86
|
|
|
* @param array $body |
87
|
|
|
* @return RequestInterface |
88
|
|
|
*/ |
89
|
|
|
public function withBody(array $body = []): RequestInterface |
90
|
|
|
{ |
91
|
|
|
$this->body = $body; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Append to existing body. |
98
|
|
|
* |
99
|
|
|
* @param array $body |
100
|
|
|
* @return RequestInterface |
101
|
|
|
*/ |
102
|
|
|
public function addBody(array $body = []): RequestInterface |
103
|
|
|
{ |
104
|
|
|
$this->body = array_merge($this->body, $body); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get existing body. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getBody(): array |
115
|
|
|
{ |
116
|
|
|
return $this->body; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Specify the headers for the request. |
121
|
|
|
* |
122
|
|
|
* @param array $headers |
123
|
|
|
* @return RequestInterface |
124
|
|
|
*/ |
125
|
|
|
public function withHeaders(array $headers = []): RequestInterface |
126
|
|
|
{ |
127
|
|
|
$this->headers = $headers; |
128
|
|
|
|
129
|
|
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Append to existing headers. |
134
|
|
|
* |
135
|
|
|
* @param array $headers |
136
|
|
|
* @return RequestInterface |
137
|
|
|
*/ |
138
|
|
|
public function addHeaders(array $headers = []): RequestInterface |
139
|
|
|
{ |
140
|
|
|
$this->headers = array_merge($this->headers, $headers); |
141
|
|
|
|
142
|
|
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Get existing headers. |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getHeaders(): array |
151
|
|
|
{ |
152
|
|
|
return $this->headers; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Specify the options for the request. |
157
|
|
|
* |
158
|
|
|
* @param array $options |
159
|
|
|
* @return RequestInterface |
160
|
|
|
*/ |
161
|
|
|
public function withOptions(array $options = []): RequestInterface |
162
|
|
|
{ |
163
|
|
|
$this->options = $options; |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Append to existing options. |
170
|
|
|
* |
171
|
|
|
* @param array $options |
172
|
|
|
* @return RequestInterface |
173
|
|
|
*/ |
174
|
|
|
public function addOptions(array $options = []): RequestInterface |
175
|
|
|
{ |
176
|
|
|
$this->options = array_merge($this->options, $options); |
177
|
|
|
|
178
|
|
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get existing options. |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function getOptions(): array |
187
|
|
|
{ |
188
|
|
|
return $this->options; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Specify the body to be Form Parameters. |
193
|
|
|
* |
194
|
|
|
* @return RequestInterface |
195
|
|
|
*/ |
196
|
|
|
public function asFormParams(): RequestInterface |
197
|
|
|
{ |
198
|
|
|
$this->format = 'form_params'; |
199
|
|
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Specify the body to be JSON. |
205
|
|
|
* |
206
|
|
|
* @return RequestInterface |
207
|
|
|
*/ |
208
|
|
|
public function asJson(): RequestInterface |
209
|
|
|
{ |
210
|
|
|
$this->format = 'json'; |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Toggle debugging. |
217
|
|
|
* |
218
|
|
|
* @param bool $debug |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function debug($debug = true): RequestInterface |
222
|
|
|
{ |
223
|
|
|
$this->debug = $debug; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Send a GET Request. |
230
|
|
|
* |
231
|
|
|
* @return ResponseInterface |
232
|
|
|
*/ |
233
|
|
|
public function get(): ResponseInterface |
234
|
|
|
{ |
235
|
|
|
return $this->makeRequest(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Send a POST Request. |
240
|
|
|
* |
241
|
|
|
* @return ResponseInterface |
242
|
|
|
*/ |
243
|
|
|
public function post(): ResponseInterface |
244
|
|
|
{ |
245
|
|
|
return $this->makeRequest('POST'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Send a PUT Request. |
250
|
|
|
* |
251
|
|
|
* @return ResponseInterface |
252
|
|
|
*/ |
253
|
|
|
public function put(): ResponseInterface |
254
|
|
|
{ |
255
|
|
|
return $this->makeRequest('PUT'); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Send a PATCH Request. |
260
|
|
|
* |
261
|
|
|
* @return ResponseInterface |
262
|
|
|
*/ |
263
|
|
|
public function patch(): ResponseInterface |
264
|
|
|
{ |
265
|
|
|
return $this->makeRequest('PATCH'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Send a DELETE Request. |
270
|
|
|
* |
271
|
|
|
* @return ResponseInterface |
272
|
|
|
*/ |
273
|
|
|
public function delete(): ResponseInterface |
274
|
|
|
{ |
275
|
|
|
return $this->makeRequest('DELETE'); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $method |
280
|
|
|
* @return ResponseInterface |
281
|
|
|
* @throws \InvalidArgumentException |
282
|
|
|
*/ |
283
|
|
|
public function request(string $method): ResponseInterface |
284
|
|
|
{ |
285
|
|
|
if (!in_array(strtolower($method), ['get', 'post', 'put', 'patch', 'delete'])) { |
286
|
|
|
throw new \InvalidArgumentException('The specified method must be either GET, POST, PUT, PATCH or DELETE'); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $this->makeRequest($method); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Sends the request. |
294
|
|
|
* |
295
|
|
|
* @param string $method |
296
|
|
|
* @return ResponseInterface |
297
|
|
|
*/ |
298
|
|
|
private function makeRequest(string $method = 'GET'): ResponseInterface |
299
|
|
|
{ |
300
|
|
|
$requestParameters = [ |
301
|
|
|
$this->format => $this->body, |
302
|
|
|
'headers' => $this->headers, |
303
|
|
|
'debug' => $this->debug, |
304
|
|
|
]; |
305
|
|
|
|
306
|
|
|
if ($this->options !== null) { |
307
|
|
|
$requestParameters = array_merge($requestParameters, $this->options); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$response = $this->client->request($method, $this->uri, $requestParameters); |
311
|
|
|
|
312
|
|
|
$this->debug = false; |
313
|
|
|
|
314
|
|
|
return $response; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|