1 | <?php |
||
21 | class Client |
||
22 | { |
||
23 | const DEFAULT_OPTIONS = [ |
||
24 | 'schema' => 'https', |
||
25 | 'path' => '/', |
||
26 | 'user_agent' => 'WyriHaximus/php-api-client', |
||
27 | 'headers' => [], |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var GuzzleClient |
||
32 | */ |
||
33 | protected $handler; |
||
34 | |||
35 | /** |
||
36 | * @var LoopInterface |
||
37 | */ |
||
38 | protected $loop; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $options = []; |
||
44 | |||
45 | /** |
||
46 | * @var Hydrator |
||
47 | */ |
||
48 | protected $hydrator; |
||
49 | |||
50 | /** |
||
51 | * @var CacheInterface |
||
52 | */ |
||
53 | protected $cache; |
||
54 | |||
55 | /** |
||
56 | * @param LoopInterface $loop |
||
57 | * @param GuzzleClient $handler |
||
58 | * @param array $options |
||
59 | */ |
||
60 | 12 | public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
|
70 | |||
71 | /** |
||
72 | * @param string $path |
||
73 | * @param bool $refresh |
||
74 | * @return PromiseInterface |
||
75 | */ |
||
76 | 5 | public function request(string $path, bool $refresh = false): PromiseInterface |
|
82 | |||
83 | /** |
||
84 | * @param string $path |
||
85 | * @param bool $refresh |
||
86 | * @return PromiseInterface |
||
87 | */ |
||
88 | 5 | public function requestRaw(string $path, bool $refresh = false): PromiseInterface |
|
97 | |||
98 | /** |
||
99 | * @param UriInterface $uri |
||
100 | * @return PromiseInterface |
||
101 | */ |
||
102 | 3 | protected function checkCache(UriInterface $uri): PromiseInterface |
|
122 | |||
123 | /** |
||
124 | * @param RequestInterface $request |
||
125 | * @param ResponseInterface $response |
||
126 | */ |
||
127 | protected function storeCache(RequestInterface $request, ResponseInterface $response) |
||
128 | { |
||
129 | if (!($this->cache instanceof CacheInterface)) { |
||
130 | return; |
||
131 | } |
||
132 | |||
133 | $document = [ |
||
134 | 'body' => $response->getBody()->getContents(), |
||
135 | 'headers' => $response->getHeaders(), |
||
136 | 'protocol_version' => $response->getProtocolVersion(), |
||
137 | 'reason_phrase' => $response->getReasonPhrase(), |
||
138 | 'status_code' => $response->getStatusCode(), |
||
139 | ]; |
||
140 | |||
141 | $key = $this->determineCacheKey($request->getUri()); |
||
142 | |||
143 | $this->cache->set($key, json_encode($document)); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param UriInterface $uri |
||
148 | * @return string |
||
149 | */ |
||
150 | 2 | protected function determineCacheKey(UriInterface $uri): string |
|
165 | |||
166 | /** |
||
167 | * @param string $string |
||
168 | * @return string |
||
169 | */ |
||
170 | 2 | protected function stripExtraSlashes(string $string): string |
|
174 | |||
175 | /** |
||
176 | * @param RequestInterface $request |
||
177 | * @param bool $refresh |
||
178 | * @return PromiseInterface |
||
179 | */ |
||
180 | 5 | public function requestPsr7(RequestInterface $request, bool $refresh = false): PromiseInterface |
|
218 | |||
219 | /** |
||
220 | * @param string $path |
||
221 | * @return RequestInterface |
||
222 | */ |
||
223 | 5 | protected function createRequest(string $path): RequestInterface |
|
229 | |||
230 | /** |
||
231 | * @return array |
||
232 | */ |
||
233 | 5 | public function getHeaders(): array |
|
241 | |||
242 | /** |
||
243 | * @param string $json |
||
244 | * @return PromiseInterface |
||
245 | */ |
||
246 | public function jsonDecode(string $json): PromiseInterface |
||
252 | |||
253 | /** |
||
254 | * @return Hydrator |
||
255 | */ |
||
256 | 1 | public function getHydrator(): Hydrator |
|
260 | |||
261 | /** |
||
262 | * @return LoopInterface |
||
263 | */ |
||
264 | 3 | public function getLoop(): LoopInterface |
|
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | 8 | public function getBaseURL(): string |
|
276 | } |
||
277 |