Skip to content

resend requests that returns http error #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/HttpRequestGraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static function post($url, $data, $httpHeaders = array(), $variables = nu
{
self::prepareRequest($httpHeaders, $data, $variables);

$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);
self::$postDataJSON = self::$postDataGraphQL;

return self::processResponse($response);
return self::processRequest('POST', $url);
}
}
}
105 changes: 89 additions & 16 deletions lib/HttpRequestJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class HttpRequestJson
{

/**
* HTTP request headers
*
Expand All @@ -32,7 +31,7 @@ class HttpRequestJson
*
* @var string
*/
private static $postDataJSON;
protected static $postDataJSON;


/**
Expand Down Expand Up @@ -68,9 +67,7 @@ public static function get($url, $httpHeaders = array())
{
self::prepareRequest($httpHeaders);

$response = CurlRequest::get($url, self::$httpHeaders);

return self::processResponse($response);
return self::processRequest('GET', $url);
}

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

$response = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);

return self::processResponse($response);
return self::processRequest('POST', $url);
}

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

$response = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);

return self::processResponse($response);
return self::processRequest('PUT', $url);
}

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

$response = CurlRequest::delete($url, self::$httpHeaders);
return self::processRequest('DELETE', $url);
}

/**
* Process a curl request and return decoded JSON response
*
* @param string $method Request http method ('GET', 'POST', 'PUT' or 'DELETE')
* @param string $url Request URL
*
* @throws CurlException if response received with unexpected HTTP code.
*
* @return array
*/
public static function processRequest($method, $url) {
$retry = 0;
$raw = null;

while(true) {
try {
switch($method) {
case 'GET':
$raw = CurlRequest::get($url, self::$httpHeaders);
break;
case 'POST':
$raw = CurlRequest::post($url, self::$postDataJSON, self::$httpHeaders);
break;
case 'PUT':
$raw = CurlRequest::put($url, self::$postDataJSON, self::$httpHeaders);
break;
case 'DELETE':
$raw = CurlRequest::delete($url, self::$httpHeaders);
break;
default:
throw new \Exception("unexpected request method '$method'");
}

return self::processResponse($raw);
} catch(\Exception $e) {
if (!self::shouldRetry($raw, $e, $retry++)) {
throw $e;
}
}
}
}

/**
* Evaluate if send again a request
*
* @param string $response Raw request response
* @param exception $error the request error occured
* @param integer $retry the current number of retry
*
* @return bool
*/
public static function shouldRetry($response, $error, $retry) {
$config = ShopifySDK::$config;

if (isset($config['RequestRetryCallback'])) {
return $config['RequestRetryCallback']($response, $error, $retry);
}

return self::processResponse($response);
return false;
}

/**
Expand All @@ -135,8 +187,29 @@ public static function delete($url, $httpHeaders = array())
*/
protected static function processResponse($response)
{
$responseArray = json_decode($response, true);

return json_decode($response, true);
}
if ($responseArray === null) {
//Something went wrong, Checking HTTP Codes
$httpOK = 200; //Request Successful, OK.
$httpCreated = 201; //Create Successful.
$httpDeleted = 204; //Delete Successful
$httpOther = 303; //See other (headers).

$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;

//should be null if any other library used for http calls
$httpCode = CurlRequest::$lastHttpCode;

if ($httpCode == $httpOther && array_key_exists('location', $lastHttpResponseHeaders)) {
return ['location' => $lastHttpResponseHeaders['location']];
}

}
if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
}
}

return $responseArray;
}
}
19 changes: 0 additions & 19 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,25 +526,6 @@ public function processResponse($responseArray, $dataKey = null)
{
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;

if ($responseArray === null) {
//Something went wrong, Checking HTTP Codes
$httpOK = 200; //Request Successful, OK.
$httpCreated = 201; //Create Successful.
$httpDeleted = 204; //Delete Successful
$httpOther = 303; //See other (headers).

//should be null if any other library used for http calls
$httpCode = CurlRequest::$lastHttpCode;

if ($httpCode == $httpOther && array_key_exists('location', self::$lastHttpResponseHeaders)) {
return ['location' => self::$lastHttpResponseHeaders['location']];
}

if ($httpCode != null && $httpCode != $httpOK && $httpCode != $httpCreated && $httpCode != $httpDeleted) {
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
}
}

$lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
$this->getLinks($lastResponseHeaders);

Expand Down