Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BA-645-configurable-http-client-timeout #208

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading