1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IBM\Watson\Common\Api; |
4
|
|
|
|
5
|
|
|
use Http\Client\HttpClient; |
6
|
|
|
use IBM\Watson\Common\Exception\InsufficientPrivilegesException; |
7
|
|
|
use IBM\Watson\Common\Exception\NotFoundException; |
8
|
|
|
use IBM\Watson\Common\Exception\UnknownErrorException; |
9
|
|
|
use IBM\Watson\Common\Hydrator\HydratorInterface; |
10
|
|
|
use IBM\Watson\Common\Hydrator\NoopHydrator; |
11
|
|
|
use IBM\Watson\Common\RequestBuilder; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use Psr\Http\Message\StreamInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @codeCoverageIgnore |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractApi |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \Http\Client\HttpClient |
22
|
|
|
*/ |
23
|
|
|
protected $httpClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \IBM\Watson\Common\Hydrator\HydratorInterface |
27
|
|
|
*/ |
28
|
|
|
protected $hydrator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \IBM\Watson\Common\RequestBuilder |
32
|
|
|
*/ |
33
|
|
|
protected $requestBuilder; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* AbstractApi constructor. |
37
|
|
|
* |
38
|
|
|
* @param \Http\Client\HttpClient $httpClient |
39
|
|
|
* @param \IBM\Watson\Common\Hydrator\HydratorInterface $hydrator |
40
|
|
|
* @param \IBM\Watson\Common\RequestBuilder $requestBuilder |
41
|
|
|
*/ |
42
|
|
|
public function __construct(HttpClient $httpClient, HydratorInterface $hydrator, RequestBuilder $requestBuilder) |
43
|
|
|
{ |
44
|
|
|
$this->httpClient = $httpClient; |
45
|
|
|
|
46
|
|
|
if (!$hydrator instanceof NoopHydrator) { |
47
|
|
|
$this->hydrator = $hydrator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->requestBuilder = $requestBuilder; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Create and send PSR-7 GET request |
55
|
|
|
* |
56
|
|
|
* @param string $path |
57
|
|
|
* @param array|null $params |
58
|
|
|
* @param array|null $headers |
59
|
|
|
* |
60
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
61
|
|
|
* |
62
|
|
|
* @throws \Http\Client\Exception |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
protected function get($path, array $params = null, array $headers = null) |
66
|
|
|
{ |
67
|
|
|
if (null === $params) { |
68
|
|
|
$params = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (null === $headers) { |
72
|
|
|
$headers = []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (count($params) > 0) { |
76
|
|
|
$path .= '?' . \http_build_query($params); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->httpClient->sendRequest( |
80
|
|
|
$this->requestBuilder->create('GET', $path, $headers) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create and send PSR-7 POST request |
86
|
|
|
* |
87
|
|
|
* @param string $path |
88
|
|
|
* @param array|null $params |
89
|
|
|
* @param array|null $headers |
90
|
|
|
* |
91
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
92
|
|
|
* |
93
|
|
|
* @throws \Http\Client\Exception |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
|
protected function post($path, array $params = null, array $headers = null) |
97
|
|
|
{ |
98
|
|
|
if (null === $headers) { |
99
|
|
|
$headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (null === $params) { |
103
|
|
|
$params = []; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $this->postRaw($path, \http_build_query($params), $headers); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Create and send PSR-7 POST request |
111
|
|
|
* |
112
|
|
|
* @param string $path |
113
|
|
|
* @param resource|string|StreamInterface|null $body |
114
|
|
|
* @param array|null $headers |
115
|
|
|
* |
116
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
117
|
|
|
* |
118
|
|
|
* @throws \Http\Client\Exception |
119
|
|
|
* @throws \Exception |
120
|
|
|
*/ |
121
|
|
|
protected function postRaw($path, $body, array $headers = null) |
122
|
|
|
{ |
123
|
|
|
if (null === $headers) { |
124
|
|
|
$headers = []; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->httpClient->sendRequest( |
128
|
|
|
$this->requestBuilder->create('POST', $path, $headers, $body) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create and set PSR-7 PUT request |
134
|
|
|
* |
135
|
|
|
* @param string $path |
136
|
|
|
* @param array|null $params |
137
|
|
|
* @param array|null $headers |
138
|
|
|
* |
139
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
140
|
|
|
* |
141
|
|
|
* @throws \Http\Client\Exception |
142
|
|
|
* @throws \Exception |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
protected function put($path, array $params = null, array $headers = null) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
if (null === $headers) { |
147
|
|
|
$headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->httpClient->sendRequest( |
151
|
|
|
$this->requestBuilder->create('PUT', $path, $headers, \http_build_query($params)) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create and send PSR-7 PATCH request |
157
|
|
|
* |
158
|
|
|
* @param string $path |
159
|
|
|
* @param array|null $params |
160
|
|
|
* @param array|null $headers |
161
|
|
|
* |
162
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
163
|
|
|
* |
164
|
|
|
* @throws \Http\Client\Exception |
165
|
|
|
* @throws \Exception |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
protected function patch($path, array $params = null, array $headers = null) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
if (null === $headers) { |
170
|
|
|
$headers = ['Content-Type' => 'application/json']; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $this->httpClient->sendRequest( |
174
|
|
|
$this->requestBuilder->create('PATCH', $path, $headers, \json_encode($params)) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Create and send PSR-7 DELETE request |
180
|
|
|
* |
181
|
|
|
* @param string $path |
182
|
|
|
* @param array|null $params |
183
|
|
|
* @param array|null $headers |
184
|
|
|
* |
185
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
186
|
|
|
* |
187
|
|
|
* @throws \Http\Client\Exception |
188
|
|
|
* @throws \Exception |
189
|
|
|
*/ |
190
|
|
View Code Duplication |
protected function delete($path, array $params = null, array $headers = null) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
if (null === $headers) { |
193
|
|
|
$headers = ['Content-Type' => 'application/x-www-form-urlencoded']; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->httpClient->sendRequest( |
197
|
|
|
$this->requestBuilder->create('DELETE', $path, $headers, \http_build_query($params)) |
198
|
|
|
); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Handle API errors |
203
|
|
|
* |
204
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
205
|
|
|
* |
206
|
|
|
* @throws \IBM\Watson\Common\Exception\InsufficientPrivilegesException |
207
|
|
|
* @throws \IBM\Watson\Common\Exception\NotFoundException |
208
|
|
|
* @throws \IBM\Watson\Common\Exception\UnknownErrorException |
209
|
|
|
*/ |
210
|
|
|
protected function handleErrors(ResponseInterface $response) |
211
|
|
|
{ |
212
|
|
|
$body = $response->getBody()->__toString(); |
213
|
|
|
|
214
|
|
|
$content = \json_decode($body, true); |
215
|
|
|
$message = ''; |
216
|
|
|
if (JSON_ERROR_NONE === \json_last_error()) { |
217
|
|
|
$message = $content['error']; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
switch ($response->getStatusCode()) { |
221
|
|
|
case 401: |
222
|
|
|
throw new InsufficientPrivilegesException($message); |
223
|
|
|
break; |
|
|
|
|
224
|
|
|
case 404: |
225
|
|
|
throw new NotFoundException($message); |
226
|
|
|
break; |
|
|
|
|
227
|
|
|
default: |
228
|
|
|
throw new UnknownErrorException($message); |
229
|
|
|
break; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.