Skip to content

Commit cff16de

Browse files
committed
feat(dataservice): add request id support for write ops (Add/Update/Void/SendEmail)
1 parent 5e4cafc commit cff16de

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/DataService/DataService.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ class DataService
150150
*/
151151
private $clientName = CoreConstants::CLIENT_CURL;
152152

153+
/**
154+
* Optional Request Id to be sent as query parameter on write operations
155+
* @var string|null
156+
*/
157+
private $requestId;
158+
153159

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

380+
/**
381+
* Set the request id which will be appended as query parameter to supported operations
382+
* (Add, Update, Void, SendEmail) for idempotency.
383+
*
384+
* @param string $requestId
385+
* @return $this
386+
*/
387+
public function setRequestId($requestId)
388+
{
389+
$this->requestId = $requestId;
390+
return $this;
391+
}
392+
374393
/**
375394
* New Static function for static Reading from Config or Passing Array
376395
* The config needs to include
@@ -643,6 +662,7 @@ private function sendRequestParseResponseBodyAndHandleHttpError($entity, $uri, $
643662
case DataService::ADD:
644663
case DataService::VOID:
645664
case DataService::UPDATE:
665+
$uri = $this->appendRequestIdToUri($uri);
646666
$requestParameters = $this->initPostRequest($entity, $uri);
647667
break;
648668
case DataService::FINDBYID:
@@ -1979,4 +1999,19 @@ private function getIDString($id){
19791999
return (String)$id;
19802000
}
19812001
}
2002+
2003+
/**
2004+
* Appends request id to uri if it is set
2005+
* @param string $uri
2006+
* @return string
2007+
*/
2008+
private function appendRequestIdToUri($uri)
2009+
{
2010+
if (isset($this->requestId) && $this->requestId !== '') {
2011+
$delimiter = (strpos($uri, '?') === false) ? '?' : '&';
2012+
$uri = $uri . $delimiter . 'requestid=' . urlencode($this->requestId);
2013+
}
2014+
$this->requestId = null;
2015+
return $uri;
2016+
}
19822017
}

0 commit comments

Comments
 (0)