From d42ae6acb8472e0dd1284f74d5b39a0258d9e73f Mon Sep 17 00:00:00 2001 From: Michael Dennis Date: Tue, 31 Oct 2017 21:34:41 -0700 Subject: [PATCH] Refactor makeRequest method --- lib/Client.php | 66 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/lib/Client.php b/lib/Client.php index 37da48f..8de80f0 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -131,6 +131,51 @@ private function buildUrl($queryParams = null) return sprintf('%s%s%s', $this->host, $this->version ?: '', $path); } + /** + * Prepare response object + * + * @param resource $curl the curl resource + * + * @return Response object + */ + private function prepareResponse($curl) + { + $response = curl_exec($curl); + + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + + $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + + $responseBody = substr($response, $headerSize); + + $responseHeaders = substr($response, 0, $headerSize); + $responseHeaders = explode("\n", $responseHeaders); + $responseHeaders = array_map('trim', $responseHeaders); + + $response = new Response($statusCode, $responseBody, $responseHeaders); + + return $response; + } + + /** + * Retry request + * + * @param array $responseHeaders headers from rate limited response + * @param string $method the HTTP verb + * @param string $url the final url to call + * @param array $body request body + * @param array $headers original headers + * + * @return Response response object + */ + private function retryRequest($responseHeaders, $method, $url, $body, $headers) + { + $sleepDurations = $responseHeaders['X-Ratelimit-Reset'] - time(); + sleep($sleepDurations > 0 ? $sleepDurations : 0); + + return $this->makeRequest($method, $url, $body, $headers, false); + } + /** * Make the API call and return the response. This is separated into * 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 if (isset($headers)) { $this->headers = array_merge($this->headers, $headers); } + if (isset($body)) { $encodedBody = json_encode($body); curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedBody); $this->headers = array_merge($this->headers, ['Content-Type: application/json']); } - curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); - $response = curl_exec($curl); - $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); - $statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); - - $responseBody = substr($response, $headerSize); - $responseHeaders = substr($response, 0, $headerSize); + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers); - $responseHeaders = explode("\n", $responseHeaders); - $responseHeaders = array_map('trim', $responseHeaders); + $response = $this->prepareResponse($curl); curl_close($curl); - - $response = new Response($statusCode, $responseBody, $responseHeaders); - if ($statusCode == 429 && $retryOnLimit) { - $headers = $response->headers(true); - $sleepDurations = $headers['X-Ratelimit-Reset'] - time(); - sleep($sleepDurations > 0 ? $sleepDurations : 0); - return $this->makeRequest($method, $url, $body, $headers, false); + if ($response->statusCode() == 429 && $retryOnLimit) { + return $this->retryRequest($response->headers(true), $method, $url, $body, $headers); } return $response;