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 | * Request. |
||
64 | * |
||
65 | * @var Request |
||
66 | */ |
||
67 | private $request; |
||
68 | |||
69 | /** |
||
70 | * Response. |
||
71 | * |
||
72 | * @var Response |
||
73 | */ |
||
74 | private $response; |
||
75 | |||
76 | /** |
||
77 | * Response headers. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $responseHeaders; |
||
82 | |||
83 | /** |
||
84 | * Initialize HTTP client. |
||
85 | * |
||
86 | * @param string $url Store URL. |
||
87 | * @param string $consumerKey Consumer key. |
||
88 | * @param string $consumerSecret Consumer Secret. |
||
89 | * @param array $options Client options. |
||
90 | */ |
||
91 | public function __construct($url, $consumerKey, $consumerSecret, $options) |
||
102 | |||
103 | /** |
||
104 | * Check if is under SSL. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | protected function isSsl() |
||
112 | |||
113 | /** |
||
114 | * Build API URL. |
||
115 | * |
||
116 | * @param string $url Store URL. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function buildApiUrl($url) |
||
126 | |||
127 | /** |
||
128 | * Build URL. |
||
129 | * |
||
130 | * @param string $url URL. |
||
131 | * @param array $parameters Query string parameters. |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function buildUrlQuery($url, $parameters = []) |
||
143 | |||
144 | /** |
||
145 | * Authenticate. |
||
146 | * |
||
147 | * @param string $url Request URL. |
||
148 | * @param string $method Request method. |
||
149 | * @param array $parameters Request parameters. |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | protected function authenticate($url, $method, $parameters = []) |
||
166 | |||
167 | /** |
||
168 | * Setup method. |
||
169 | * |
||
170 | * @param string $method Request method. |
||
171 | */ |
||
172 | protected function setupMethod($method) |
||
180 | |||
181 | /** |
||
182 | * Get request headers. |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | protected function getRequestHeaders() |
||
194 | |||
195 | /** |
||
196 | * Create request. |
||
197 | * |
||
198 | * @param string $endpoint Request endpoint. |
||
199 | * @param string $method Request method. |
||
200 | * @param array $data Request data. |
||
201 | * @param array $parameters Request parameters. |
||
202 | * |
||
203 | * @return Request |
||
204 | */ |
||
205 | protected function createRequest($endpoint, $method, $data = [], $parameters = []) |
||
226 | |||
227 | /** |
||
228 | * Get response headers. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | protected function getResponseHeaders() |
||
255 | |||
256 | /** |
||
257 | * Create response. |
||
258 | * |
||
259 | * @return Response |
||
260 | */ |
||
261 | protected function createResponse() |
||
281 | |||
282 | /** |
||
283 | * Set default cURL settings. |
||
284 | */ |
||
285 | protected function setDefaultCurlSettings() |
||
300 | |||
301 | /** |
||
302 | * Look for errors in the request. |
||
303 | * |
||
304 | * @param array $parsedResponse Parsed body response. |
||
305 | */ |
||
306 | protected function lookForErrors($parsedResponse) |
||
323 | |||
324 | /** |
||
325 | * Process response. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | protected function processResponse() |
||
343 | |||
344 | /** |
||
345 | * Make requests. |
||
346 | * |
||
347 | * @param string $endpoint Request endpoint. |
||
348 | * @param string $method Request method. |
||
349 | * @param array $data Request data. |
||
350 | * @param array $parameters Request parameters. |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | public function request($endpoint, $method, $data = [], $parameters = []) |
||
377 | |||
378 | /** |
||
379 | * Get request data. |
||
380 | * |
||
381 | * @return Request |
||
382 | */ |
||
383 | public function getRequest() |
||
387 | |||
388 | /** |
||
389 | * Get response data. |
||
390 | * |
||
391 | * @return Response |
||
392 | */ |
||
393 | public function getResponse() |
||
397 | } |
||
398 |