-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from buckaroo-it/add-support-for-guzzlehttp-v5
BP-3290 Add support for guzzlehttp v5
- Loading branch information
Showing
4 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Buckaroo\Transaction\Request\HttpClient; | ||
|
||
use Buckaroo\Exceptions\BuckarooException; | ||
use Buckaroo\Exceptions\TransferException; | ||
use Buckaroo\Handlers\Logging\Subject; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use GuzzleHttp\Exception\RequestException; | ||
|
||
class GuzzleHttpClientV5 extends HttpClientAbstract | ||
{ | ||
/** | ||
* @var Subject | ||
*/ | ||
protected Subject $logger; | ||
protected ClientInterface $httpClient; | ||
|
||
public function __construct(Subject $logger) | ||
{ | ||
parent::__construct($logger); | ||
$this->logger = $logger; | ||
|
||
$this->httpClient = new Client([ | ||
'timeout' => self::TIMEOUT, | ||
'connect_timeout' => self::CONNECT_TIMEOUT, | ||
]); | ||
} | ||
|
||
/** | ||
* @param string $url | ||
* @param array $headers | ||
* @param string $method | ||
* @param string|null $data | ||
* @return array|mixed | ||
* @throws TransferException | ||
* @throws BuckarooException|GuzzleException | ||
*/ | ||
|
||
public function call(string $url, array $headers, string $method, string $data = null) | ||
{ | ||
$headers = $this->convertHeadersFormat($headers); | ||
|
||
$request = $this->httpClient->createRequest($method, $url, [ | ||
'headers' => $headers, | ||
'body' => $data, | ||
]); | ||
|
||
try | ||
{ | ||
$response = $this->httpClient->send($request); | ||
|
||
$result = (string) $response->getBody(); | ||
|
||
$this->logger->info('RESPONSE HEADERS: ' . json_encode($response->getHeaders())); | ||
$this->logger->info('RESPONSE BODY: ' . $response->getBody()); | ||
} catch (RequestException $e) { | ||
throw new TransferException($this->logger, "Transfer failed", 0, $e); | ||
} | ||
|
||
$result = $this->getDecodedResult($response, $result); | ||
|
||
return [ | ||
$response, | ||
$result, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Buckaroo\Transaction\Request\HttpClient; | ||
|
||
use Buckaroo\Handlers\Logging\Subject; | ||
use Composer\InstalledVersions; | ||
|
||
class HttpClientFactory | ||
{ | ||
public static function createClient(Subject $logger) | ||
{ | ||
// Detect the installed GuzzleHttp version | ||
$versionString = InstalledVersions::getVersion('guzzlehttp/guzzle'); | ||
// Extract the major version number | ||
$majorVersion = (int) explode('.', $versionString)[0]; | ||
|
||
// Instantiate the appropriate client based on the major version | ||
if ($majorVersion === 5) { | ||
return new GuzzleHttpClientV5($logger); | ||
} | ||
return new GuzzleHttpClientV7($logger); | ||
} | ||
} |