Skip to content

Commit

Permalink
Merge pull request #357 from mesilov/354-add-x-request-id-support
Browse files Browse the repository at this point in the history
Add Request ID to query string parameters
  • Loading branch information
mesilov authored Dec 9, 2023
2 parents 187ce78 + 2a50cab commit c8e5bc8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/Core/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function getCredentials(): Credentials
* @return RenewedAccessToken
* @throws InvalidArgumentException
* @throws TransportExceptionInterface
* @throws \JsonException
* @throws TransportException
*/
public function getNewAccessToken(): RenewedAccessToken
Expand All @@ -110,6 +109,7 @@ public function getNewAccessToken(): RenewedAccessToken
'client_id' => $this->getCredentials()->getApplicationProfile()->getClientId(),
'client_secret' => $this->getCredentials()->getApplicationProfile()->getClientSecret(),
'refresh_token' => $this->getCredentials()->getAccessToken()->getRefreshToken(),
$this->requestIdGenerator->getQueryStringParameterName() => $requestId
]
)
);
Expand Down Expand Up @@ -170,8 +170,9 @@ public function getResponse(string $apiMethod, array $parameters = []): Response
}
$parameters['auth'] = $this->getCredentials()->getAccessToken()->getAccessToken();
}


// duplicate request id in query string for current version of bitrix24 api
// vendor don't use request id from headers =(
$url .= '?' . $this->requestIdGenerator->getQueryStringParameterName() . '=' . $requestId;
$requestOptions = [
'json' => $parameters,
'headers' => array_merge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

class DefaultRequestIdGenerator implements RequestIdGeneratorInterface
{
private const DEFAULT_REQUEST_ID_FIELD_NAME = 'X-Request-ID';
private const DEFAULT_REQUEST_ID_HEADER_FIELD_NAME = 'X-Request-ID';
private const DEFAULT_QUERY_STRING_PARAMETER_NAME = 'request_id';
private const KEY_NAME_VARIANTS = [
'REQUEST_ID',
'HTTP_X_REQUEST_ID',
'UNIQUE_ID'
];

public function getQueryStringParameterName(): string
{
return self::DEFAULT_QUERY_STRING_PARAMETER_NAME;
}


private function generate(): string
{
return Uuid::v7()->toRfc4122();
Expand All @@ -23,13 +30,11 @@ private function generate(): string
private function findExists(): ?string
{
$candidate = null;
foreach(self::KEY_NAME_VARIANTS as $key)
{
if(!empty($_SERVER[$key]))
{
$candidate = $_SERVER[$key];
break;
}
foreach (self::KEY_NAME_VARIANTS as $key) {
if (!empty($_SERVER[$key])) {
$candidate = $_SERVER[$key];
break;
}
}
return $candidate;
}
Expand All @@ -45,6 +50,6 @@ public function getRequestId(): string

public function getHeaderFieldName(): string
{
return self::DEFAULT_REQUEST_ID_FIELD_NAME;
return self::DEFAULT_REQUEST_ID_HEADER_FIELD_NAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface RequestIdGeneratorInterface
public function getRequestId(): string;

public function getHeaderFieldName(): string;

public function getQueryStringParameterName():string;
}

0 comments on commit c8e5bc8

Please sign in to comment.