1 | <?PHP |
||
26 | class RestClient |
||
27 | { |
||
28 | /** |
||
29 | * Your API key. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | private $apiKey; |
||
34 | |||
35 | /** |
||
36 | * @var HttpClient |
||
37 | */ |
||
38 | protected $httpClient; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $apiHost; |
||
44 | |||
45 | /** |
||
46 | * @var CacheItemPoolInterface |
||
47 | */ |
||
48 | protected $cache; |
||
49 | |||
50 | /** |
||
51 | * @param string $apiKey |
||
52 | * @param string $apiHost |
||
53 | * @param HttpClient $httpClient |
||
54 | * @param CacheItemPoolInterface $cache |
||
55 | */ |
||
56 | public function __construct($apiKey, $apiHost, $httpClient = null, CacheItemPoolInterface $cache = null) |
||
63 | |||
64 | /** |
||
65 | * @return HttpClient |
||
66 | */ |
||
67 | protected function getHttpClient() |
||
81 | |||
82 | /** |
||
83 | * Sends the API request if cache not hit |
||
84 | * |
||
85 | * @param string $method |
||
86 | * @param string $uri |
||
87 | * @param null $body |
||
88 | * @param array $headers |
||
89 | * @param string $cacheKey |
||
90 | * @param integer $ttl |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | public function send($method, $uri, $body = null, array $headers = [], $cacheKey = null, $ttl = null) |
||
128 | |||
129 | /** |
||
130 | * Process the API response, provides error handling |
||
131 | * |
||
132 | * @param ResponseInterface $response |
||
133 | * |
||
134 | * @throws \Exception |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function processResponse(ResponseInterface $response) |
||
166 | |||
167 | /** |
||
168 | * @param string $endpointUrl |
||
169 | * @param array $queryString |
||
170 | * @param string|null $cacheKey |
||
171 | * @param integer|null $ttl |
||
172 | * |
||
173 | * @return ResponseInterface |
||
174 | */ |
||
175 | public function get($endpointUrl, $queryString = [], $cacheKey = null, $ttl = null) |
||
179 | |||
180 | /** |
||
181 | * @param string $endpointUrl |
||
182 | * @param array $postData |
||
183 | * |
||
184 | * @return \stdClass |
||
185 | */ |
||
186 | public function post($endpointUrl, array $postData = []) |
||
208 | |||
209 | /** |
||
210 | * @param $uri |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | private function getApiUrl($uri) |
||
218 | |||
219 | /** |
||
220 | * @param string $apiEndpoint |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | private function generateEndpoint($apiEndpoint) |
||
228 | } |
||
229 |