Skip to content
Open
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
15 changes: 15 additions & 0 deletions lib/Error/RequestIdConflict.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Pokepay\Error;

use Pokepay\Error\HttpRequest;

class RequestIdConflict extends HttpRequest
{
public $requestId;

public function __construct($code, $rawResponse, $jsonResponse, $requestId)
{
parent::__construct();
$this->requestId = $requestId;
}
}
1 change: 1 addition & 0 deletions lib/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function request($callId, $method, $url, $headers, $params, $responseClas
if ($this->curlOptions) {
$opts += $this->curlOptions;
}
$opts += array(CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 5);

curl_setopt_array($curl, $opts);

Expand Down
58 changes: 49 additions & 9 deletions lib/PartnerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class PartnerAPI
private $curlOptions = array();

private $acceptLanguage;
private $maxRetries = 2;
private $appName;
private $appVersion;

Expand Down Expand Up @@ -129,16 +130,55 @@ public function send($request)
if ($this->acceptLanguage) {
array_push($headers, 'Accept-Language: ' . $this->acceptLanguage);
}

array_push($headers, 'User-Agent: ' . $this->computeUserAgent());

return $this->clientInstance->request(
$request->getCallId(),
$request->getMethod(),
$this->apiBase . $request->getPath(),
$headers,
$request->getParams() + $request->getDefaultParams(),
$request->responseClass
);
$retry = 0;
while (true) {
try {
return $this->clientInstance->request(
$request->getCallId(),
$request->getMethod(),
$this->apiBase . $request->getPath(),
$headers,
$request->getParams() + $request->getDefaultParams(),
$request->responseClass
);
} catch (Error\ApiConnection $e) {
if (!$request->isRetriable())
{
throw $e;
}

// Retry when timeout
if ($e->errno != CURLE_OPERATION_TIMEDOUT || $retry >= $this->maxRetries)
{
throw $e;
}
} catch (Error\HttpRequest $e) {
if (is_array($e->response)
&& array_key_exists('type', $e->response)
&& $e->response['type'] === 'request_id_conflict')
{
throw new Error\RequestIdConflict(
$e->code,
$e->rawResponse,
$e->response,
$request->getParams()['request_id']
);
}
if (!$request->isRetriable())
{
throw $e;
}
// Retry on 503
if ($e->code != 503 || $retry >= $this->maxRetries)
{
throw $e;
}
sleep(3);
}
++$retry;
$request->setCallId(null); // Re-generate the call ID
}
}
}
11 changes: 10 additions & 1 deletion lib/Request/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getCallId()

public function setCallId($newCallId)
{
$this->callId = $newCallId;
$this->callId = $newCallId || Uuid::uuid4();
}

public function getMethod()
Expand All @@ -51,4 +51,13 @@ public function getParams()
{
return array();
}

public function isRetriable()
{
$params = $this->getParams();
return (
$this->method == 'GET' || $this->method == 'PATCH'
|| (is_array($params) && array_key_exists('request_id', $params))
);
}
}