1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Component\Http; |
11
|
|
|
|
12
|
|
|
use Chapi\Entity\Http\AuthEntity; |
13
|
|
|
use Chapi\Exception\HttpConnectionException; |
14
|
|
|
use GuzzleHttp\ClientInterface; |
15
|
|
|
use GuzzleHttp\Exception\ClientException; |
16
|
|
|
use GuzzleHttp\Exception\ConnectException; |
17
|
|
|
use GuzzleHttp\Exception\RequestException; |
18
|
|
|
use GuzzleHttp\Exception\ServerException; |
19
|
|
|
use GuzzleHttp\Exception\TooManyRedirectsException; |
20
|
|
|
|
21
|
|
|
class HttpGuzzleClient implements HttpClientInterface |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_CONNECTION_TIMEOUT = 5; |
24
|
|
|
const DEFAULT_TIMEOUT = 30; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ClientInterface |
28
|
|
|
*/ |
29
|
|
|
private $guzzleClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AuthEntity |
33
|
|
|
*/ |
34
|
|
|
private $authEntity; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClientInterface $guzzleClient |
38
|
|
|
* @param AuthEntity $authEntity |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct( |
41
|
|
|
ClientInterface $guzzleClient, |
42
|
|
|
AuthEntity $authEntity |
43
|
|
|
) { |
44
|
7 |
|
$this->guzzleClient = $guzzleClient; |
45
|
7 |
|
$this->authEntity = $authEntity; |
46
|
7 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $url |
50
|
|
|
* @return HttpClientResponseInterface |
51
|
|
|
* @throws HttpConnectionException |
52
|
|
|
*/ |
53
|
3 |
|
public function get($url) |
54
|
|
|
{ |
55
|
3 |
|
$requestOptions = $this->getDefaultRequestOptions(); |
56
|
|
|
|
57
|
|
|
try { |
58
|
3 |
|
$response = $this->guzzleClient->request('GET', $url, $requestOptions); |
59
|
2 |
|
return new HttpGuzzleResponse($response); |
60
|
1 |
|
} catch (ClientException $exception) { // 400 level errors |
61
|
|
|
throw new HttpConnectionException( |
62
|
|
|
sprintf('Client error: Calling %s returned %d', $this->guzzleClient->getConfig('base_uri') . $url, $exception->getCode()), |
63
|
|
|
$exception->getCode(), |
64
|
|
|
$exception |
65
|
|
|
); |
66
|
1 |
|
} catch (ServerException $exception) { // 500 level errors |
67
|
|
|
throw new HttpConnectionException( |
68
|
|
|
sprintf('Server error: Calling %s returned %d', $this->guzzleClient->getConfig('base_uri') . $url, $exception->getCode()), |
69
|
|
|
$exception->getCode(), |
70
|
|
|
$exception |
71
|
|
|
); |
72
|
1 |
|
} catch (TooManyRedirectsException $exception) { // too many redirects to follow |
73
|
|
|
throw new HttpConnectionException( |
74
|
|
|
sprintf('Request to %s failed due to too many redirects', $this->guzzleClient->getConfig('base_uri') . $url), |
75
|
|
|
HttpConnectionException::ERROR_CODE_TOO_MANY_REDIRECT_EXCEPTION, |
76
|
|
|
$exception |
77
|
|
|
); |
78
|
1 |
|
} catch (ConnectException $exception) { // networking error |
79
|
|
|
throw new HttpConnectionException( |
80
|
|
|
sprintf('Cannot connect to %s due to some networking error', $this->guzzleClient->getConfig('base_uri') . $url), |
81
|
|
|
HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION, |
82
|
|
|
$exception |
83
|
|
|
); |
84
|
1 |
|
} catch (RequestException $exception) { // networking error (connection timeout, DNS errors, etc.) |
85
|
|
|
throw new HttpConnectionException( |
86
|
|
|
sprintf('Cannot connect to %s due to networking error', $this->guzzleClient->getConfig('base_uri') . $url), |
87
|
|
|
HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION, |
88
|
|
|
$exception |
89
|
|
|
); |
90
|
1 |
|
} catch (\Exception $exception) { |
91
|
1 |
|
throw new HttpConnectionException( |
92
|
1 |
|
sprintf('Can\'t get response from "%s"', $this->guzzleClient->getConfig('base_uri') . $url), |
93
|
1 |
|
HttpConnectionException::ERROR_CODE_UNKNOWN, |
94
|
|
|
$exception |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $url |
101
|
|
|
* @param mixed $postData |
102
|
|
|
* @return HttpGuzzleResponse |
103
|
|
|
*/ |
104
|
2 |
|
public function postJsonData($url, $postData) |
105
|
|
|
{ |
106
|
2 |
|
return $this->sendJsonDataWithMethod('POST', $url, $postData); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $url |
111
|
|
|
* @param mixed $mPutData |
112
|
|
|
* @return HttpGuzzleResponse |
113
|
|
|
*/ |
114
|
|
|
public function putJsonData($url, $mPutData) |
115
|
|
|
{ |
116
|
|
|
return $this->sendJsonDataWithMethod('PUT', $url, $mPutData); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $url |
122
|
|
|
* @return HttpGuzzleResponse |
123
|
|
|
*/ |
124
|
2 |
|
public function delete($url) |
125
|
|
|
{ |
126
|
2 |
|
$requestOptions = $this->getDefaultRequestOptions(); |
127
|
2 |
|
$response = $this->guzzleClient->request('DELETE', $url, $requestOptions); |
128
|
2 |
|
return new HttpGuzzleResponse($response); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $method |
133
|
|
|
* @param string $url |
134
|
|
|
* @param mixed $data |
135
|
|
|
* @return HttpGuzzleResponse |
136
|
|
|
*/ |
137
|
2 |
|
private function sendJsonDataWithMethod($method, $url, $data) |
138
|
|
|
{ |
139
|
2 |
|
$requestOptions = $this->getDefaultRequestOptions(); |
140
|
2 |
|
$requestOptions['json'] = $data; |
141
|
|
|
|
142
|
2 |
|
$response = $this->guzzleClient->request($method, $url, $requestOptions); |
143
|
|
|
|
144
|
2 |
|
return new HttpGuzzleResponse($response); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns default options for the HTTP request. |
149
|
|
|
* If an username and password is provided, auth |
150
|
|
|
* header will be applied as well. |
151
|
|
|
* |
152
|
|
|
* @return array<string,integer|string> |
153
|
|
|
*/ |
154
|
7 |
|
private function getDefaultRequestOptions() |
155
|
|
|
{ |
156
|
|
|
$requestOptions = [ |
157
|
7 |
|
'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT, |
158
|
7 |
|
'timeout' => self::DEFAULT_TIMEOUT |
159
|
|
|
]; |
160
|
|
|
|
161
|
7 |
|
if (!empty($this->authEntity->username) |
162
|
7 |
|
&& !empty($this->authEntity->password) |
163
|
|
|
) { |
164
|
3 |
|
$requestOptions['auth'] = [ |
165
|
3 |
|
$this->authEntity->username, |
166
|
3 |
|
$this->authEntity->password |
167
|
|
|
]; |
168
|
|
|
} |
169
|
|
|
|
170
|
7 |
|
return $requestOptions; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|