1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © 2017 Toan Nguyen. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gojira\Api\Client; |
10
|
|
|
|
11
|
|
|
use Gojira\Api\Authentication\JiraBasicAuthentication; |
12
|
|
|
use Gojira\Framework\Serializer\Serializer; |
13
|
|
|
use Gojira\Api\Exception\ApiException; |
14
|
|
|
use Gojira\Api\Exception\HttpNotFoundException; |
15
|
|
|
use Gojira\Api\Exception\UnauthorizedException; |
16
|
|
|
use Gojira\Api\HandlerStack\GuzzleStack; |
17
|
|
|
use Gojira\Api\Request\HttpMethod; |
18
|
|
|
use Gojira\Api\Request\StatusCodes; |
19
|
|
|
use GuzzleHttp\Client; |
20
|
|
|
use GuzzleHttp\Exception\ClientException; |
21
|
|
|
use GuzzleHttp\Psr7\Request; |
22
|
|
|
use GuzzleHttp\Psr7\Response; |
23
|
|
|
use GuzzleHttp\RequestOptions; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Guzzle HTTP client to work with JIRA REST API |
27
|
|
|
* |
28
|
|
|
* @package Gojira\Api\Client |
29
|
|
|
* @author Toan Nguyen <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class GuzzleClient extends BaseClient implements ClientInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Client |
35
|
|
|
*/ |
36
|
|
|
protected $httpClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $cookiefile; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $crtBundleFile; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $httpStatusCode; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Call the API with an endpoint |
55
|
|
|
* |
56
|
|
|
* @param string $endpoint |
57
|
|
|
* @param array $endpointParameters |
58
|
|
|
* @param null $body |
59
|
|
|
* @param string $method |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function callEndpoint( |
64
|
|
|
$endpoint, |
65
|
|
|
array $endpointParameters = [], |
66
|
|
|
$body = null, |
67
|
|
|
$method = HttpMethod::GET |
68
|
|
|
) { |
69
|
|
|
// Set the endpoint |
70
|
|
|
$this->setEndpoint($endpoint); |
71
|
|
|
// Set the parameters |
72
|
|
|
$this->setEndpointParameters($endpointParameters); |
73
|
|
|
// Set the body |
74
|
|
|
$this->setBody($body); |
75
|
|
|
// Set the HTTP method |
76
|
|
|
$this->setHttpMethod($method); |
77
|
|
|
// Call the endpoint |
78
|
|
|
$this->call(); |
79
|
|
|
|
80
|
|
|
// return the result |
81
|
|
|
return $this->getResult(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Initialize the JIRA REST API |
86
|
|
|
* This method initializes a Guzzle Http client instance and saves it in the private $httpClient variable. |
87
|
|
|
* Other methods can retrieve this Guzzle Http client instance by calling $this->getHttpClient(). |
88
|
|
|
* This method should called only once |
89
|
|
|
* |
90
|
|
|
* @throws ApiException |
91
|
|
|
*/ |
92
|
|
|
public function init() |
93
|
|
|
{ |
94
|
|
|
// Check if the curl object isn't set already |
95
|
|
|
if ($this->isInit()) { |
96
|
|
|
throw new ApiException("A Guzzle Http Client instance is already initialized"); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$defaults = [ |
100
|
|
|
'version' => '1.1' |
101
|
|
|
]; |
102
|
|
|
|
103
|
|
|
// Enable debug if debug is true |
104
|
|
|
if ($this->isDebug()) { |
105
|
|
|
$defaults['debug'] = true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Enable GuzzleStack if use cache is true |
109
|
|
|
if ($this->isUseCache()) { |
110
|
|
|
$defaults['handler'] = GuzzleStack::create(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Set crtBundleFile (certificate) if given else disable SSL verification |
114
|
|
|
if (!empty($this->crtBundleFile)) { |
115
|
|
|
$defaults['verify'] = $this->getCrtBundleFile(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Set cookiefile for sessiondata |
119
|
|
|
if (!empty($this->cookiefile)) { |
120
|
|
|
$defaults['config'] = [ |
121
|
|
|
'curl' => [ |
122
|
|
|
CURLOPT_COOKIEJAR => $this->getCookiefile() |
123
|
|
|
] |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$httpClient = new Client(array_merge_recursive([ |
128
|
|
|
'base_uri' => $this->getBaseUrl() |
129
|
|
|
], $defaults)); |
130
|
|
|
|
131
|
|
|
$this->setHttpClient($httpClient); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Call the API endpoint |
136
|
|
|
* |
137
|
|
|
* @throws \Gojira\Api\Exception\UnauthorizedException |
138
|
|
|
* @throws \Gojira\Api\Exception\HttpNotFoundException |
139
|
|
|
* @throws \Gojira\Api\Exception\HttpException |
140
|
|
|
* @throws \Gojira\Api\Exception\ResultException |
141
|
|
|
*/ |
142
|
|
|
protected function call() |
143
|
|
|
{ |
144
|
|
|
// Check if the curl object is set |
145
|
|
|
if (!$this->isInit()) { |
146
|
|
|
// If it isn't, we do it right now |
147
|
|
|
$this->init(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$options = []; |
151
|
|
|
// Set basic authentication and other headers if enabled |
152
|
|
|
if ($this->authentication instanceof JiraBasicAuthentication) { |
153
|
|
|
$this->setAccept('application/json'); |
154
|
|
|
$this->setContentType('application/json;charset=UTF-8'); |
155
|
|
|
$this->setAuthorization($this->authentication->getCredential()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// Set endpoint parameters if available |
159
|
|
|
foreach ($this->getEndpointParameters() as $key => $value) { |
160
|
|
|
if (!empty($value)) { |
161
|
|
|
$options[RequestOptions::QUERY] = [ |
162
|
|
|
$key => $value |
163
|
|
|
]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Create new PSR-7 request |
168
|
|
|
$request = new Request( |
169
|
|
|
$this->getHttpMethod(), |
170
|
|
|
$this->getEndpoint(), |
171
|
|
|
$this->getHeaders(), |
172
|
|
|
$this->getBody() |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
try { |
176
|
|
|
$response = $this->getHttpClient()->send($request, $options); |
177
|
|
|
} catch (ClientException $e) { |
178
|
|
|
// Check if not found |
179
|
|
|
if ($e->getResponse()->getStatusCode() === StatusCodes::HTTP_NOT_FOUND) { |
180
|
|
|
throw new HttpNotFoundException(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// Check if unauthorized |
184
|
|
|
if ($e->getResponse()->getStatusCode() === StatusCodes::HTTP_UNAUTHORIZED) { |
185
|
|
|
throw new UnauthorizedException(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
throw $e; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// Set body content, status code |
192
|
|
|
$this->processResponse($response); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set HTTP client |
197
|
|
|
* |
198
|
|
|
* @param Client $httpClient |
199
|
|
|
*/ |
200
|
|
|
public function setHttpClient($httpClient) |
201
|
|
|
{ |
202
|
|
|
$this->httpClient = $httpClient; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get HTTP client |
207
|
|
|
* |
208
|
|
|
* @return Client |
209
|
|
|
*/ |
210
|
|
|
public function getHttpClient() |
211
|
|
|
{ |
212
|
|
|
return $this->httpClient; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Set certificate bundle file |
217
|
|
|
* |
218
|
|
|
* @param string $crtBundleFile |
219
|
|
|
*/ |
220
|
|
|
public function setCrtBundleFile($crtBundleFile) |
221
|
|
|
{ |
222
|
|
|
$this->crtBundleFile = $crtBundleFile; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get certificate bundle file |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function getCrtBundleFile() |
231
|
|
|
{ |
232
|
|
|
return $this->crtBundleFile; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Set cookie file |
237
|
|
|
* |
238
|
|
|
* @param string $cookiefile |
239
|
|
|
*/ |
240
|
|
|
public function setCookiefile($cookiefile) |
241
|
|
|
{ |
242
|
|
|
$this->cookiefile = $cookiefile; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Get cookie file |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function getCookiefile() |
251
|
|
|
{ |
252
|
|
|
return $this->cookiefile; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get response HTTP code |
257
|
|
|
* |
258
|
|
|
* @return int |
259
|
|
|
*/ |
260
|
|
|
public function getResultHttpCode() |
261
|
|
|
{ |
262
|
|
|
return $this->httpStatusCode; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Set HTTP status code |
267
|
|
|
* |
268
|
|
|
* @param int $httpStatusCode |
269
|
|
|
*/ |
270
|
|
|
public function setHttpStatusCode($httpStatusCode) |
271
|
|
|
{ |
272
|
|
|
$this->httpStatusCode = $httpStatusCode; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Returns if Curl is initialized or not |
277
|
|
|
* |
278
|
|
|
* @return bool |
279
|
|
|
*/ |
280
|
|
|
protected function isInit() |
281
|
|
|
{ |
282
|
|
|
if ($this->getHttpClient() !== null) { |
283
|
|
|
return true; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return false; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Set body content for current client |
291
|
|
|
* |
292
|
|
|
* @param Response $response PSR-7 response object |
293
|
|
|
* |
294
|
|
|
* @return void |
295
|
|
|
*/ |
296
|
|
|
protected function processResponse(Response $response) |
297
|
|
|
{ |
298
|
|
|
$this->setHttpStatusCode($response->getStatusCode()); |
299
|
|
|
$bodyContents = $response->getBody()->getContents(); |
300
|
|
|
if ($this->getAccept() === 'application/json' && !empty($bodyContents)) { |
301
|
|
|
$bodyContents = Serializer::decode($bodyContents); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$this->setData($bodyContents); |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.