retry prefinery get requests

This commit is contained in:
Alex Grintsvayg 2016-09-07 15:07:28 -04:00
parent 0b6f2d1309
commit e5c31ee9de
2 changed files with 10 additions and 3 deletions

View file

@ -36,6 +36,7 @@ class Curl
'password' => null, 'password' => null,
'cookie' => null, 'cookie' => null,
'json_data' => false, 'json_data' => false,
'retry' => false,
]; ];
$invalid = array_diff_key($options, $defaults); $invalid = array_diff_key($options, $defaults);
@ -63,13 +64,14 @@ class Curl
throw new LogicException('Unable to initialize cURL'); throw new LogicException('Unable to initialize cURL');
} }
$urlWithParams = $url;
if ($method == static::GET && $params) if ($method == static::GET && $params)
{ {
$url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($params); $urlWithParams .= (strpos($urlWithParams, '?') === false ? '?' : '&') . http_build_query($params);
} }
curl_setopt_array($ch, [ curl_setopt_array($ch, [
CURLOPT_URL => $url, CURLOPT_URL => $urlWithParams,
CURLOPT_HTTPHEADER => $options['headers'], CURLOPT_HTTPHEADER => $options['headers'],
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
// CURLOPT_FAILONERROR => true, // CURLOPT_FAILONERROR => true,
@ -141,6 +143,11 @@ class Curl
if (curl_errno($ch)) if (curl_errno($ch))
{ {
if ($options['retry'] && is_numeric($options['retry']) && $options['retry'] > 0)
{
$options['retry'] -= 1;
return static::doCurl($method, $url, $params, $options);
}
throw new CurlException($ch); throw new CurlException($ch);
} }

View file

@ -135,7 +135,7 @@ class Prefinery
{ {
$apiKey = Config::get('prefinery_key'); $apiKey = Config::get('prefinery_key');
return static::decodePrefineryResponse( return static::decodePrefineryResponse(
Curl::get(static::DOMAIN . static::PREFIX . $endpoint . '.json?api_key=' . $apiKey, $data, static::$curlOptions) Curl::get(static::DOMAIN . static::PREFIX . $endpoint . '.json?api_key=' . $apiKey, $data, array_merge(static::$curlOptions, ['retry' => 3]))
); );
} }