@@ -131,6 +131,51 @@ private function buildUrl($queryParams = null)
131
131
return sprintf ('%s%s%s ' , $ this ->host , $ this ->version ?: '' , $ path );
132
132
}
133
133
134
+ /**
135
+ * Prepare response object
136
+ *
137
+ * @param resource $curl the curl resource
138
+ *
139
+ * @return Response object
140
+ */
141
+ private function prepareResponse ($ curl )
142
+ {
143
+ $ response = curl_exec ($ curl );
144
+
145
+ $ headerSize = curl_getinfo ($ curl , CURLINFO_HEADER_SIZE );
146
+
147
+ $ statusCode = curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
148
+
149
+ $ responseBody = substr ($ response , $ headerSize );
150
+
151
+ $ responseHeaders = substr ($ response , 0 , $ headerSize );
152
+ $ responseHeaders = explode ("\n" , $ responseHeaders );
153
+ $ responseHeaders = array_map ('trim ' , $ responseHeaders );
154
+
155
+ $ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
156
+
157
+ return $ response ;
158
+ }
159
+
160
+ /**
161
+ * Retry request
162
+ *
163
+ * @param array $responseHeaders headers from rate limited response
164
+ * @param string $method the HTTP verb
165
+ * @param string $url the final url to call
166
+ * @param array $body request body
167
+ * @param array $headers original headers
168
+ *
169
+ * @return Response response object
170
+ */
171
+ private function retryRequest ($ responseHeaders , $ method , $ url , $ body , $ headers )
172
+ {
173
+ $ sleepDurations = $ responseHeaders ['X-Ratelimit-Reset ' ] - time ();
174
+ sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
175
+
176
+ return $ this ->makeRequest ($ method , $ url , $ body , $ headers , false );
177
+ }
178
+
134
179
/**
135
180
* Make the API call and return the response. This is separated into
136
181
* it's own function, so we can mock it easily for testing.
@@ -158,32 +203,21 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
158
203
if (isset ($ headers )) {
159
204
$ this ->headers = array_merge ($ this ->headers , $ headers );
160
205
}
206
+
161
207
if (isset ($ body )) {
162
208
$ encodedBody = json_encode ($ body );
163
209
curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ encodedBody );
164
210
$ this ->headers = array_merge ($ this ->headers , ['Content-Type: application/json ' ]);
165
211
}
166
- curl_setopt ($ curl , CURLOPT_HTTPHEADER , $ this ->headers );
167
212
168
- $ response = curl_exec ($ curl );
169
- $ headerSize = curl_getinfo ($ curl , CURLINFO_HEADER_SIZE );
170
- $ statusCode = curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
171
-
172
- $ responseBody = substr ($ response , $ headerSize );
173
- $ responseHeaders = substr ($ response , 0 , $ headerSize );
213
+ curl_setopt ($ curl , CURLOPT_HTTPHEADER , $ this ->headers );
174
214
175
- $ responseHeaders = explode ("\n" , $ responseHeaders );
176
- $ responseHeaders = array_map ('trim ' , $ responseHeaders );
215
+ $ response = $ this ->prepareResponse ($ curl );
177
216
178
217
curl_close ($ curl );
179
-
180
- $ response = new Response ($ statusCode , $ responseBody , $ responseHeaders );
181
218
182
- if ($ statusCode == 429 && $ retryOnLimit ) {
183
- $ headers = $ response ->headers (true );
184
- $ sleepDurations = $ headers ['X-Ratelimit-Reset ' ] - time ();
185
- sleep ($ sleepDurations > 0 ? $ sleepDurations : 0 );
186
- return $ this ->makeRequest ($ method , $ url , $ body , $ headers , false );
219
+ if ($ response ->statusCode () == 429 && $ retryOnLimit ) {
220
+ return $ this ->retryRequest ($ response ->headers (true ), $ method , $ url , $ body , $ headers );
187
221
}
188
222
189
223
return $ response ;
0 commit comments