From 485562ae520aef4d3c6aaf2cbb8eed35bfd0323b Mon Sep 17 00:00:00 2001 From: Vildan Bina Date: Tue, 17 Dec 2024 15:47:50 +0100 Subject: [PATCH] add configurable timeout for Http Client --- src/Config/Config.php | 32 ++++++++++++++++++- src/Transaction/Client.php | 2 +- .../Request/HttpClient/GuzzleHttpClientV5.php | 11 +++++-- .../Request/HttpClient/GuzzleHttpClientV7.php | 10 +++--- .../Request/HttpClient/HttpClientFactory.php | 8 ++--- 5 files changed, 50 insertions(+), 13 deletions(-) diff --git a/src/Config/Config.php b/src/Config/Config.php index dddfc0c4..a9433b1f 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -99,6 +99,14 @@ abstract class Config implements Loggable * @var Subject */ protected Subject $logger; + /** + * @var int|null + */ + private ?int $timeout; + /** + * @var int|null + */ + private ?int $connectTimeout; /** * @param string $websiteKey @@ -116,6 +124,8 @@ abstract class Config implements Loggable * @param string|null $culture * @param string|null $channel * @param Subject|null $logger + * @param int|null $timeout + * @param int|null $connectTimeout */ public function __construct( string $websiteKey, @@ -132,7 +142,9 @@ public function __construct( ?string $moduleVersion = null, ?string $culture = null, ?string $channel = null, - Subject $logger = null + Subject $logger = null, + ?int $timeout = null, + ?int $connectTimeout = null ) { $this->websiteKey = $websiteKey; $this->secretKey = $secretKey; @@ -149,6 +161,8 @@ public function __construct( $this->moduleVersion = $_ENV['ModuleVersion'] ?? $moduleVersion ?? '1.0.0'; $this->culture = $_ENV['Culture'] ?? $culture ?? ''; $this->channel = $_ENV['Channel'] ?? $channel ?? ''; + $this->timeout = $_ENV['BPE_HTTP_TIMEOUT'] ?? $timeout ?? null; + $this->connectTimeout = $_ENV['BPE_HTTP_CONNECT_TIMEOUT'] ?? $connectTimeout ?? null; $this->setLogger($logger ?? new DefaultLogger()); } @@ -357,4 +371,20 @@ public function getLogger(): ?Subject { return $this->logger; } + + /** + * @return int|null + */ + public function getTimeout(): ?int + { + return $this->timeout; + } + + /** + * @return int|null + */ + public function getConnectTimeout(): ?int + { + return $this->connectTimeout; + } } diff --git a/src/Transaction/Client.php b/src/Transaction/Client.php index a1c4d878..a06f558f 100644 --- a/src/Transaction/Client.php +++ b/src/Transaction/Client.php @@ -61,7 +61,7 @@ class Client public function __construct(?Config $config) { $this->config = $config; - $this->httpClient = HttpClientFactory::createClient($config->getLogger()); + $this->httpClient = HttpClientFactory::createClient($config); } /** diff --git a/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php index acb11dc5..74a97198 100644 --- a/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php @@ -2,6 +2,7 @@ namespace Buckaroo\Transaction\Request\HttpClient; +use Buckaroo\Config\Config; use Buckaroo\Exceptions\BuckarooException; use Buckaroo\Exceptions\TransferException; use Buckaroo\Handlers\Logging\Subject; @@ -18,14 +19,18 @@ class GuzzleHttpClientV5 extends HttpClientAbstract protected Subject $logger; protected ClientInterface $httpClient; - public function __construct(Subject $logger) + /** + * @param Config $config + */ + public function __construct(Config $config) { + $logger = $config->getLogger(); parent::__construct($logger); $this->logger = $logger; $this->httpClient = new Client([ - 'timeout' => self::TIMEOUT, - 'connect_timeout' => self::CONNECT_TIMEOUT, + 'timeout' => (int)($config->getTimeout() ?? self::TIMEOUT), + 'connect_timeout' => (int)($config->getConnectTimeout() ?? self::CONNECT_TIMEOUT), ]); } diff --git a/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php index 015b4e09..2f5ac19e 100644 --- a/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php +++ b/src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php @@ -2,6 +2,7 @@ namespace Buckaroo\Transaction\Request\HttpClient; +use Buckaroo\Config\Config; use Buckaroo\Exceptions\BuckarooException; use Buckaroo\Exceptions\TransferException; use Buckaroo\Handlers\Logging\Subject; @@ -21,17 +22,18 @@ class GuzzleHttpClientV7 extends HttpClientAbstract protected ClientInterface $httpClient; /** - * @param Subject $logger + * @param Config $config */ - public function __construct(Subject $logger) + public function __construct(Config $config) { + $logger = $config->getLogger(); parent::__construct($logger); $this->logger = $logger; $this->httpClient = new Client([ - RequestOptions::TIMEOUT => self::TIMEOUT, - RequestOptions::CONNECT_TIMEOUT => self::CONNECT_TIMEOUT, + RequestOptions::TIMEOUT => (int)($config->getTimeout() ?? self::TIMEOUT), + RequestOptions::CONNECT_TIMEOUT => (int)($config->getConnectTimeout() ?? self::CONNECT_TIMEOUT), ]); } diff --git a/src/Transaction/Request/HttpClient/HttpClientFactory.php b/src/Transaction/Request/HttpClient/HttpClientFactory.php index 1c7a1220..886eb76e 100644 --- a/src/Transaction/Request/HttpClient/HttpClientFactory.php +++ b/src/Transaction/Request/HttpClient/HttpClientFactory.php @@ -2,12 +2,12 @@ namespace Buckaroo\Transaction\Request\HttpClient; -use Buckaroo\Handlers\Logging\Subject; +use Buckaroo\Config\Config; use Composer\InstalledVersions; class HttpClientFactory { - public static function createClient(Subject $logger) + public static function createClient(Config $config) { // Detect the installed GuzzleHttp version $versionString = InstalledVersions::getVersion('guzzlehttp/guzzle'); @@ -16,8 +16,8 @@ public static function createClient(Subject $logger) // Instantiate the appropriate client based on the major version if ($majorVersion === 5) { - return new GuzzleHttpClientV5($logger); + return new GuzzleHttpClientV5($config); } - return new GuzzleHttpClientV7($logger); + return new GuzzleHttpClientV7($config); } }