From b4ec498f32e5732c65ee4a2bd86966f2af504c5a Mon Sep 17 00:00:00 2001 From: Vladimir Kolchin Date: Thu, 6 Apr 2023 11:21:04 +0300 Subject: [PATCH 1/2] add logger and request/response logging for http client --- lib/RetailCrm/ApiClient.php | 11 +++++ lib/RetailCrm/Client/AbstractLoader.php | 11 +++++ lib/RetailCrm/Http/Client.php | 55 +++++++++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/lib/RetailCrm/ApiClient.php b/lib/RetailCrm/ApiClient.php index 4149a5f..b5c7ad5 100644 --- a/lib/RetailCrm/ApiClient.php +++ b/lib/RetailCrm/ApiClient.php @@ -11,6 +11,7 @@ namespace RetailCrm; +use Psr\Log\LoggerInterface; use RetailCrm\Client\ApiVersion3; use RetailCrm\Client\ApiVersion4; use RetailCrm\Client\ApiVersion5; @@ -67,4 +68,14 @@ class ApiClient { return $this->version; } + + /** + * Set logger + * + * @param LoggerInterface|null $logger + */ + public function setLogger($logger = null) + { + $this->request->setLogger($logger); + } } diff --git a/lib/RetailCrm/Client/AbstractLoader.php b/lib/RetailCrm/Client/AbstractLoader.php index d590b8e..03b1568 100755 --- a/lib/RetailCrm/Client/AbstractLoader.php +++ b/lib/RetailCrm/Client/AbstractLoader.php @@ -11,6 +11,7 @@ namespace RetailCrm\Client; +use Psr\Log\LoggerInterface; use RetailCrm\Http\Client; use RetailCrm\Http\RequestOptions; @@ -142,6 +143,16 @@ abstract class AbstractLoader return $this->siteCode; } + /** + * Set logger + * + * @param LoggerInterface|null $logger + */ + public function setLogger($logger) + { + $this->client->setLogger($logger); + } + /** * Getting the list of available api versions * diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index f3f6259..a70ddd0 100755 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -11,6 +11,7 @@ namespace RetailCrm\Http; +use Psr\Log\LoggerInterface; use RetailCrm\Exception\CurlException; use RetailCrm\Exception\InvalidJsonException; use RetailCrm\Exception\LimitException; @@ -34,6 +35,11 @@ class Client protected $defaultParameters; protected $options; + /** + * @var LoggerInterface|null $logger + */ + protected $logger; + /** * Client constructor. * @@ -116,6 +122,8 @@ class Client curl_setopt($curlHandler, CURLOPT_HTTPHEADER, $this->options->getHttpHeaders()); } + $this->logRequest($url, $method, $parameters); + if (self::METHOD_POST === $method) { curl_setopt($curlHandler, CURLOPT_POST, true); @@ -166,6 +174,16 @@ class Client $this->options = $options; } + /** + * Set logger + * + * @param LoggerInterface|null $logger + */ + public function setLogger($logger = null) + { + $this->logger = $logger; + } + /** * @param $curlHandler * @param $method @@ -178,6 +196,8 @@ class Client $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($curlHandler, CURLINFO_CONTENT_TYPE); + $this->logResponse($responseBody, $statusCode); + if (503 === $statusCode) { throw new LimitException("Service temporary unavailable"); } @@ -200,4 +220,39 @@ class Client return [$statusCode, $responseBody]; } + + /** + * @param string $url + * @param string $method + * @param array $params + */ + private function logRequest($url, $method, $params) + { + if (null === $this->logger) { + return; + } + + $message = 'Send request: ' . $method . ' ' . $url; + + if (!empty($params)) { + $message .= ' with params: ' . json_encode($params); + } + + $this->logger->info($message); + } + + /** + * @param string $responseBody + * @param int $statusCode + */ + private function logResponse($responseBody, $statusCode) + { + if (null === $this->logger) { + return; + } + + $message = 'Response with code ' . $statusCode . ' received with body: ' . $responseBody; + + $this->logger->info($message); + } } From 27dab93cf50e93fe231b241be99dcd9ec9330524 Mon Sep 17 00:00:00 2001 From: Vladimir Kolchin Date: Thu, 6 Apr 2023 11:43:05 +0300 Subject: [PATCH 2/2] small change for response logging --- lib/RetailCrm/Http/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/RetailCrm/Http/Client.php b/lib/RetailCrm/Http/Client.php index a70ddd0..4d836a5 100755 --- a/lib/RetailCrm/Http/Client.php +++ b/lib/RetailCrm/Http/Client.php @@ -136,6 +136,8 @@ class Client list($statusCode, $responseBody) = $this->checkResponse($curlHandler, $method); + $this->logResponse($responseBody, $statusCode); + return new ApiResponse($statusCode, $responseBody); } @@ -196,8 +198,6 @@ class Client $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($curlHandler, CURLINFO_CONTENT_TYPE); - $this->logResponse($responseBody, $statusCode); - if (503 === $statusCode) { throw new LimitException("Service temporary unavailable"); }