1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\ApiClient\Api; |
4
|
|
|
|
5
|
|
|
use Happyr\ApiClient\Exception\Domain as DomainExceptions; |
6
|
|
|
use Happyr\ApiClient\Exception\DomainException; |
7
|
|
|
use Happyr\ApiClient\Hydrator\Hydrator; |
8
|
|
|
use Happyr\ApiClient\Hydrator\NoopHydrator; |
9
|
|
|
use Http\Client\HttpClient; |
10
|
|
|
use Http\Message\RequestFactory; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Tobias Nyholm <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class HttpApi |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var HttpClient |
20
|
|
|
*/ |
21
|
|
|
protected $httpClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Hydrator |
25
|
|
|
*/ |
26
|
|
|
protected $hydrator; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var RequestFactory |
30
|
|
|
*/ |
31
|
|
|
protected $requestFactory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param HttpClient $httpClient |
35
|
|
|
* @param RequestFactory $requestFactory |
36
|
|
|
* @param Hydrator $hydrator |
37
|
|
|
*/ |
38
|
|
|
public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestFactory $requestFactory) |
39
|
|
|
{ |
40
|
|
|
$this->httpClient = $httpClient; |
41
|
|
|
$this->requestFactory = $requestFactory; |
42
|
|
|
if (!$hydrator instanceof NoopHydrator) { |
43
|
|
|
$this->hydrator = $hydrator; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ResponseInterface $response |
49
|
|
|
* @param string $class |
50
|
|
|
* |
51
|
|
|
* @throws \Exception |
52
|
|
|
* |
53
|
|
|
* @return mixed|ResponseInterface |
54
|
|
|
*/ |
55
|
|
|
protected function hydrateResponse(ResponseInterface $response, $class) |
56
|
|
|
{ |
57
|
|
|
if (!$this->hydrator) { |
58
|
|
|
return $response; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($response->getStatusCode() === 204) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) { |
66
|
|
|
$this->handleErrors($response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $this->hydrator->hydrate($response, $class); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Send a GET request with query parameters. |
74
|
|
|
* |
75
|
|
|
* @param string $path Request path |
76
|
|
|
* @param array $params GET parameters |
77
|
|
|
* @param array $requestHeaders Request Headers |
78
|
|
|
* |
79
|
|
|
* @return ResponseInterface |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
protected function httpGet($path, array $params = [], array $requestHeaders = []) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
if (count($params) > 0) { |
84
|
|
|
$path .= '?'.http_build_query($params); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->httpClient->sendRequest( |
88
|
|
|
$this->requestFactory->createRequest('GET', $path, $requestHeaders) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Send a POST request with JSON-encoded parameters. |
94
|
|
|
* |
95
|
|
|
* @param string $path Request path |
96
|
|
|
* @param array $params POST parameters to be JSON encoded |
97
|
|
|
* @param array $requestHeaders Request headers |
98
|
|
|
* |
99
|
|
|
* @return ResponseInterface |
100
|
|
|
*/ |
101
|
|
|
protected function httpPost($path, array $params = [], array $requestHeaders = []) |
102
|
|
|
{ |
103
|
|
|
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
104
|
|
|
|
105
|
|
|
return $this->httpPostRaw($path, http_build_query($params), $requestHeaders); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Send a POST request with raw data. |
110
|
|
|
* |
111
|
|
|
* @param string $path Request path |
112
|
|
|
* @param array|string $body Request body |
113
|
|
|
* @param array $requestHeaders Request headers |
114
|
|
|
* |
115
|
|
|
* @return ResponseInterface |
116
|
|
|
*/ |
117
|
|
|
protected function httpPostRaw($path, $body, array $requestHeaders = []) |
118
|
|
|
{ |
119
|
|
|
return $response = $this->httpClient->sendRequest( |
|
|
|
|
120
|
|
|
$this->requestFactory->createRequest('POST', $path, $requestHeaders, $body) |
|
|
|
|
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Send a PUT request with JSON-encoded parameters. |
126
|
|
|
* |
127
|
|
|
* @param string $path Request path |
128
|
|
|
* @param array $params POST parameters to be JSON encoded |
129
|
|
|
* @param array $requestHeaders Request headers |
130
|
|
|
* |
131
|
|
|
* @return ResponseInterface |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
protected function httpPut($path, array $params = [], array $requestHeaders = []) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
136
|
|
|
|
137
|
|
|
return $this->httpClient->sendRequest( |
138
|
|
|
$this->requestFactory->createRequest('PUT', $path, $requestHeaders, http_build_query($params)) |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Send a DELETE request with JSON-encoded parameters. |
144
|
|
|
* |
145
|
|
|
* @param string $path Request path |
146
|
|
|
* @param array $params POST parameters to be JSON encoded |
147
|
|
|
* @param array $requestHeaders Request headers |
148
|
|
|
* |
149
|
|
|
* @return ResponseInterface |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
protected function httpDelete($path, array $params = [], array $requestHeaders = []) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; |
154
|
|
|
|
155
|
|
|
return $this->httpClient->sendRequest( |
156
|
|
|
$this->requestFactory->createRequest('DELETE', $path, $requestHeaders, http_build_query($params)) |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Handle HTTP errors. |
162
|
|
|
* |
163
|
|
|
* Call is controlled by the specific API methods. |
164
|
|
|
* |
165
|
|
|
* @param ResponseInterface $response |
166
|
|
|
* |
167
|
|
|
* @throws DomainException |
168
|
|
|
*/ |
169
|
|
|
protected function handleErrors(ResponseInterface $response) |
170
|
|
|
{ |
171
|
|
|
$statusCode = $response->getStatusCode(); |
172
|
|
|
switch ($statusCode) { |
173
|
|
|
case 400: |
174
|
|
|
throw DomainExceptions\HttpClientException::badRequest($response); |
175
|
|
|
case 401: |
176
|
|
|
throw DomainExceptions\HttpClientException::unauthorized($response); |
177
|
|
|
case 402: |
178
|
|
|
throw DomainExceptions\HttpClientException::requestFailed($response); |
179
|
|
|
case 404: |
180
|
|
|
throw DomainExceptions\HttpClientException::notFound($response); |
181
|
|
|
case 404 < $statusCode && $statusCode < 500: |
182
|
|
|
throw new DomainExceptions\HttpClientException('Client error', $statusCode, $response); |
183
|
|
|
case 500 <= $statusCode: |
184
|
|
|
throw DomainExceptions\HttpServerException::serverError($statusCode); |
185
|
|
|
default: |
186
|
|
|
throw new DomainExceptions\UnknownErrorException('Failed to hydrate response from API.', $statusCode); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
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.