Untitled
unknown
plain_text
9 months ago
1.9 kB
5
Indexable
public function request(string $method, string $endpoint, array $options = [], ?array $withheader = [], ?array $cache = null)
{
$data = array();
try {
$cachedData = $method === 'GET' ? $this->requestGetCache($endpoint, $options, $cache) : null;
if ($cachedData === null || !empty($cache['flush'])) {
$response = $this->client->request($method, $endpoint, $options);
$statusCode = $response->getStatusCode();
if ($statusCode === 200 || $statusCode === 201) {
$body = $response->getBody();
if (count($withheader)) {
foreach ($withheader as $header) {
$data[$header] = $response->getHeader($header);
}
$data['data'] = json_decode($body, true, 2048, JSON_THROW_ON_ERROR);
} else {
$data = json_decode($body, true, 2048, JSON_THROW_ON_ERROR);
}
}
if ($method === 'GET') {
$this->requestSaveCache($endpoint, $options, $cache, $data);
} else if (in_array($method, ['PUT', 'PATCH', 'DELETE', 'POST'])) {
$this->getCacheManager()->flushKeys($endpoint, 'cacheapiclient');
}
return $data;
}
return $cachedData;
} catch (\Throwable $e) {
return $this->handlingException($e);
}
}
public function requestGetCache($endpoint, $options, $cache)
{
if ($cache && is_array($cache)) {
if (!empty($options['query'])) {
$key = $cache['cache_key'] ?? $endpoint . http_build_query((array)$options['query'] ?? []);
return $this->getCacheManager()->getCache($key, 'cacheapiclient');
}
}
return null;
}
Editor is loading...
Leave a Comment