9
9
use SendGrid \Exception \InvalidRequest ;
10
10
11
11
/**
12
- *
13
12
* Class Client
14
- * @package SendGrid
15
13
* @version 3.9.5
16
14
*
17
15
* Quickly and easily access any REST or REST-like API.
76
74
* @method Client segments()
77
75
* @method Client singlesends()
78
76
*
79
- *
80
77
* Devices
81
78
* @method Client devices()
82
79
*
@@ -206,24 +203,30 @@ class Client
206
203
protected $ retryOnLimit ;
207
204
208
205
/**
209
- * These are the supported HTTP verbs
206
+ * Supported HTTP verbs.
210
207
*
211
208
* @var array
212
209
*/
213
210
private $ methods = ['get ' , 'post ' , 'patch ' , 'put ' , 'delete ' ];
214
211
215
212
/**
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
+ ) {
227
230
$ this ->host = $ host ;
228
231
$ this ->headers = $ headers ?: [];
229
232
$ this ->version = $ version ;
@@ -275,7 +278,7 @@ public function getCurlOptions()
275
278
}
276
279
277
280
/**
278
- * Set extra options to set during curl initialization
281
+ * Set extra options to set during curl initialization.
279
282
*
280
283
* @param array $options
281
284
*
@@ -289,7 +292,7 @@ public function setCurlOptions(array $options)
289
292
}
290
293
291
294
/**
292
- * Set default retry on limit flag
295
+ * Set default retry on limit flag.
293
296
*
294
297
* @param bool $retry
295
298
*
@@ -303,7 +306,7 @@ public function setRetryOnLimit($retry)
303
306
}
304
307
305
308
/**
306
- * Set concurrent request flag
309
+ * Set concurrent request flag.
307
310
*
308
311
* @param bool $isConcurrent
309
312
*
@@ -317,7 +320,7 @@ public function setIsConcurrentRequest($isConcurrent)
317
320
}
318
321
319
322
/**
320
- * Build the final URL to be passed
323
+ * Build the final URL to be passed.
321
324
*
322
325
* @param array $queryParams an array of all the query parameters
323
326
*
@@ -329,16 +332,17 @@ private function buildUrl($queryParams = null)
329
332
if (isset ($ queryParams )) {
330
333
$ path .= '? ' . http_build_query ($ queryParams );
331
334
}
335
+
332
336
return sprintf ('%s%s%s ' , $ this ->host , $ this ->version ?: '' , $ path );
333
337
}
334
338
335
339
/**
336
340
* Creates curl options for a request
337
- * this function does not mutate any private variables
341
+ * this function does not mutate any private variables.
338
342
*
339
343
* @param string $method
340
- * @param array $body
341
- * @param array $headers
344
+ * @param array $body
345
+ * @param array $headers
342
346
*
343
347
* @return array
344
348
*/
@@ -349,7 +353,7 @@ private function createCurlOptions($method, $body = null, $headers = null)
349
353
CURLOPT_HEADER => true ,
350
354
CURLOPT_CUSTOMREQUEST => strtoupper ($ method ),
351
355
CURLOPT_SSL_VERIFYPEER => true ,
352
- CURLOPT_FAILONERROR => false
356
+ CURLOPT_FAILONERROR => false ,
353
357
] + $ this ->curlOptions ;
354
358
355
359
if (isset ($ headers )) {
@@ -369,9 +373,8 @@ private function createCurlOptions($method, $body = null, $headers = null)
369
373
}
370
374
371
375
/**
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
375
378
*
376
379
* @return array
377
380
*/
@@ -401,9 +404,9 @@ private function createCurlMultiHandle(array $requests)
401
404
}
402
405
403
406
/**
404
- * Prepare response object
407
+ * Prepare response object.
405
408
*
406
- * @param resource $channel the curl resource
409
+ * @param resource $channel the curl resource
407
410
* @param string $content
408
411
*
409
412
* @return Response object
@@ -413,17 +416,17 @@ private function parseResponse($channel, $content)
413
416
$ headerSize = curl_getinfo ($ channel , CURLINFO_HEADER_SIZE );
414
417
$ statusCode = curl_getinfo ($ channel , CURLINFO_HTTP_CODE );
415
418
416
- $ responseBody = substr ($ content , $ headerSize );
419
+ $ responseBody = mb_substr ($ content , $ headerSize );
417
420
418
- $ responseHeaders = substr ($ content , 0 , $ headerSize );
421
+ $ responseHeaders = mb_substr ($ content , 0 , $ headerSize );
419
422
$ responseHeaders = explode ("\n" , $ responseHeaders );
420
423
$ responseHeaders = array_map ('trim ' , $ responseHeaders );
421
424
422
425
return new Response ($ statusCode , $ responseBody , $ responseHeaders );
423
426
}
424
427
425
428
/**
426
- * Retry request
429
+ * Retry request.
427
430
*
428
431
* @param array $responseHeaders headers from rate limited response
429
432
* @param string $method the HTTP verb
@@ -432,12 +435,14 @@ private function parseResponse($channel, $content)
432
435
* @param array $headers original headers
433
436
*
434
437
* @return Response response object
438
+ *
435
439
* @throws InvalidRequest
436
440
*/
437
441
private function retryRequest (array $ responseHeaders , $ method , $ url , $ body , $ headers )
438
442
{
439
443
$ sleepDurations = $ responseHeaders ['X-Ratelimit-Reset ' ] - time ();
440
444
sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
445
+
441
446
return $ this ->makeRequest ($ method , $ url , $ body , $ headers , false );
442
447
}
443
448
@@ -452,6 +457,7 @@ private function retryRequest(array $responseHeaders, $method, $url, $body, $hea
452
457
* @param bool $retryOnLimit should retry if rate limit is reach?
453
458
*
454
459
* @return Response object
460
+ *
455
461
* @throws InvalidRequest
456
462
*/
457
463
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
469
475
470
476
$ response = $ this ->parseResponse ($ channel , $ content );
471
477
472
- if ($ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE && $ retryOnLimit ) {
478
+ if ($ retryOnLimit && $ response ->statusCode () === self ::TOO_MANY_REQUESTS_HTTP_CODE ) {
473
479
$ responseHeaders = $ response ->headers (true );
480
+
474
481
return $ this ->retryRequest ($ responseHeaders , $ method , $ url , $ body , $ headers );
475
482
}
476
483
@@ -480,7 +487,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
480
487
}
481
488
482
489
/**
483
- * Send all saved requests at once
490
+ * Send all saved requests at once.
484
491
*
485
492
* @param array $requests
486
493
*
@@ -504,11 +511,10 @@ public function makeAllRequests(array $requests = [])
504
511
$ responses = [];
505
512
$ sleepDurations = 0 ;
506
513
foreach ($ channels as $ id => $ channel ) {
507
-
508
514
$ content = curl_multi_getcontent ($ channel );
509
515
$ response = $ this ->parseResponse ($ channel , $ content );
510
516
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 ) {
512
518
$ headers = $ response ->headers (true );
513
519
$ sleepDurations = max ($ sleepDurations , $ headers ['X-Ratelimit-Reset ' ] - time ());
514
520
$ requestData = [
@@ -531,6 +537,7 @@ public function makeAllRequests(array $requests = [])
531
537
sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
532
538
$ responses = array_merge ($ responses , $ this ->makeAllRequests ($ retryRequests ));
533
539
}
540
+
534
541
return $ responses ;
535
542
}
536
543
@@ -557,20 +564,22 @@ public function _($name = null)
557
564
558
565
/**
559
566
* 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()).
561
568
*
562
569
* @param string $name name of the dynamic method call or HTTP verb
563
570
* @param array $args parameters passed with the method call
564
571
*
565
572
* @return Client|Response|Response[]|null object
573
+ *
566
574
* @throws InvalidRequest
567
575
*/
568
576
public function __call ($ name , $ args )
569
577
{
570
- $ name = strtolower ($ name );
578
+ $ name = mb_strtolower ($ name );
571
579
572
580
if ($ name === 'version ' ) {
573
581
$ this ->version = $ args [0 ];
582
+
574
583
return $ this ->_ ();
575
584
}
576
585
@@ -579,7 +588,7 @@ public function __call($name, $args)
579
588
return $ this ->makeAllRequests ();
580
589
}
581
590
582
- if (in_array ($ name , $ this ->methods , true )) {
591
+ if (\ in_array ($ name , $ this ->methods , true )) {
583
592
$ body = isset ($ args [0 ]) ? $ args [0 ] : null ;
584
593
$ queryParams = isset ($ args [1 ]) ? $ args [1 ] : null ;
585
594
$ url = $ this ->buildUrl ($ queryParams );
@@ -590,6 +599,7 @@ public function __call($name, $args)
590
599
// save request to be sent later
591
600
$ requestData = ['method ' => $ name , 'url ' => $ url , 'body ' => $ body , 'headers ' => $ headers ];
592
601
$ this ->savedRequests [] = $ this ->createSavedRequest ($ requestData , $ retryOnLimit );
602
+
593
603
return null ;
594
604
}
595
605
0 commit comments