Skip to content

Commit

Permalink
add configurable timeout for Http Client
Browse files Browse the repository at this point in the history
  • Loading branch information
vildanbina committed Dec 17, 2024
1 parent be51337 commit 485562a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
32 changes: 31 additions & 1 deletion src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Transaction/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions src/Transaction/Request/HttpClient/GuzzleHttpClientV5.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
]);
}

Expand Down
10 changes: 6 additions & 4 deletions src/Transaction/Request/HttpClient/GuzzleHttpClientV7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
]);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Transaction/Request/HttpClient/HttpClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
}

0 comments on commit 485562a

Please sign in to comment.