This repository was archived by the owner on Oct 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAbstractClient.php
More file actions
261 lines (225 loc) · 6.73 KB
/
AbstractClient.php
File metadata and controls
261 lines (225 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
declare(strict_types=1);
namespace PCextreme\Cloudstack;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use GuzzleHttp\Exception\BadResponseException;
use PCextreme\Cloudstack\RequestFactory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use UnexpectedValueException;
abstract class AbstractClient
{
/**
* @var string
*/
const METHOD_GET = 'GET';
/**
* @var string
*/
const METHOD_POST = 'POST';
/**
* @var RequestFactory
*/
protected $requestFactory;
/**
* @var HttpClientInterface
*/
protected $httpClient;
/**
* Constructs a new Cloudstack client instance.
*
* @param array $options
* An array of options to set on this client.
* @param array $collaborators
* An array of collaborators that may be used to override
* this provider's default behavior. Collaborators include
* `requestFactory` and `httpClient`.
*/
public function __construct(array $options = [], array $collaborators = [])
{
if (empty($collaborators['requestFactory'])) {
$collaborators['requestFactory'] = new RequestFactory();
}
$this->setRequestFactory($collaborators['requestFactory']);
if (empty($collaborators['httpClient'])) {
$clientOptions = $this->getAllowedClientOptions($options);
$collaborators['httpClient'] = new HttpClient(
array_intersect_key($options, array_flip($clientOptions))
);
}
$this->setHttpClient($collaborators['httpClient']);
}
/**
* Return the list of options that can be passed to the HttpClient
*
* @param array $options
* @return array
*/
protected function getAllowedClientOptions(array $options) : array
{
$clientOptions = ['timeout', 'proxy'];
// Only allow turning off ssl verification is it's for a proxy
if (! empty($options['proxy'])) {
$clientOptions[] = 'verify';
}
return $clientOptions;
}
/**
* Returns a PSR-7 request instance that is not authenticated.
*
* @param string $method
* @param string $url
* @param array $options
* @return RequestInterface
*/
public function getRequest(string $method, string $url, array $options = []) : RequestInterface
{
return $this->createRequest($method, $url, $options);
}
/**
* Creates a PSR-7 request instance.
*
* @param string $method
* @param string $url
* @param array $options
* @return RequestInterface
*/
protected function createRequest(string $method, string $url, array $options) : RequestInterface
{
$factory = $this->getRequestFactory();
return $factory->getRequestWithOptions($method, $url, $options);
}
/**
* Sends a request instance and returns a response instance.
*
* @param RequestInterface $request
* @return ResponseInterface
*/
protected function sendRequest(RequestInterface $request) : ResponseInterface
{
try {
$response = $this->getHttpClient()->send($request);
} catch (BadResponseException $e) {
$response = $e->getResponse();
}
return $response;
}
/**
* Sends a request and returns the parsed response.
*
* @param RequestInterface $request
* @return mixed
*/
public function getResponse(RequestInterface $request)
{
$response = $this->sendRequest($request);
$parsed = $this->parseResponse($response);
$this->checkResponse($response, $parsed);
return $parsed;
}
/**
* Attempts to parse a JSON response.
*
* @param string $content
* @return array
* @throws UnexpectedValueException
*/
protected function parseJson(string $content) : array
{
$content = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new UnexpectedValueException(sprintf(
"Failed to parse JSON response: %s",
json_last_error_msg()
));
}
return $content;
}
/**
* Returns the content type header of a response.
*
* @param ResponseInterface $response
* @return string
*/
protected function getContentType(ResponseInterface $response) : string
{
return join(';', (array) $response->getHeader('content-type'));
}
/**
* Parses the response according to its content-type header.
*
* @param ResponseInterface $response
* @return mixed
* @throws UnexpectedValueException
*/
protected function parseResponse(ResponseInterface $response)
{
$content = (string) $response->getBody();
$type = $this->getContentType($response);
if (strpos($type, 'urlencoded') !== false) {
parse_str($content, $parsed);
return $parsed;
}
// Attempt to parse the string as JSON regardless of content type,
// since some providers use non-standard content types. Only throw an
// exception if the JSON could not be parsed when it was expected to.
try {
return $this->parseJson($content);
} catch (UnexpectedValueException $e) {
if (strpos($type, 'json') !== false) {
throw $e;
}
return $content;
}
}
/**
* Checks a provider response for errors.
*
* @param ResponseInterface $response
* @param array|string $data
* @return void
* @throws \PCextreme\Cloudstack\Exception\ClientException
*/
abstract protected function checkResponse(ResponseInterface $response, $data);
/**
* Sets the request factory instance.
*
* @param RequestFactory $factory
* @return self
*/
public function setRequestFactory(RequestFactory $factory) : self
{
$this->requestFactory = $factory;
return $this;
}
/**
* Returns the request factory instance.
*
* @return RequestFactory
*/
public function getRequestFactory() : RequestFactory
{
return $this->requestFactory;
}
/**
* Sets the HTTP client instance.
*
* @param HttpClientInterface $client
* @return self
*/
public function setHttpClient(HttpClientInterface $client) : self
{
$this->httpClient = $client;
return $this;
}
/**
* Returns the HTTP client instance.
*
* @return HttpClientInterface
*/
public function getHttpClient() : HttpClientInterface
{
return $this->httpClient;
}
}