99use SendGrid \Exception \InvalidRequest ;
1010
1111/**
12- *
1312 * Class Client
14- * @package SendGrid
1513 * @version 3.9.5
1614 *
1715 * Quickly and easily access any REST or REST-like API.
7674 * @method Client segments()
7775 * @method Client singlesends()
7876 *
79- *
8077 * Devices
8178 * @method Client devices()
8279 *
@@ -206,24 +203,30 @@ class Client
206203 protected $ retryOnLimit ;
207204
208205 /**
209- * These are the supported HTTP verbs
206+ * Supported HTTP verbs.
210207 *
211208 * @var array
212209 */
213210 private $ methods = ['get ' , 'post ' , 'patch ' , 'put ' , 'delete ' ];
214211
215212 /**
216- * Initialize the client
217- *
218- * @param string $host the base url (e.g. https://api.sendgrid.com)
219- * @param array $headers global request headers
220- * @param string $version api version (configurable) - this is specific to the SendGrid API
221- * @param array $path holds the segments of the url path
222- * @param array $curlOptions extra options to set during curl initialization
223- * @param bool $retryOnLimit set default retry on limit flag
224- */
225- public function __construct ($ host , $ headers = null , $ version = null , $ path = null , $ curlOptions = null , $ retryOnLimit = false )
226- {
213+ * Initialize the client.
214+ *
215+ * @param string $host the base url (e.g. https://api.sendgrid.com)
216+ * @param array $headers global request headers
217+ * @param string $version api version (configurable) - this is specific to the SendGrid API
218+ * @param array $path holds the segments of the url path
219+ * @param array $curlOptions extra options to set during curl initialization
220+ * @param bool $retryOnLimit set default retry on limit flag
221+ */
222+ public function __construct (
223+ $ host ,
224+ $ headers = null ,
225+ $ version = null ,
226+ $ path = null ,
227+ $ curlOptions = null ,
228+ $ retryOnLimit = false
229+ ) {
227230 $ this ->host = $ host ;
228231 $ this ->headers = $ headers ?: [];
229232 $ this ->version = $ version ;
@@ -275,7 +278,7 @@ public function getCurlOptions()
275278 }
276279
277280 /**
278- * Set extra options to set during curl initialization
281+ * Set extra options to set during curl initialization.
279282 *
280283 * @param array $options
281284 *
@@ -289,7 +292,7 @@ public function setCurlOptions(array $options)
289292 }
290293
291294 /**
292- * Set default retry on limit flag
295+ * Set default retry on limit flag.
293296 *
294297 * @param bool $retry
295298 *
@@ -303,7 +306,7 @@ public function setRetryOnLimit($retry)
303306 }
304307
305308 /**
306- * Set concurrent request flag
309+ * Set concurrent request flag.
307310 *
308311 * @param bool $isConcurrent
309312 *
@@ -317,7 +320,7 @@ public function setIsConcurrentRequest($isConcurrent)
317320 }
318321
319322 /**
320- * Build the final URL to be passed
323+ * Build the final URL to be passed.
321324 *
322325 * @param array $queryParams an array of all the query parameters
323326 *
@@ -329,16 +332,17 @@ private function buildUrl($queryParams = null)
329332 if (isset ($ queryParams )) {
330333 $ path .= '? ' . http_build_query ($ queryParams );
331334 }
335+
332336 return sprintf ('%s%s%s ' , $ this ->host , $ this ->version ?: '' , $ path );
333337 }
334338
335339 /**
336340 * Creates curl options for a request
337- * this function does not mutate any private variables
341+ * this function does not mutate any private variables.
338342 *
339343 * @param string $method
340- * @param array $body
341- * @param array $headers
344+ * @param array $body
345+ * @param array $headers
342346 *
343347 * @return array
344348 */
@@ -349,7 +353,7 @@ private function createCurlOptions($method, $body = null, $headers = null)
349353 CURLOPT_HEADER => true ,
350354 CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
351355 CURLOPT_SSL_VERIFYPEER => true ,
352- CURLOPT_FAILONERROR => false
356+ CURLOPT_FAILONERROR => false ,
353357 ] + $ this ->curlOptions ;
354358
355359 if (isset ($ headers )) {
@@ -369,9 +373,8 @@ private function createCurlOptions($method, $body = null, $headers = null)
369373 }
370374
371375 /**
372- * @param array $requestData
373- * e.g. ['method' => 'POST', 'url' => 'www.example.com', 'body' => 'test body', 'headers' => []]
374- * @param bool $retryOnLimit
376+ * @param array $requestData (method, url, body and headers)
377+ * @param bool $retryOnLimit
375378 *
376379 * @return array
377380 */
@@ -401,9 +404,9 @@ private function createCurlMultiHandle(array $requests)
401404 }
402405
403406 /**
404- * Prepare response object
407+ * Prepare response object.
405408 *
406- * @param resource $channel the curl resource
409+ * @param resource $channel the curl resource
407410 * @param string $content
408411 *
409412 * @return Response object
@@ -413,17 +416,17 @@ private function parseResponse($channel, $content)
413416 $ headerSize = curl_getinfo ($ channel , CURLINFO_HEADER_SIZE );
414417 $ statusCode = curl_getinfo ($ channel , CURLINFO_HTTP_CODE );
415418
416- $ responseBody = substr ($ content , $ headerSize );
419+ $ responseBody = mb_substr ($ content , $ headerSize );
417420
418- $ responseHeaders = substr ($ content , 0 , $ headerSize );
421+ $ responseHeaders = mb_substr ($ content , 0 , $ headerSize );
419422 $ responseHeaders = explode ("\n" , $ responseHeaders );
420423 $ responseHeaders = array_map ('trim ' , $ responseHeaders );
421424
422425 return new Response ($ statusCode , $ responseBody , $ responseHeaders );
423426 }
424427
425428 /**
426- * Retry request
429+ * Retry request.
427430 *
428431 * @param array $responseHeaders headers from rate limited response
429432 * @param string $method the HTTP verb
@@ -432,12 +435,14 @@ private function parseResponse($channel, $content)
432435 * @param array $headers original headers
433436 *
434437 * @return Response response object
438+ *
435439 * @throws InvalidRequest
436440 */
437441 private function retryRequest (array $ responseHeaders , $ method , $ url , $ body , $ headers )
438442 {
439443 $ sleepDurations = $ responseHeaders ['X-Ratelimit-Reset ' ] - time ();
440444 sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
445+
441446 return $ this ->makeRequest ($ method , $ url , $ body , $ headers , false );
442447 }
443448
@@ -452,6 +457,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea
452457 * @param bool $retryOnLimit should retry if rate limit is reach?
453458 *
454459 * @return Response object
460+ *
455461 * @throws InvalidRequest
456462 */
457463 public function makeRequest ($ method , $ url , $ body = null , $ headers = null , $ retryOnLimit = false )
@@ -469,8 +475,9 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
469475
470476 $ response = $ this ->parseResponse ($ channel , $ content );
471477
472- if ($ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE && $ retryOnLimit ) {
478+ if ($ retryOnLimit && $ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE ) {
473479 $ responseHeaders = $ response ->headers (true );
480+
474481 return $ this ->retryRequest ($ responseHeaders , $ method , $ url , $ body , $ headers );
475482 }
476483
@@ -480,7 +487,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
480487 }
481488
482489 /**
483- * Send all saved requests at once
490+ * Send all saved requests at once.
484491 *
485492 * @param array $requests
486493 *
@@ -504,11 +511,10 @@ public function makeAllRequests(array $requests = [])
504511 $ responses = [];
505512 $ sleepDurations = 0 ;
506513 foreach ($ channels as $ id => $ channel ) {
507-
508514 $ content = curl_multi_getcontent ($ channel );
509515 $ response = $ this ->parseResponse ($ channel , $ content );
510516
511- if ($ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE && $ requests [ $ id ][ ' retryOnLimit ' ] ) {
517+ if ($ requests [ $ id ][ ' retryOnLimit ' ] && $ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE ) {
512518 $ headers = $ response ->headers (true );
513519 $ sleepDurations = max ($ sleepDurations , $ headers ['X-Ratelimit-Reset ' ] - time ());
514520 $ requestData = [
@@ -531,6 +537,7 @@ public function makeAllRequests(array $requests = [])
531537 sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
532538 $ responses = array_merge ($ responses , $ this ->makeAllRequests ($ retryRequests ));
533539 }
540+
534541 return $ responses ;
535542 }
536543
@@ -557,20 +564,22 @@ public function _($name = null)
557564
558565 /**
559566 * Dynamically add method calls to the url, then call a method.
560- * (e.g. client.name.name.method())
567+ * (e.g. client.name.name.method()).
561568 *
562569 * @param string $name name of the dynamic method call or HTTP verb
563570 * @param array $args parameters passed with the method call
564571 *
565572 * @return Client|Response|Response[]|null object
573+ *
566574 * @throws InvalidRequest
567575 */
568576 public function __call ($ name , $ args )
569577 {
570- $ name = strtolower ($ name );
578+ $ name = mb_strtolower ($ name );
571579
572580 if ($ name === 'version ' ) {
573581 $ this ->version = $ args [0 ];
582+
574583 return $ this ->_ ();
575584 }
576585
@@ -579,7 +588,7 @@ public function __call($name, $args)
579588 return $ this ->makeAllRequests ();
580589 }
581590
582- if (in_array ($ name , $ this ->methods , true )) {
591+ if (\ in_array ($ name , $ this ->methods , true )) {
583592 $ body = isset ($ args [0 ]) ? $ args [0 ] : null ;
584593 $ queryParams = isset ($ args [1 ]) ? $ args [1 ] : null ;
585594 $ url = $ this ->buildUrl ($ queryParams );
@@ -590,6 +599,7 @@ public function __call($name, $args)
590599 // save request to be sent later
591600 $ requestData = ['method ' => $ name , 'url ' => $ url , 'body ' => $ body , 'headers ' => $ headers ];
592601 $ this ->savedRequests [] = $ this ->createSavedRequest ($ requestData , $ retryOnLimit );
602+
593603 return null ;
594604 }
595605
0 commit comments