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) |
||
92 | { |
||
93 | if (!\function_exists('curl_version')) { |
||
94 | throw new HttpClientException('cURL is NOT installed on this server', -1, new Request(), new Response()); |
||
95 | } |
||
96 | |||
97 | $this->options = new Options($options); |
||
98 | $this->url = $this->buildApiUrl($url); |
||
99 | $this->consumerKey = $consumerKey; |
||
100 | $this->consumerSecret = $consumerSecret; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Check if is under SSL. |
||
105 | * |
||
106 | * @return bool |
||
107 | */ |
||
108 | protected function isSsl() |
||
109 | { |
||
110 | return 'https://' === \substr($this->url, 0, 8); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Build API URL. |
||
115 | * |
||
116 | * @param string $url Store URL. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | protected function buildApiUrl($url) |
||
121 | { |
||
122 | $api = $this->options->isWPAPI() ? $this->options->apiPrefix() : '/wc-api/'; |
||
123 | |||
124 | return \rtrim($url, '/') . $api . $this->options->getVersion() . '/'; |
||
125 | } |
||
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() |
||
251 | |||
252 | /** |
||
253 | * Create response. |
||
254 | * |
||
255 | * @return Response |
||
256 | */ |
||
257 | protected function createResponse() |
||
277 | |||
278 | /** |
||
279 | * Set default cURL settings. |
||
280 | */ |
||
281 | protected function setDefaultCurlSettings() |
||
296 | |||
297 | /** |
||
298 | * Look for errors in the request. |
||
299 | * |
||
300 | * @param array $parsedResponse Parsed body response. |
||
301 | */ |
||
302 | protected function lookForErrors($parsedResponse) |
||
319 | |||
320 | /** |
||
321 | * Process response. |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | protected function processResponse() |
||
339 | |||
340 | /** |
||
341 | * Make requests. |
||
342 | * |
||
343 | * @param string $endpoint Request endpoint. |
||
344 | * @param string $method Request method. |
||
345 | * @param array $data Request data. |
||
346 | * @param array $parameters Request parameters. |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | public function request($endpoint, $method, $data = [], $parameters = []) |
||
373 | |||
374 | /** |
||
375 | * Get request data. |
||
376 | * |
||
377 | * @return Request |
||
378 | */ |
||
379 | public function getRequest() |
||
383 | |||
384 | /** |
||
385 | * Get response data. |
||
386 | * |
||
387 | * @return Response |
||
388 | */ |
||
389 | public function getResponse() |
||
393 | } |
||
394 |