From bbef3664c5285c19f9770176817bfaf5015abf7c Mon Sep 17 00:00:00 2001 From: japatel Date: Wed, 12 Nov 2014 09:26:16 -0600 Subject: [PATCH] Adding resetRequestId in ApiContext to enable multiple create calls in succession --- lib/PayPal/Rest/ApiContext.php | 26 ++++++++++++---- tests/PayPal/Test/Rest/ApiContextTest.php | 37 +++++++++++++++++++++++ 2 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 tests/PayPal/Test/Rest/ApiContextTest.php diff --git a/lib/PayPal/Rest/ApiContext.php b/lib/PayPal/Rest/ApiContext.php index c914bf9..a99ee47 100644 --- a/lib/PayPal/Rest/ApiContext.php +++ b/lib/PayPal/Rest/ApiContext.php @@ -32,6 +32,19 @@ class ApiContext */ private $credential; + + /** + * Construct + * + * @param \PayPal\Auth\OAuthTokenCredential $credential + * @param string|null $requestId + */ + public function __construct($credential = null, $requestId = null) + { + $this->requestId = $requestId; + $this->credential = $credential; + } + /** * Get Credential * @@ -60,15 +73,16 @@ class ApiContext } /** - * Construct + * 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 + * using the same ApiContext object, you need to reset request Id. * - * @param \PayPal\Auth\OAuthTokenCredential $credential - * @param string|null $requestId + * @return string */ - public function __construct($credential = null, $requestId = null) + public function resetRequestId() { - $this->requestId = $requestId; - $this->credential = $credential; + $this->requestId = $this->generateRequestId(); + return $this->getrequestId(); } /** diff --git a/tests/PayPal/Test/Rest/ApiContextTest.php b/tests/PayPal/Test/Rest/ApiContextTest.php new file mode 100644 index 0000000..2b9148d --- /dev/null +++ b/tests/PayPal/Test/Rest/ApiContextTest.php @@ -0,0 +1,37 @@ +apiContext = new ApiContext(); + } + + public function testGetRequestId() + { + $requestId = $this->apiContext->getrequestId(); + $this->assertNotNull($requestId); + $this->assertEquals($requestId, $this->apiContext->getrequestId()); + } + + public function testResetRequestId() + { + $requestId = $this->apiContext->getrequestId(); + $newRequestId = $this->apiContext->resetRequestId(); + $this->assertNotNull($newRequestId); + $this->assertNotEquals($newRequestId, $requestId); + } + +}