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 HttpGuzzlClient implements HttpClientInterface |
22
|
|
|
{ |
23
|
|
|
const DEFAULT_CONNECTION_TIMEOUT = 5; |
24
|
|
|
const DEFAULT_TIMEOUT = 30; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ClientInterface |
28
|
|
|
*/ |
29
|
|
|
private $oGuzzelClient; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var AuthEntity |
33
|
|
|
*/ |
34
|
|
|
private $oAuthEntity; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClientInterface $oGuzzelClient |
38
|
|
|
* @param AuthEntity $oAuthEntity |
39
|
|
|
*/ |
40
|
7 |
|
public function __construct( |
41
|
|
|
ClientInterface $oGuzzelClient, |
42
|
|
|
AuthEntity $oAuthEntity |
43
|
|
|
) |
44
|
|
|
{ |
45
|
7 |
|
$this->oGuzzelClient = $oGuzzelClient; |
46
|
7 |
|
$this->oAuthEntity = $oAuthEntity; |
47
|
7 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $sUrl |
51
|
|
|
* @return HttpClientResponseInterface |
52
|
|
|
* @throws HttpConnectionException |
53
|
|
|
*/ |
54
|
3 |
|
public function get($sUrl) |
55
|
|
|
{ |
56
|
3 |
|
$_aRequestOptions = $this->getDefaultRequestOptions(); |
57
|
|
|
|
58
|
|
|
try |
59
|
|
|
{ |
60
|
3 |
|
$_oResponse = $this->oGuzzelClient->request('GET', $sUrl, $_aRequestOptions); |
61
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
62
|
|
|
} |
63
|
1 |
|
catch (ClientException $oException) // 400 level errors |
64
|
|
|
{ |
65
|
|
|
throw new HttpConnectionException( |
66
|
|
|
sprintf('Client error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()), |
67
|
|
|
$oException->getCode(), |
68
|
|
|
$oException |
69
|
|
|
); |
70
|
|
|
} |
71
|
1 |
|
catch (ServerException $oException) // 500 level errors |
72
|
|
|
{ |
73
|
|
|
throw new HttpConnectionException( |
74
|
|
|
sprintf('Server error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()), |
75
|
|
|
$oException->getCode(), |
76
|
|
|
$oException |
77
|
|
|
); |
78
|
|
|
} |
79
|
1 |
|
catch (TooManyRedirectsException $oException) // too many redirects to follow |
80
|
|
|
{ |
81
|
|
|
throw new HttpConnectionException( |
82
|
|
|
sprintf('Request to %s failed due to too many redirects', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
83
|
|
|
HttpConnectionException::ERROR_CODE_TOO_MANY_REDIRECT_EXCEPTION, |
84
|
|
|
$oException |
85
|
|
|
); |
86
|
|
|
} |
87
|
1 |
|
catch (ConnectException $oException) // networking error |
88
|
|
|
{ |
89
|
|
|
throw new HttpConnectionException( |
90
|
|
|
sprintf('Cannot connect to %s due to some networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
91
|
|
|
HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION, |
92
|
|
|
$oException |
93
|
|
|
); |
94
|
|
|
} |
95
|
1 |
|
catch (RequestException $oException) // networking error (connection timeout, DNS errors, etc.) |
96
|
|
|
{ |
97
|
|
|
throw new HttpConnectionException( |
98
|
|
|
sprintf('Cannot connect to %s due to networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
99
|
|
|
HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION, |
100
|
|
|
$oException |
101
|
|
|
); |
102
|
|
|
} |
103
|
1 |
|
catch (\Exception $oException) |
104
|
|
|
{ |
105
|
1 |
|
throw new HttpConnectionException( |
106
|
1 |
|
sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
107
|
1 |
|
HttpConnectionException::ERROR_CODE_UNKNOWN, |
108
|
|
|
$oException |
109
|
1 |
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $sUrl |
115
|
|
|
* @param mixed $mPostData |
116
|
|
|
* @return HttpGuzzlResponse |
117
|
|
|
*/ |
118
|
2 |
|
public function postJsonData($sUrl, $mPostData) |
119
|
|
|
{ |
120
|
2 |
|
return $this->sendJsonDataWithMethod('POST', $sUrl, $mPostData); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $sUrl |
125
|
|
|
* @param mixed $mPutData |
126
|
|
|
* @return HttpGuzzlResponse |
127
|
|
|
*/ |
128
|
|
|
public function putJsonData($sUrl, $mPutData) |
129
|
|
|
{ |
130
|
|
|
return $this->sendJsonDataWithMethod('PUT', $sUrl, $mPutData); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param string $sUrl |
136
|
|
|
* @return HttpGuzzlResponse |
137
|
|
|
*/ |
138
|
2 |
|
public function delete($sUrl) |
139
|
|
|
{ |
140
|
2 |
|
$_aRequestOptions = $this->getDefaultRequestOptions(); |
141
|
2 |
|
$_oResponse = $this->oGuzzelClient->request('DELETE', $sUrl, $_aRequestOptions); |
142
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $sMethod |
147
|
|
|
* @param $sUrl |
148
|
|
|
* @param $mData |
149
|
|
|
* @return HttpGuzzlResponse |
150
|
|
|
*/ |
151
|
2 |
|
private function sendJsonDataWithMethod($sMethod, $sUrl, $mData) |
152
|
|
|
{ |
153
|
2 |
|
$_aRequestOptions = $this->getDefaultRequestOptions(); |
154
|
2 |
|
$_aRequestOptions['json'] = $mData; |
155
|
|
|
|
156
|
2 |
|
$_oResponse = $this->oGuzzelClient->request($sMethod, $sUrl, $_aRequestOptions); |
157
|
|
|
|
158
|
2 |
|
return new HttpGuzzlResponse($_oResponse); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns default options for the HTTP request. |
163
|
|
|
* If an username and password is provided, auth |
164
|
|
|
* header will be applied as well. |
165
|
|
|
* |
166
|
|
|
* @return array<string,integer|string> |
|
|
|
|
167
|
|
|
*/ |
168
|
7 |
|
private function getDefaultRequestOptions() |
169
|
|
|
{ |
170
|
|
|
$_aRequestOptions = [ |
171
|
7 |
|
'connect_timeout' => self::DEFAULT_CONNECTION_TIMEOUT, |
172
|
|
|
'timeout' => self::DEFAULT_TIMEOUT |
173
|
7 |
|
]; |
174
|
|
|
|
175
|
7 |
|
if (!empty($this->oAuthEntity->username) |
176
|
7 |
|
&& !empty($this->oAuthEntity->password) |
177
|
3 |
|
) |
178
|
7 |
|
{ |
179
|
3 |
|
$_aRequestOptions['auth'] = [ |
180
|
3 |
|
$this->oAuthEntity->username, |
181
|
3 |
|
$this->oAuthEntity->password |
182
|
3 |
|
]; |
183
|
3 |
|
} |
184
|
|
|
|
185
|
7 |
|
return $_aRequestOptions; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.