@@ -20,6 +20,8 @@ class Client
2020
2121 private string $ body = '' ;
2222
23+ private static $ guzzleFactory = null ;
24+
2325 private \GuzzleHttp \HandlerStack $ guzzleHandler ;
2426
2527 private string $ host = '' ;
@@ -124,8 +126,7 @@ public function delete(string $url) : ?array
124126 {
125127 try
126128 {
127- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (), 'handler ' => $ this ->guzzleHandler , ]);
128- $ response = $ guzzle ->request ('DELETE ' , $ url );
129+ $ response = $ this ->getGuzzleClient ()->request ('DELETE ' , $ url );
129130
130131 return $ this ->process ($ response );
131132 }
@@ -156,8 +157,7 @@ public function get(string $url, array $parameters) : ?array
156157 }
157158 }
158159
159- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (), 'handler ' => $ this ->guzzleHandler , ]);
160- $ response = $ guzzle ->request ('GET ' , $ url );
160+ $ response = $ this ->getGuzzleClient ()->request ('GET ' , $ url );
161161
162162 return $ this ->process ($ response );
163163 }
@@ -213,11 +213,35 @@ public function getBody() : string
213213 return $ this ->body ;
214214 }
215215
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+
216235 public function getLastError () : string
217236 {
218237 return $ this ->lastError ;
219238 }
220239
240+ public function getSessionCallback () : ?callable
241+ {
242+ return $ this ->sessionCallback ;
243+ }
244+
221245 public function getStatusCode () : int
222246 {
223247 return $ this ->statusCode ;
@@ -235,8 +259,7 @@ public function next() : array
235259 return [];
236260 }
237261
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 );
240263
241264 return $ this ->process ($ response );
242265 }
@@ -257,10 +280,7 @@ public function post(string $url, array $parameters) : ?array
257280 try
258281 {
259282 $ 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 );
264284
265285 return $ this ->process ($ response );
266286 }
@@ -281,18 +301,16 @@ public function put(string $url, array $parameters, string $method = 'PUT') : ?a
281301 try
282302 {
283303 $ json = \json_encode ($ parameters ['body ' ], JSON_PRETTY_PRINT );
284- $ guzzle = new \GuzzleHttp \Client (['headers ' => $ this ->getHeaders (
304+ $ guzzle = $ this ->getGuzzleClient (
305+ $ json ,
285306 [
286307 'Connection ' => 'keep-alive ' ,
287308 'Content-Length ' => \strlen ($ json ),
288309 'Accept-Encoding ' => 'gzip, deflate ' ,
289310 'Host ' => $ this ->host ,
290311 'Accept ' => '*/* '
291312 ]
292- ),
293- 'handler ' => $ this ->guzzleHandler ,
294- 'body ' => $ json , ]);
295-
313+ );
296314 $ response = $ guzzle ->request ($ method , $ url );
297315
298316 return $ this ->process ($ response );
@@ -341,6 +359,11 @@ public function removeScope(string $scope) : self
341359 return $ this ;
342360 }
343361
362+ public static function setGuzzleFactory (?callable $ factory ) : void
363+ {
364+ self ::$ guzzleFactory = $ factory ;
365+ }
366+
344367 public function setHost (string $ host ) : self
345368 {
346369 $ this ->host = $ host ;
0 commit comments