|
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 GuzzleClient $client; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected string $uri = ''; |
|
16
|
|
|
|
|
17
|
|
|
/** @var mixed */ |
|
18
|
|
|
protected $body = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var array */ |
|
21
|
|
|
protected array $headers = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array */ |
|
24
|
|
|
protected array $options = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected string $format = 'json'; |
|
28
|
|
|
|
|
29
|
|
|
/** @var resource|bool */ |
|
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 mixed $body |
|
70
|
|
|
* @param array $headers |
|
71
|
|
|
* @param array $options |
|
72
|
|
|
* @return RequestInterface |
|
73
|
|
|
*/ |
|
74
|
|
|
public function with($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 mixed $body |
|
87
|
|
|
* @return RequestInterface |
|
88
|
|
|
*/ |
|
89
|
|
|
public function withBody($body = []): RequestInterface |
|
90
|
|
|
{ |
|
91
|
|
|
$this->body = $body; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Append to existing body. |
|
98
|
|
|
* |
|
99
|
|
|
* @param mixed $body |
|
100
|
|
|
* @return RequestInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
public function addBody($body = []): RequestInterface |
|
103
|
|
|
{ |
|
104
|
|
|
if (is_array($body) && is_array($this->body)) { |
|
105
|
|
|
$this->body = array_merge($this->body, $body); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
if (is_string($body) && is_string($this->body)) { |
|
109
|
|
|
$this->body .= $body; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get existing body. |
|
117
|
|
|
* |
|
118
|
|
|
* @return mixed |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getBody() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->body; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Specify the headers for the request. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $headers |
|
129
|
|
|
* @return RequestInterface |
|
130
|
|
|
*/ |
|
131
|
|
|
public function withHeaders(array $headers = []): RequestInterface |
|
132
|
|
|
{ |
|
133
|
|
|
$this->headers = $headers; |
|
134
|
|
|
|
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Append to existing headers. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $headers |
|
142
|
|
|
* @return RequestInterface |
|
143
|
|
|
*/ |
|
144
|
|
|
public function addHeaders(array $headers = []): RequestInterface |
|
145
|
|
|
{ |
|
146
|
|
|
$this->headers = array_merge($this->headers, $headers); |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get existing headers. |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getHeaders(): array |
|
157
|
|
|
{ |
|
158
|
|
|
return $this->headers; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Specify the options for the request. |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $options |
|
165
|
|
|
* @return RequestInterface |
|
166
|
|
|
*/ |
|
167
|
|
|
public function withOptions(array $options = []): RequestInterface |
|
168
|
|
|
{ |
|
169
|
|
|
$this->options = $options; |
|
170
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Append to existing options. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $options |
|
178
|
|
|
* @return RequestInterface |
|
179
|
|
|
*/ |
|
180
|
|
|
public function addOptions(array $options = []): RequestInterface |
|
181
|
|
|
{ |
|
182
|
|
|
$this->options = array_merge($this->options, $options); |
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get existing options. |
|
189
|
|
|
* |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getOptions(): array |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->options; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Specify the body to be Form Parameters. |
|
199
|
|
|
* |
|
200
|
|
|
* @return RequestInterface |
|
201
|
|
|
*/ |
|
202
|
|
|
public function asFormParams(): RequestInterface |
|
203
|
|
|
{ |
|
204
|
|
|
$this->format = 'form_params'; |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Specify the body to be JSON. |
|
211
|
|
|
* |
|
212
|
|
|
* @return RequestInterface |
|
213
|
|
|
*/ |
|
214
|
|
|
public function asJson(): RequestInterface |
|
215
|
|
|
{ |
|
216
|
|
|
$this->format = 'json'; |
|
217
|
|
|
|
|
218
|
|
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Specify the body to be a string. |
|
223
|
|
|
* |
|
224
|
|
|
* @return RequestInterface |
|
225
|
|
|
*/ |
|
226
|
|
|
public function asString(): RequestInterface |
|
227
|
|
|
{ |
|
228
|
|
|
$this->format = 'body'; |
|
229
|
|
|
|
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Toggle debugging. |
|
235
|
|
|
* |
|
236
|
|
|
* @param resource|bool $debug |
|
237
|
|
|
* @return $this |
|
238
|
|
|
*/ |
|
239
|
|
|
public function debug($debug = false): RequestInterface |
|
240
|
|
|
{ |
|
241
|
|
|
$this->debug = $debug; |
|
242
|
|
|
|
|
243
|
|
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Send a GET Request. |
|
248
|
|
|
* |
|
249
|
|
|
* @return ResponseInterface |
|
250
|
|
|
*/ |
|
251
|
|
|
public function get(): ResponseInterface |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->makeRequest(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Send a POST Request. |
|
258
|
|
|
* |
|
259
|
|
|
* @return ResponseInterface |
|
260
|
|
|
*/ |
|
261
|
|
|
public function post(): ResponseInterface |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->makeRequest('POST'); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Send a PUT Request. |
|
268
|
|
|
* |
|
269
|
|
|
* @return ResponseInterface |
|
270
|
|
|
*/ |
|
271
|
|
|
public function put(): ResponseInterface |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->makeRequest('PUT'); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Send a PATCH Request. |
|
278
|
|
|
* |
|
279
|
|
|
* @return ResponseInterface |
|
280
|
|
|
*/ |
|
281
|
|
|
public function patch(): ResponseInterface |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->makeRequest('PATCH'); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Send a DELETE Request. |
|
288
|
|
|
* |
|
289
|
|
|
* @return ResponseInterface |
|
290
|
|
|
*/ |
|
291
|
|
|
public function delete(): ResponseInterface |
|
292
|
|
|
{ |
|
293
|
|
|
return $this->makeRequest('DELETE'); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @param string $method |
|
298
|
|
|
* @return ResponseInterface |
|
299
|
|
|
* @throws \InvalidArgumentException |
|
300
|
|
|
*/ |
|
301
|
|
|
public function request(string $method): ResponseInterface |
|
302
|
|
|
{ |
|
303
|
|
|
if (!in_array(strtolower($method), ['get', 'post', 'put', 'patch', 'delete'])) { |
|
304
|
|
|
throw new \InvalidArgumentException('The specified method must be either GET, POST, PUT, PATCH or DELETE'); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
return $this->makeRequest($method); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Sends the request. |
|
312
|
|
|
* |
|
313
|
|
|
* @param string $method |
|
314
|
|
|
* @return ResponseInterface |
|
315
|
|
|
*/ |
|
316
|
|
|
private function makeRequest(string $method = 'GET'): ResponseInterface |
|
317
|
|
|
{ |
|
318
|
|
|
$requestParameters = [ |
|
319
|
|
|
$this->format => $this->body, |
|
320
|
|
|
'headers' => $this->headers, |
|
321
|
|
|
'debug' => $this->debug, |
|
322
|
|
|
]; |
|
323
|
|
|
|
|
324
|
|
|
if ($this->options !== null) { |
|
325
|
|
|
$requestParameters = array_merge($requestParameters, $this->options); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$response = $this->client->request($method, $this->uri, $requestParameters); |
|
329
|
|
|
|
|
330
|
|
|
$this->debug = false; |
|
331
|
|
|
|
|
332
|
|
|
return $response; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|