Skip to content

Commit 3609f49

Browse files
authored
Merge pull request #240 from fabio-sassi-spotview/fault_tollerant_req
resend requests that returns http error
2 parents a6b22e4 + 74ec175 commit 3609f49

File tree

3 files changed

+92
-38
lines changed

3 files changed

+92
-38
lines changed

lib/HttpRequestGraphQL.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static function post($url, $data, $httpHeaders = array(), $variables = nu
6868
{
6969
self::prepareRequest($httpHeaders, $data, $variables);
7070

71-
$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);
71+
self::$postDataJSON = self::$postDataGraphQL;
7272

73-
return self::processResponse($response);
73+
return self::processRequest('POST', $url);
7474
}
75-
}
75+
}

lib/HttpRequestJson.php

+89-16
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
*/
2020
class HttpRequestJson
2121
{
22-
2322
/**
2423
* HTTP request headers
2524
*
@@ -32,7 +31,7 @@ class HttpRequestJson
3231
*
3332
* @var string
3433
*/
35-
private static $postDataJSON;
34+
protected static $postDataJSON;
3635

3736

3837
/**
@@ -68,9 +67,7 @@ public static function get($url, $httpHeaders = array())
6867
{
6968
self::prepareRequest($httpHeaders);
7069

71-
$response = CurlRequest::get($url, self::$httpHeaders);
72-
73-
return self::processResponse($response);
70+
return self::processRequest('GET', $url);
7471
}
7572

7673
/**
@@ -86,9 +83,7 @@ public static function post($url, $dataArray, $httpHeaders = array())
8683
{
8784
self::prepareRequest($httpHeaders, $dataArray);
8885

89-
$response = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
90-
91-
return self::processResponse($response);
86+
return self::processRequest('POST', $url);
9287
}
9388

9489
/**
@@ -104,9 +99,7 @@ public static function put($url, $dataArray, $httpHeaders = array())
10499
{
105100
self::prepareRequest($httpHeaders, $dataArray);
106101

107-
$response = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
108-
109-
return self::processResponse($response);
102+
return self::processRequest('PUT', $url);
110103
}
111104

112105
/**
@@ -121,9 +114,68 @@ public static function delete($url, $httpHeaders = array())
121114
{
122115
self::prepareRequest($httpHeaders);
123116

124-
$response = CurlRequest::delete($url, self::$httpHeaders);
117+
return self::processRequest('DELETE', $url);
118+
}
119+
120+
/**
121+
* Process a curl request and return decoded JSON response
122+
*
123+
* @param string $method Request http method ('GET', 'POST', 'PUT' or 'DELETE')
124+
* @param string $url Request URL
125+
*
126+
* @throws CurlException if response received with unexpected HTTP code.
127+
*
128+
* @return array
129+
*/
130+
public static function processRequest($method, $url) {
131+
$retry = 0;
132+
$raw = null;
133+
134+
while(true) {
135+
try {
136+
switch($method) {
137+
case 'GET':
138+
$raw = CurlRequest::get($url, self::$httpHeaders);
139+
break;
140+
case 'POST':
141+
$raw = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
142+
break;
143+
case 'PUT':
144+
$raw = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
145+
break;
146+
case 'DELETE':
147+
$raw = CurlRequest::delete($url, self::$httpHeaders);
148+
break;
149+
default:
150+
throw new \Exception("unexpected request method '$method'");
151+
}
152+
153+
return self::processResponse($raw);
154+
} catch(\Exception $e) {
155+
if (!self::shouldRetry($raw, $e, $retry++)) {
156+
throw $e;
157+
}
158+
}
159+
}
160+
}
161+
162+
/**
163+
* Evaluate if send again a request
164+
*
165+
* @param string $response Raw request response
166+
* @param exception $error the request error occured
167+
* @param integer $retry the current number of retry
168+
*
169+
* @return bool
170+
*/
171+
public static function shouldRetry($response, $error, $retry) {
172+
$config = ShopifySDK::$config;
173+
174+
if (isset($config['RequestRetryCallback'])) {
175+
return $config['RequestRetryCallback']($response, $error, $retry);
176+
}
125177

126-
return self::processResponse($response);
178+
return false;
127179
}
128180

129181
/**
@@ -135,8 +187,29 @@ public static function delete($url, $httpHeaders = array())
135187
*/
136188
protected static function processResponse($response)
137189
{
190+
$responseArray = json_decode($response, true);
138191

139-
return json_decode($response, true);
140-
}
192+
if ($responseArray === null) {
193+
//Something went wrong, Checking HTTP Codes
194+
$httpOK = 200; //Request Successful, OK.
195+
$httpCreated = 201; //Create Successful.
196+
$httpDeleted = 204; //Delete Successful
197+
$httpOther = 303; //See other (headers).
198+
199+
$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
200+
201+
//should be null if any other library used for http calls
202+
$httpCode = CurlRequest::$lastHttpCode;
203+
204+
if ($httpCode == $httpOther && array_key_exists('location', $lastHttpResponseHeaders)) {
205+
return ['location' => $lastHttpResponseHeaders['location']];
206+
}
141207

142-
}
208+
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
209+
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
210+
}
211+
}
212+
213+
return $responseArray;
214+
}
215+
}

lib/ShopifyResource.php

-19
Original file line numberDiff line numberDiff line change
@@ -526,25 +526,6 @@ public function processResponse($responseArray, $dataKey = null)
526526
{
527527
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
528528

529-
if ($responseArray === null) {
530-
//Something went wrong, Checking HTTP Codes
531-
$httpOK = 200; //Request Successful, OK.
532-
$httpCreated = 201; //Create Successful.
533-
$httpDeleted = 204; //Delete Successful
534-
$httpOther = 303; //See other (headers).
535-
536-
//should be null if any other library used for http calls
537-
$httpCode = CurlRequest::$lastHttpCode;
538-
539-
if ($httpCode == $httpOther && array_key_exists('location', self::$lastHttpResponseHeaders)) {
540-
return ['location' => self::$lastHttpResponseHeaders['location']];
541-
}
542-
543-
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
544-
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
545-
}
546-
}
547-
548529
$lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
549530
$this->getLinks($lastResponseHeaders);
550531

0 commit comments

Comments
 (0)