Skip to content
Open
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
35 changes: 35 additions & 0 deletions src/DataService/DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class DataService
*/
private $clientName = CoreConstants::CLIENT_CURL;

/**
* Optional Request Id to be sent as query parameter on write operations
* @var string|null
*/
private $requestId;


/**
* Initializes a new instance of the DataService class. The old way to construct the dataService. Used by PHP SDK < 3.0.0
Expand Down Expand Up @@ -371,6 +377,19 @@ public function setClientName($clientName){
return $this;
}

/**
* Set the request id which will be appended as query parameter to supported operations
* (Add, Update, Void, SendEmail) for idempotency.
*
* @param string $requestId
* @return $this
*/
public function setRequestId($requestId)
{
$this->requestId = $requestId;
return $this;
}

/**
* New Static function for static Reading from Config or Passing Array
* The config needs to include
Expand Down Expand Up @@ -643,6 +662,7 @@ private function sendRequestParseResponseBodyAndHandleHttpError($entity, $uri, $
case DataService::ADD:
case DataService::VOID:
case DataService::UPDATE:
$uri = $this->appendRequestIdToUri($uri);
$requestParameters = $this->initPostRequest($entity, $uri);
break;
case DataService::FINDBYID:
Expand Down Expand Up @@ -1979,4 +1999,19 @@ private function getIDString($id){
return (String)$id;
}
}

/**
* Appends request id to uri if it is set
* @param string $uri
* @return string
*/
private function appendRequestIdToUri($uri)
{
if (isset($this->requestId) && $this->requestId !== '') {
$delimiter = (strpos($uri, '?') === false) ? '?' : '&';
return $uri . $delimiter . 'requestid=' . urlencode($this->requestId);
}
$this->requestId = null;
return $uri;
}
}