From 2a50cabc6baaa53957b4a71aaee34d35cb674041 Mon Sep 17 00:00:00 2001 From: mesilov Date: Sun, 10 Dec 2023 02:14:58 +0600 Subject: [PATCH] Add Request ID to query string parameters The Request ID parameter is now included in query strings in addition to the header field for improved tracking. This change was made to accommodate for the current version of the Bitrix24 API that does not use Request ID from headers. A corresponding `getQueryStringParameterName` method was also added to the `RequestIdGeneratorInterface`. Signed-off-by: mesilov --- src/Core/ApiClient.php | 7 +++--- .../RequestId/DefaultRequestIdGenerator.php | 23 +++++++++++-------- .../RequestId/RequestIdGeneratorInterface.php | 2 ++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Core/ApiClient.php b/src/Core/ApiClient.php index 9f2f6125..821c1172 100644 --- a/src/Core/ApiClient.php +++ b/src/Core/ApiClient.php @@ -84,7 +84,6 @@ public function getCredentials(): Credentials * @return RenewedAccessToken * @throws InvalidArgumentException * @throws TransportExceptionInterface - * @throws \JsonException * @throws TransportException */ public function getNewAccessToken(): RenewedAccessToken @@ -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 ] ) ); @@ -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( diff --git a/src/Infrastructure/HttpClient/RequestId/DefaultRequestIdGenerator.php b/src/Infrastructure/HttpClient/RequestId/DefaultRequestIdGenerator.php index d0332e8b..1fde5608 100644 --- a/src/Infrastructure/HttpClient/RequestId/DefaultRequestIdGenerator.php +++ b/src/Infrastructure/HttpClient/RequestId/DefaultRequestIdGenerator.php @@ -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(); @@ -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; } @@ -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; } } \ No newline at end of file diff --git a/src/Infrastructure/HttpClient/RequestId/RequestIdGeneratorInterface.php b/src/Infrastructure/HttpClient/RequestId/RequestIdGeneratorInterface.php index 60dd9f0f..77ba06b2 100644 --- a/src/Infrastructure/HttpClient/RequestId/RequestIdGeneratorInterface.php +++ b/src/Infrastructure/HttpClient/RequestId/RequestIdGeneratorInterface.php @@ -9,4 +9,6 @@ interface RequestIdGeneratorInterface public function getRequestId(): string; public function getHeaderFieldName(): string; + + public function getQueryStringParameterName():string; } \ No newline at end of file