@@ -32,17 +32,20 @@ class Client
32
32
protected $ curlOptions ;
33
33
/** @var array */
34
34
private $ methods ;
35
+ /** @var bool */
36
+ private $ retryOnLimit ;
35
37
36
38
/**
37
39
* Initialize the client
38
40
*
39
- * @param string $host the base url (e.g. https://api.sendgrid.com)
40
- * @param array $headers global request headers
41
- * @param string $version api version (configurable)
42
- * @param array $path holds the segments of the url path
43
- * @param array $curlOptions extra options to set during curl initialization
41
+ * @param string $host the base url (e.g. https://api.sendgrid.com)
42
+ * @param array $headers global request headers
43
+ * @param string $version api version (configurable)
44
+ * @param array $path holds the segments of the url path
45
+ * @param array $curlOptions extra options to set during curl initialization
46
+ * @param bool $retryOnLimit set default retry on limit flag
44
47
*/
45
- public function __construct ($ host , $ headers = null , $ version = null , $ path = null , $ curlOptions = null )
48
+ public function __construct ($ host , $ headers = null , $ version = null , $ path = null , $ curlOptions = null , $ retryOnLimit = false )
46
49
{
47
50
$ this ->host = $ host ;
48
51
$ this ->headers = $ headers ?: [];
@@ -51,6 +54,8 @@ public function __construct($host, $headers = null, $version = null, $path = nul
51
54
$ this ->curlOptions = $ curlOptions ?: [];
52
55
// These are the supported HTTP verbs
53
56
$ this ->methods = ['delete ' , 'get ' , 'patch ' , 'post ' , 'put ' ];
57
+
58
+ $ this ->retryOnLimit = $ retryOnLimit ;
54
59
}
55
60
56
61
/**
@@ -134,10 +139,11 @@ private function buildUrl($queryParams = null)
134
139
* @param string $url the final url to call
135
140
* @param array $body request body
136
141
* @param array $headers any additional request headers
142
+ * @param bool $retryOnLimit should retry if rate limit is reach?
137
143
*
138
144
* @return Response object
139
145
*/
140
- public function makeRequest ($ method , $ url , $ body = null , $ headers = null )
146
+ public function makeRequest ($ method , $ url , $ body = null , $ headers = null , $ retryOnLimit = false )
141
147
{
142
148
$ curl = curl_init ($ url );
143
149
@@ -169,8 +175,17 @@ public function makeRequest($method, $url, $body = null, $headers = null)
169
175
$ responseHeaders = array_map ('trim ' , $ responseHeaders );
170
176
171
177
curl_close ($ curl );
178
+
179
+ $ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
180
+
181
+ if ($ statusCode == 429 && $ retryOnLimit ) {
182
+ $ headers = $ response ->headers (true );
183
+ $ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
184
+ sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
185
+ return $ this ->makeRequest ($ method , $ url , $ body , $ headers , false );
186
+ }
172
187
173
- return new Response ( $ statusCode , $ responseBody , $ responseHeaders ) ;
188
+ return $ response ;
174
189
}
175
190
176
191
/**
@@ -211,7 +226,8 @@ public function __call($name, $args)
211
226
$ queryParams = isset ($ args [1 ]) ? $ args [1 ] : null ;
212
227
$ url = $ this ->buildUrl ($ queryParams );
213
228
$ headers = isset ($ args [2 ]) ? $ args [2 ] : null ;
214
- return $ this ->makeRequest ($ name , $ url , $ body , $ headers );
229
+ $ retryOnLimit = isset ($ args [3 ]) ? $ args [3 ] : $ this ->retryOnLimit ;
230
+ return $ this ->makeRequest ($ name , $ url , $ body , $ headers , $ retryOnLimit );
215
231
}
216
232
217
233
return $ this ->_ ($ name );
0 commit comments