Merge pull request #747 from paypal/paypal-request-id-changes

PayPal Request ID changes
This commit is contained in:
Bryant Luk
2016-12-14 13:21:45 -08:00
committed by GitHub
4 changed files with 31 additions and 38 deletions

View File

@@ -18,13 +18,6 @@ class PayPalHttpConnection
*/ */
private $httpConfig; private $httpConfig;
/**
* HTTP status codes for which a retry must be attempted
* retry is currently attempted for Request timeout, Bad Gateway,
* Service Unavailable and Gateway timeout errors.
*/
private static $retryCodes = array('408', '502', '503', '504',);
/** /**
* LoggingManager * LoggingManager
* *
@@ -124,17 +117,6 @@ class PayPalHttpConnection
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
} }
//Retry if Failing
$retries = 0;
if (in_array($httpStatus, self::$retryCodes) && $this->httpConfig->getHttpRetryCount() != null) {
$this->logger->info("Got $httpStatus response from server. Retrying");
do {
$result = curl_exec($ch);
//Retrieve Response Status
$httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
} while (in_array($httpStatus, self::$retryCodes) && (++$retries < $this->httpConfig->getHttpRetryCount()));
}
//Throw Exception if Retries and Certificates doenst work //Throw Exception if Retries and Certificates doenst work
if (curl_errno($ch)) { if (curl_errno($ch)) {
$ex = new PayPalConnectionException( $ex = new PayPalConnectionException(
@@ -168,18 +150,7 @@ class PayPalHttpConnection
curl_close($ch); curl_close($ch);
//More Exceptions based on HttpStatus Code //More Exceptions based on HttpStatus Code
if (in_array($httpStatus, self::$retryCodes)) { if ($httpStatus < 200 || $httpStatus >= 300) {
$ex = new PayPalConnectionException(
$this->httpConfig->getUrl(),
"Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " .
"Retried $retries times."
);
$ex->setData($result);
$this->logger->error("Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}. " .
"Retried $retries times." . $result);
$this->logger->debug("\n\n" . str_repeat('=', 128) . "\n");
throw $ex;
} elseif ($httpStatus < 200 || $httpStatus >= 300) {
$ex = new PayPalConnectionException( $ex = new PayPalConnectionException(
$this->httpConfig->getUrl(), $this->httpConfig->getUrl(),
"Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.", "Got Http response code $httpStatus when accessing {$this->httpConfig->getUrl()}.",

View File

@@ -82,7 +82,7 @@ class RestHandler implements IPayPalHandler
$httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false); $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false);
} }
if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') { if (($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') && !is_null($this->apiContext->getRequestId())) {
$httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId()); $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId());
} }
// Add any additional Headers that they may have provided // Add any additional Headers that they may have provided

View File

@@ -85,17 +85,24 @@ class ApiContext
*/ */
public function getRequestId() public function getRequestId()
{ {
if ($this->requestId == null) {
$this->requestId = $this->generateRequestId();
}
return $this->requestId; return $this->requestId;
} }
/**
* Sets the request ID
*
* @param string $requestId the PayPal-Request-Id value to use
*/
public function setRequestId($requestId)
{
$this->requestId = $requestId;
}
/** /**
* Resets the requestId that can be used to set the PayPal-request-id * Resets the requestId that can be used to set the PayPal-request-id
* header used for idempotency. In cases where you need to make multiple create calls * header used for idempotency. In cases where you need to make multiple create calls
* using the same ApiContext object, you need to reset request Id. * using the same ApiContext object, you need to reset request Id.
* @deprecated Call setRequestId with a unique value.
* *
* @return string * @return string
*/ */
@@ -140,6 +147,7 @@ class ApiContext
* Generates a unique per request id that * Generates a unique per request id that
* can be used to set the PayPal-Request-Id header * can be used to set the PayPal-Request-Id header
* that is used for idempotency * that is used for idempotency
* @deprecated
* *
* @return string * @return string
*/ */

View File

@@ -22,13 +22,27 @@ class ApiContextTest extends PHPUnit_Framework_TestCase
public function testGetRequestId() public function testGetRequestId()
{ {
$requestId = $this->apiContext->getRequestId(); $requestId = $this->apiContext->getRequestId();
$this->assertNotNull($requestId); $this->assertNull($requestId);
$this->assertEquals($requestId, $this->apiContext->getRequestId()); }
public function testSetRequestId()
{
$this->assertNull($this->apiContext->getRequestId());
$expectedRequestId = 'random-value';
$this->apiContext->setRequestId($expectedRequestId);
$requestId = $this->apiContext->getRequestId();
$this->assertEquals($expectedRequestId, $requestId);
} }
public function testResetRequestId() public function testResetRequestId()
{ {
$requestId = $this->apiContext->getRequestId(); $this->assertNull($this->apiContext->getRequestId());
$requestId = $this->apiContext->resetRequestId();
$this->assertNotNull($requestId);
// Tests that another resetRequestId call will generate a new ID
$newRequestId = $this->apiContext->resetRequestId(); $newRequestId = $this->apiContext->resetRequestId();
$this->assertNotNull($newRequestId); $this->assertNotNull($newRequestId);
$this->assertNotEquals($newRequestId, $requestId); $this->assertNotEquals($newRequestId, $requestId);