@@ -20,6 +20,8 @@ class Client
20
20
21
21
private string $ body = '' ;
22
22
23
+ private static $ guzzleFactory = null ;
24
+
23
25
private \GuzzleHttp \HandlerStack $ guzzleHandler ;
24
26
25
27
private string $ host = '' ;
@@ -124,8 +126,7 @@ public function delete(string $url) : ?array
124
126
{
125
127
try
126
128
{
127
- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (), 'handler ' => $ this ->guzzleHandler , ]);
128
- $ response = $ guzzle ->request ('DELETE ' , $ url );
129
+ $ response = $ this ->getGuzzleClient ()->request ('DELETE ' , $ url );
129
130
130
131
return $ this ->process ($ response );
131
132
}
@@ -156,8 +157,7 @@ public function get(string $url, array $parameters) : ?array
156
157
}
157
158
}
158
159
159
- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (), 'handler ' => $ this ->guzzleHandler , ]);
160
- $ response = $ guzzle ->request ('GET ' , $ url );
160
+ $ response = $ this ->getGuzzleClient ()->request ('GET ' , $ url );
161
161
162
162
return $ this ->process ($ response );
163
163
}
@@ -213,11 +213,35 @@ public function getBody() : string
213
213
return $ this ->body ;
214
214
}
215
215
216
+ public function getGuzzleClient (string $ body = '' , array $ headers = []) : \GuzzleHttp \Client
217
+ {
218
+ $ config = [
219
+ 'headers ' => $ this ->getHeaders ($ headers ),
220
+ 'handler ' => $ this ->guzzleHandler , ];
221
+
222
+ if (\strlen ($ body ))
223
+ {
224
+ $ config ['body ' ] = $ body ;
225
+ }
226
+
227
+ return self ::$ guzzleFactory ? \call_user_func (self ::$ guzzleFactory , $ config ) : new \GuzzleHttp \Client ($ config );
228
+ }
229
+
230
+ public static function getGuzzleFactory () : ?callable
231
+ {
232
+ return self ::$ guzzleFactory ;
233
+ }
234
+
216
235
public function getLastError () : string
217
236
{
218
237
return $ this ->lastError ;
219
238
}
220
239
240
+ public function getSessionCallback () : ?callable
241
+ {
242
+ return $ this ->sessionCallback ;
243
+ }
244
+
221
245
public function getStatusCode () : int
222
246
{
223
247
return $ this ->statusCode ;
@@ -235,8 +259,7 @@ public function next() : array
235
259
return [];
236
260
}
237
261
238
- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (), 'handler ' => $ this ->guzzleHandler , ]);
239
- $ response = $ guzzle ->request ('GET ' , 'https://api.cc.email ' . $ this ->next );
262
+ $ response = $ this ->getGuzzleClient ()->request ('GET ' , 'https://api.cc.email ' . $ this ->next );
240
263
241
264
return $ this ->process ($ response );
242
265
}
@@ -257,10 +280,7 @@ public function post(string $url, array $parameters) : ?array
257
280
try
258
281
{
259
282
$ json = \json_encode ($ parameters ['body ' ], JSON_PRETTY_PRINT );
260
- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (),
261
- 'handler ' => $ this ->guzzleHandler ,
262
- 'body ' => $ json , ]);
263
- $ response = $ guzzle ->request ('POST ' , $ url );
283
+ $ response = $ this ->getGuzzleClient ()->request ('POST ' , $ url );
264
284
265
285
return $ this ->process ($ response );
266
286
}
@@ -281,18 +301,16 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ?a
281
301
try
282
302
{
283
303
$ json = \json_encode ($ parameters ['body ' ], JSON_PRETTY_PRINT );
284
- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (
304
+ $ guzzle = $ this ->getGuzzleClient (
305
+ $ json ,
285
306
[
286
307
'Connection ' => 'keep-alive ' ,
287
308
'Content-Length ' => \strlen ($ json ),
288
309
'Accept-Encoding ' => 'gzip, deflate ' ,
289
310
'Host ' => $ this ->host ,
290
311
'Accept ' => '*/* '
291
312
]
292
- ),
293
- 'handler ' => $ this ->guzzleHandler ,
294
- 'body ' => $ json , ]);
295
-
313
+ );
296
314
$ response = $ guzzle ->request ($ method , $ url );
297
315
298
316
return $ this ->process ($ response );
@@ -341,6 +359,11 @@ public function removeScope(string $scope) : self
341
359
return $ this ;
342
360
}
343
361
362
+ public static function setGuzzleFactory (?callable $ factory ) : void
363
+ {
364
+ self ::$ guzzleFactory = $ factory ;
365
+ }
366
+
344
367
public function setHost (string $ host ) : self
345
368
{
346
369
$ this ->host = $ host ;
0 commit comments