1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Sdk\Http\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use GuzzleHttp\Exception\ClientException; |
7
|
|
|
use GuzzleHttp\Exception\ConnectException; |
8
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
9
|
|
|
use GuzzleHttp\Exception\ServerException; |
10
|
|
|
use Ipag\Sdk\Exception\HttpClientException; |
11
|
|
|
use Ipag\Sdk\Exception\HttpServerException; |
12
|
|
|
use Ipag\Sdk\Exception\HttpTransferException; |
13
|
|
|
use Ipag\Sdk\Http\Response; |
14
|
|
|
use Psr\Http\Message\StreamInterface; |
15
|
|
|
use RuntimeException; |
16
|
|
|
|
17
|
|
|
class GuzzleHttpClient extends BaseHttpClient |
18
|
|
|
{ |
19
|
|
|
private const DEFAULT_USER_AGENT = 'IPag SDK for PHP'; |
20
|
|
|
|
21
|
|
|
protected Client $client; |
22
|
|
|
|
23
|
|
|
protected ?array $headers = null; |
24
|
|
|
|
25
|
|
|
protected ?int $statusCode = null; |
26
|
|
|
|
27
|
|
|
public function __construct(array $config = []) |
28
|
|
|
{ |
29
|
|
|
static $default = [ |
30
|
|
|
'allow_redirects' => false, |
31
|
|
|
'timeout' => 60.00, |
32
|
|
|
'connect_timeout' => 10.00, |
33
|
|
|
'http_errors' => true, |
34
|
|
|
'headers' => [ |
35
|
|
|
'User-Agent' => self::DEFAULT_USER_AGENT |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
$this->client = new Client(array_merge($default, $config)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Uses the current http client wrapper to do a request. |
44
|
|
|
* |
45
|
|
|
* @throws RuntimeException |
46
|
|
|
* @throws HttpTransferException |
47
|
|
|
* @throws HttpClientException |
48
|
|
|
* @throws HttpServerException |
49
|
|
|
* |
50
|
|
|
* @param string $method |
51
|
|
|
* @param string $url |
52
|
|
|
* @param string|null $body |
53
|
|
|
* @param array $query |
54
|
|
|
* @param array $header |
55
|
|
|
* @return string|null |
56
|
|
|
*/ |
57
|
|
|
public function request(string $method, string $url, ?string $body, array $query = [], array $header = []): ?string |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$response = $this->client->request($method, $url, [ |
61
|
|
|
'headers' => $header, |
62
|
|
|
'query' => $query, |
63
|
|
|
'body' => $body, |
64
|
|
|
]); |
65
|
|
|
|
66
|
|
|
$this->headers = $response->getHeaders(); |
67
|
|
|
$this->statusCode = $response->getStatusCode(); |
68
|
|
|
|
69
|
|
|
return $response->getBody()->getContents(); |
70
|
|
|
} catch (ConnectException $e) { |
71
|
|
|
// Networking error |
72
|
|
|
throw new HttpTransferException("An networking error ocurred while trying to connect to `$url`", $e->getCode(), $e); |
73
|
|
|
} catch (ServerException $e) { |
74
|
|
|
// Server-side error 5xx |
75
|
|
|
throw new HttpServerException( |
76
|
|
|
"An server-side error ocurred with status {$e->getResponse()->getStatusCode()} from `$url`", |
77
|
|
|
$e->getCode(), |
78
|
|
|
$e, |
79
|
|
|
// If this fails, an RuntimeException will be thrown |
80
|
|
|
Response::from($e->getResponse()->getBody()->getContents(), $e->getResponse()->getHeaders(), $e->getResponse()->getStatusCode()), |
81
|
|
|
$e->getResponse()->getStatusCode(), |
82
|
|
|
$e->getResponse()->getReasonPhrase() |
83
|
|
|
); |
84
|
|
|
} catch (ClientException $e) { |
85
|
|
|
// Client-side error 4xx |
86
|
|
|
throw new HttpClientException( |
87
|
|
|
"An client-side error ocurred with status {$e->getResponse()->getStatusCode()} from `$url`", |
88
|
|
|
$e->getCode(), |
89
|
|
|
$e, |
90
|
|
|
// If this fails, an RuntimeException will be thrown |
91
|
|
|
Response::from($e->getResponse()->getBody()->getContents(), $e->getResponse()->getHeaders(), $e->getResponse()->getStatusCode()), |
92
|
|
|
$e->getResponse()->getStatusCode(), |
93
|
|
|
$e->getResponse()->getReasonPhrase() |
94
|
|
|
); |
95
|
|
|
} catch (RuntimeException $e) { |
96
|
|
|
// Stream error |
97
|
|
|
throw $e; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function lastResponseHeaders(): ?array |
102
|
|
|
{ |
103
|
|
|
return $this->headers; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function lastResponseStatusCode(): ?int |
107
|
|
|
{ |
108
|
|
|
return $this->statusCode; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |