1 | <?php |
||
24 | class HttpClient |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * cURL handle. |
||
29 | * |
||
30 | * @var resource |
||
31 | */ |
||
32 | protected $ch; |
||
33 | |||
34 | /** |
||
35 | * Store API URL. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $url; |
||
40 | |||
41 | /** |
||
42 | * Consumer key. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $consumerKey; |
||
47 | |||
48 | /** |
||
49 | * Consumer secret. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $consumerSecret; |
||
54 | |||
55 | /** |
||
56 | * Client options. |
||
57 | * |
||
58 | * @var Options |
||
59 | */ |
||
60 | protected $options; |
||
61 | |||
62 | /** |
||
63 | * The custom cURL options to use in the requests. |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | private $customCurlOptions = []; |
||
68 | |||
69 | /** |
||
70 | * Request. |
||
71 | * |
||
72 | * @var Request |
||
73 | */ |
||
74 | private $request; |
||
75 | |||
76 | /** |
||
77 | * Response. |
||
78 | * |
||
79 | * @var Response |
||
80 | */ |
||
81 | private $response; |
||
82 | |||
83 | /** |
||
84 | * Response headers. |
||
85 | * |
||
86 | * @var string |
||
87 | */ |
||
88 | private $responseHeaders; |
||
89 | |||
90 | /** |
||
91 | * Initialize HTTP client. |
||
92 | * |
||
93 | * @param string $url Store URL. |
||
94 | * @param string $consumerKey Consumer key. |
||
95 | * @param string $consumerSecret Consumer Secret. |
||
96 | * @param array $options Client options. |
||
97 | */ |
||
98 | public function __construct($url, $consumerKey, $consumerSecret, $options) |
||
109 | |||
110 | /** |
||
111 | * Check if is under SSL. |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | protected function isSsl() |
||
119 | |||
120 | /** |
||
121 | * Build API URL. |
||
122 | * |
||
123 | * @param string $url Store URL. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function buildApiUrl($url) |
||
133 | |||
134 | /** |
||
135 | * Build URL. |
||
136 | * |
||
137 | * @param string $url URL. |
||
138 | * @param array $parameters Query string parameters. |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function buildUrlQuery($url, $parameters = []) |
||
150 | |||
151 | /** |
||
152 | * Authenticate. |
||
153 | * |
||
154 | * @param string $url Request URL. |
||
155 | * @param string $method Request method. |
||
156 | * @param array $parameters Request parameters. |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function authenticate($url, $method, $parameters = []) |
||
187 | |||
188 | /** |
||
189 | * Setup method. |
||
190 | * |
||
191 | * @param string $method Request method. |
||
192 | */ |
||
193 | protected function setupMethod($method) |
||
201 | |||
202 | /** |
||
203 | * Get request headers. |
||
204 | * |
||
205 | * @param bool $sendData If request send data or not. |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | protected function getRequestHeaders($sendData = false) |
||
222 | |||
223 | /** |
||
224 | * Create request. |
||
225 | * |
||
226 | * @param string $endpoint Request endpoint. |
||
227 | * @param string $method Request method. |
||
228 | * @param array $data Request data. |
||
229 | * @param array $parameters Request parameters. |
||
230 | * |
||
231 | * @return Request |
||
232 | */ |
||
233 | protected function createRequest($endpoint, $method, $data = [], $parameters = []) |
||
261 | |||
262 | /** |
||
263 | * Get response headers. |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | protected function getResponseHeaders() |
||
286 | |||
287 | /** |
||
288 | * Create response. |
||
289 | * |
||
290 | * @return Response |
||
291 | */ |
||
292 | protected function createResponse() |
||
312 | |||
313 | /** |
||
314 | * Set default cURL settings. |
||
315 | */ |
||
316 | protected function setDefaultCurlSettings() |
||
339 | |||
340 | /** |
||
341 | * Look for errors in the request. |
||
342 | * |
||
343 | * @param array $parsedResponse Parsed body response. |
||
344 | */ |
||
345 | protected function lookForErrors($parsedResponse) |
||
369 | |||
370 | /** |
||
371 | * Process response. |
||
372 | * |
||
373 | * @return array |
||
374 | */ |
||
375 | protected function processResponse() |
||
401 | |||
402 | /** |
||
403 | * Make requests. |
||
404 | * |
||
405 | * @param string $endpoint Request endpoint. |
||
406 | * @param string $method Request method. |
||
407 | * @param array $data Request data. |
||
408 | * @param array $parameters Request parameters. |
||
409 | * |
||
410 | * @return array |
||
411 | */ |
||
412 | public function request($endpoint, $method, $data = [], $parameters = []) |
||
435 | |||
436 | /** |
||
437 | * Get request data. |
||
438 | * |
||
439 | * @return Request |
||
440 | */ |
||
441 | public function getRequest() |
||
445 | |||
446 | /** |
||
447 | * Get response data. |
||
448 | * |
||
449 | * @return Response |
||
450 | */ |
||
451 | public function getResponse() |
||
455 | |||
456 | /** |
||
457 | * Set custom cURL options to use in requests. |
||
458 | * |
||
459 | * @param array $curlOptions |
||
460 | */ |
||
461 | public function setCustomCurlOptions(array $curlOptions) |
||
465 | } |
||
466 |