Skip to content

resend requests that returns http errors #234

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

Closed
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
108 changes: 100 additions & 8 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ abstract class ShopifyResource
*/
private $prevLink = null;

/**
* The number of max request retry if a curl http error occured before throw and exception
*
* @var integer
*/
public $maxRequestRetry = 0;


public function __construct($id = null, $parentResourceUrl = '')
{
$this->id = $id;
Expand All @@ -155,6 +163,10 @@ public function __construct($id = null, $parentResourceUrl = '')
$this->httpHeaders['X-Shopify-Api-Features'] = $apiFeature;
}
}

if (isset($config['MaxRequestRetry'])) {
$this->maxRequestRetry = $config['MaxRequestRetry'];
}
}

/**
Expand Down Expand Up @@ -340,7 +352,18 @@ public function get($urlParams = array(), $url = null, $dataKey = null)
{
if (!$url) $url = $this->generateUrl($urlParams);

$response = HttpRequestJson::get($url, $this->httpHeaders);
$n = 0;
while(true) {
try {
$response = HttpRequestJson::get($url, $this->httpHeaders);
$this->checkResponse($response);
break;
} catch(\Exception $e) {
if (!$this->shouldRetry($response, $e, $n++)) {
throw $e;
}
}
}

if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey();

Expand Down Expand Up @@ -414,7 +437,18 @@ public function post($dataArray, $url = null, $wrapData = true)

if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);

$response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
$n = 0;
while(true) {
try {
$response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
$this->checkResponse($response);
break;
} catch(\Exception $e) {
if (!$this->shouldRetry($response, $e, $n++)) {
throw $e;
}
}
}

return $this->processResponse($response, $this->resourceKey);
}
Expand All @@ -440,7 +474,18 @@ public function put($dataArray, $url = null, $wrapData = true)

if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);

$response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
$n = 0;
while(true) {
try {
$response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
$this->checkResponse($response);
break;
} catch(\Exception $e) {
if (!$this->shouldRetry($response, $e, $n++)) {
throw $e;
}
}
}

return $this->processResponse($response, $this->resourceKey);
}
Expand All @@ -462,7 +507,18 @@ public function delete($urlParams = array(), $url = null)
{
if (!$url) $url = $this->generateUrl($urlParams);

$response = HttpRequestJson::delete($url, $this->httpHeaders);
$n = 0;
while(true) {
try {
$response = HttpRequestJson::delete($url, $this->httpHeaders);
$this->checkResponse($response);
break;
} catch(\Exception $e) {
if (!$this->shouldRetry($response, $e, $n++)) {
throw $e;
}
}
}

return $this->processResponse($response);
}
Expand Down Expand Up @@ -511,18 +567,39 @@ protected function castString($array)
return $string;
}

/**
* Evaluate if send again a request
*
* @param array $responseArray Request response in array format
* @param exception $error the request error occured
* @param integer $retry the current number of retry
*
* @return bool
*/
public function shouldRetry($response, $error, $retry) {
if ($retry > $this->maxRequestRetry) {
return false;
}

$config = ShopifySDK::$config;

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

return true;
}

/**
* Process the request response
*
* @param array $responseArray Request response in array format
* @param string $dataKey Keyname to fetch data from response array
*
* @throws ApiException if the response has an error specified
* @throws CurlException if response received with unexpected HTTP code.
*
* @return array
*/
public function processResponse($responseArray, $dataKey = null)
public function checkResponse($responseArray)
{
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;

Expand All @@ -544,6 +621,21 @@ public function processResponse($responseArray, $dataKey = null)
throw new Exception\CurlException("Request failed with HTTP Code $httpCode.", $httpCode);
}
}
}

/**
* Process the request response
*
* @param array $responseArray Request response in array format
* @param string $dataKey Keyname to fetch data from response array
*
* @throws ApiException if the response has an error specified
*
* @return array
*/
public function processResponse($responseArray, $dataKey = null)
{
self::$lastHttpResponseHeaders = CurlRequest::$lastHttpResponseHeaders;

$lastResponseHeaders = CurlRequest::$lastHttpResponseHeaders;
$this->getLinks($lastResponseHeaders);
Expand All @@ -555,7 +647,7 @@ public function processResponse($responseArray, $dataKey = null)
if($message=='account already enabled'){
return array('account_activation_url'=>false);
}

throw new ApiException($message, CurlRequest::$lastHttpCode);
}

Expand Down