|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @category Brownie/CartsGuru |
|
4
|
|
|
* @author Brownie <[email protected]> |
|
5
|
|
|
* @license http://www.gnu.org/copyleft/lesser.html |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Brownie\CartsGuru\HTTPClient; |
|
9
|
|
|
|
|
10
|
|
|
use Brownie\CartsGuru\Exception\InvalidCodeException; |
|
11
|
|
|
use Brownie\CartsGuru\Exception\JsonException; |
|
12
|
|
|
use Brownie\CartsGuru\Config; |
|
13
|
|
|
use Brownie\CartsGuru\Model\Base\DataModel; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* HTTP client. |
|
17
|
|
|
*/ |
|
18
|
|
|
class HTTPClient |
|
19
|
|
|
{ |
|
20
|
|
|
const HTTP_METHOD_GET = 'GET'; |
|
21
|
|
|
|
|
22
|
|
|
const HTTP_METHOD_POST = 'POST'; |
|
23
|
|
|
|
|
24
|
|
|
const HTTP_METHOD_PUT = 'PUT'; |
|
25
|
|
|
|
|
26
|
|
|
const HTTP_METHOD_DELETE = 'DELETE'; |
|
27
|
|
|
|
|
28
|
|
|
const HTTP_CODE_200 = 200; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* CartsGuru configuration. |
|
32
|
|
|
* @var Config |
|
33
|
|
|
*/ |
|
34
|
|
|
private $config; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* HTTP client. |
|
38
|
|
|
* @var Client |
|
39
|
|
|
*/ |
|
40
|
|
|
private $client; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Sets incoming data. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Client $client HTTP client. |
|
46
|
|
|
* @param Config $config CartsGuru configuration. |
|
47
|
|
|
*/ |
|
48
|
8 |
|
public function __construct(Client $client, Config $config) |
|
49
|
|
|
{ |
|
50
|
|
|
$this |
|
51
|
8 |
|
->setClient($client) |
|
52
|
8 |
|
->setConfig($config); |
|
53
|
8 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Sets the request client. |
|
57
|
|
|
* Returns the current object. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Client $client |
|
60
|
|
|
* |
|
61
|
|
|
* @return self |
|
62
|
|
|
*/ |
|
63
|
8 |
|
private function setClient(Client $client) |
|
64
|
|
|
{ |
|
65
|
8 |
|
$this->client = $client; |
|
66
|
8 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets the CartsGuru configuration. |
|
71
|
|
|
* Returns the current object. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Config $config CartsGuru configuration. |
|
74
|
|
|
* |
|
75
|
|
|
* @return self |
|
76
|
|
|
*/ |
|
77
|
8 |
|
private function setConfig(Config $config) |
|
78
|
|
|
{ |
|
79
|
8 |
|
$this->config = $config; |
|
80
|
8 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Returns request client. |
|
85
|
|
|
* |
|
86
|
|
|
* @return Client |
|
87
|
|
|
*/ |
|
88
|
5 |
|
private function getClient() |
|
89
|
|
|
{ |
|
90
|
5 |
|
return $this->client; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns CartsGuru configuration. |
|
95
|
|
|
* |
|
96
|
|
|
* @return Config |
|
97
|
|
|
*/ |
|
98
|
5 |
|
private function getConfig() |
|
99
|
|
|
{ |
|
100
|
5 |
|
return $this->config; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Performs a network request in CartsGuru. |
|
105
|
|
|
* Returns the response from CartsGuru. |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $checkHTTPCode Checked HTTP Code |
|
108
|
|
|
* @param string $endpoint The access endpoint to the resource. |
|
109
|
|
|
* @param array|DataModel $data An array of data to send. |
|
110
|
|
|
* @param string $method Query Method. |
|
111
|
|
|
* @param boolean $ignoreEmptyResponse Semaphore of ignoring an empty response. |
|
112
|
|
|
* |
|
113
|
|
|
* @throws InvalidCodeException |
|
114
|
|
|
* @throws JsonException |
|
115
|
|
|
* |
|
116
|
|
|
* @return array |
|
117
|
|
|
*/ |
|
118
|
5 |
|
public function request( |
|
119
|
|
|
$checkHTTPCode, |
|
120
|
|
|
$endpoint, |
|
121
|
|
|
$data = array(), |
|
122
|
|
|
$method = self::HTTP_METHOD_GET, |
|
123
|
|
|
$ignoreEmptyResponse = false |
|
124
|
|
|
) { |
|
125
|
5 |
|
$apiUrl = $this->getApiUrl($endpoint); |
|
126
|
|
|
|
|
127
|
5 |
|
if (is_object($data)) { |
|
128
|
2 |
|
$data = $data->toArray(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
5 |
|
$query = new Query(); |
|
132
|
|
|
$query |
|
133
|
5 |
|
->setApiUrl($apiUrl) |
|
134
|
5 |
|
->setXAuthKey($this->getConfig()->getApiAuthKey()) |
|
135
|
5 |
|
->setData($data) |
|
136
|
5 |
|
->setMethod($method) |
|
137
|
5 |
|
->setTimeOut($this->getConfig()->getTimeOut()); |
|
138
|
|
|
|
|
139
|
|
|
$response = $this |
|
140
|
5 |
|
->getClient() |
|
141
|
5 |
|
->httpRequest($query); |
|
142
|
|
|
|
|
143
|
5 |
|
$responseBody = $this->parseResponse($ignoreEmptyResponse, $response->getBody()); |
|
144
|
|
|
|
|
145
|
4 |
|
$this->checkingHTTPCode( |
|
146
|
4 |
|
$checkHTTPCode, |
|
147
|
4 |
|
$response->getHttpCode(), |
|
148
|
4 |
|
is_array($responseBody) && isset($responseBody['error']) ? ', ' . $responseBody['error'] : '' |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
|
|
return array( |
|
152
|
3 |
|
'response' => $responseBody, |
|
153
|
3 |
|
'runtime' => $response->getRuntime(), |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns the text of the JSON parsing error. |
|
159
|
|
|
* |
|
160
|
|
|
* @param integer $errorId Error ID |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function getJsonLastErrorMsg($errorId) |
|
165
|
|
|
{ |
|
166
|
2 |
|
$message = 'Unknown error'; |
|
167
|
|
|
|
|
168
|
|
|
$errors = array( |
|
169
|
2 |
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
|
170
|
2 |
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', |
|
171
|
2 |
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', |
|
172
|
2 |
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
|
173
|
2 |
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' |
|
174
|
|
|
); |
|
175
|
|
|
|
|
176
|
2 |
|
if (!empty($errors[$errorId])) { |
|
177
|
2 |
|
$message = $errors[$errorId]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
2 |
|
return $message; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Creates a complete URL to the resource. |
|
185
|
|
|
* Returns the created URL. |
|
186
|
|
|
* |
|
187
|
|
|
* @param string $endpoint API Endpoint. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string |
|
190
|
|
|
*/ |
|
191
|
5 |
|
private function getApiUrl($endpoint) |
|
192
|
|
|
{ |
|
193
|
5 |
|
return implode( |
|
194
|
5 |
|
'/', |
|
195
|
|
|
array( |
|
196
|
5 |
|
$this->getConfig()->getApiUrl(), |
|
197
|
5 |
|
$endpoint |
|
198
|
|
|
) |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Checking HTTP Code. |
|
204
|
|
|
* |
|
205
|
|
|
* @param int $checkHTTPCode Verification HTTP code. |
|
206
|
|
|
* @param int $httpCode Verifiable HTTP code. |
|
207
|
|
|
* @param string $message Error message. |
|
208
|
|
|
* |
|
209
|
|
|
* @throws InvalidCodeException |
|
210
|
|
|
*/ |
|
211
|
4 |
|
private function checkingHTTPCode($checkHTTPCode, $httpCode, $message) |
|
212
|
|
|
{ |
|
213
|
4 |
|
if ($checkHTTPCode != $httpCode) { |
|
214
|
1 |
|
throw new InvalidCodeException($httpCode . ($message)); |
|
215
|
|
|
} |
|
216
|
3 |
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Parsing with the API response. |
|
220
|
|
|
* |
|
221
|
|
|
* @param bool $ignoreEmptyResponse A sign of ignoring an empty response. |
|
222
|
|
|
* @param string $responseBody HTTP response from the API. |
|
223
|
|
|
* |
|
224
|
|
|
* @return array |
|
225
|
|
|
* |
|
226
|
|
|
* @throws JsonException |
|
227
|
|
|
*/ |
|
228
|
5 |
|
private function parseResponse($ignoreEmptyResponse, $responseBody) |
|
229
|
|
|
{ |
|
230
|
5 |
|
$response = array(); |
|
231
|
5 |
|
if (!$ignoreEmptyResponse && !empty($responseBody)) { |
|
232
|
4 |
|
$response = json_decode($responseBody, true); |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Parse Json checking. |
|
236
|
|
|
*/ |
|
237
|
4 |
|
if (json_last_error() != JSON_ERROR_NONE) { |
|
238
|
1 |
|
throw new JsonException($this->getJsonLastErrorMsg(json_last_error())); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
4 |
|
return $response; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|