1 | <?PHP |
||
25 | class RestClient |
||
26 | { |
||
27 | /** |
||
28 | * Your API key. |
||
29 | * |
||
30 | * @var string |
||
31 | */ |
||
32 | private $apiKey; |
||
33 | |||
34 | /** |
||
35 | * @var HttpClient |
||
36 | */ |
||
37 | protected $httpClient; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | protected $apiHost; |
||
43 | |||
44 | /** |
||
45 | * @var null |
||
46 | */ |
||
47 | protected $cache; |
||
48 | |||
49 | /** |
||
50 | * @param string $apiKey |
||
51 | * @param string $apiHost |
||
52 | * @param HttpClient $httpClient |
||
53 | */ |
||
54 | public function __construct($apiKey, $apiHost, $httpClient = null, $cache = null) |
||
61 | |||
62 | /** |
||
63 | * @return HttpClient |
||
64 | */ |
||
65 | protected function getHttpClient() |
||
79 | |||
80 | /** |
||
81 | * Sends the API request if cache not hit |
||
82 | * |
||
83 | * @param string $method |
||
84 | * @param string $uri |
||
85 | * @param null $body |
||
86 | * @param array $headers |
||
87 | * @param string $cacheKey |
||
88 | * @param integer $ttl |
||
89 | * |
||
90 | * @return array |
||
91 | */ |
||
92 | public function send($method, $uri, $body = null, array $headers = [], $cacheKey = null, $ttl = null) |
||
93 | { |
||
94 | $saveToCache = false; |
||
95 | |||
96 | if ($cacheKey !== null && $ttl !== null && $this->cache) { |
||
97 | if ($this->cache->contains($cacheKey)) { |
||
98 | return $this->cache->fetch($cacheKey); |
||
99 | } else { |
||
100 | $saveToCache = true; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | if (is_array($body)) { |
||
105 | $body = http_build_query($body); |
||
106 | $headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
||
107 | } |
||
108 | |||
109 | $request = MessageFactoryDiscovery::find()->createRequest($method, $this->getApiUrl($uri), $headers, $body); |
||
110 | $rawResponse = $this->getHttpClient()->sendRequest($request); |
||
111 | |||
112 | $data = $this->processResponse($rawResponse); |
||
113 | |||
114 | if ($this->cache && true === $saveToCache) { |
||
115 | $this->cache->save($cacheKey, $data, $ttl); |
||
116 | } |
||
117 | |||
118 | return $data; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Process the API response, provides error handling |
||
123 | * |
||
124 | * @param ResponseInterface $response |
||
125 | * |
||
126 | * @throws \Exception |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function processResponse(ResponseInterface $response) |
||
156 | |||
157 | /** |
||
158 | * @param string $endpointUrl |
||
159 | * @param array $queryString |
||
160 | * @param string|null $cacheKey |
||
161 | * @param integer|null $ttl |
||
162 | * |
||
163 | * @return ResponseInterface |
||
164 | */ |
||
165 | public function get($endpointUrl, $queryString = [], $cacheKey = null, $ttl = null) |
||
169 | |||
170 | /** |
||
171 | * @param string $endpointUrl |
||
172 | * @param array $postData |
||
173 | * |
||
174 | * @return \stdClass |
||
175 | */ |
||
176 | public function post($endpointUrl, array $postData = []) |
||
198 | |||
199 | /** |
||
200 | * @param $uri |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | private function getApiUrl($uri) |
||
208 | |||
209 | /** |
||
210 | * @param string $apiEndpoint |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | private function generateEndpoint($apiEndpoint) |
||
218 | } |
||
219 |