coryd.dev/api/Classes/PageFetcher.php

29 lines
772 B
PHP

<?php
namespace App\Classes;
use App\Classes\BaseHandler;
abstract class PageFetcher extends BaseHandler
{
protected function cacheGet(string $key): mixed
{
return $this->cache && $this->cache->exists($key) ? json_decode($this->cache->get($key), true) : null;
}
protected function cacheSet(string $key, mixed $value, int $ttl = 3600): void
{
if ($this->cache) $this->cache->setex($key, $ttl, json_encode($value));
}
protected function fetchSingleFromApi(string $endpoint, string $url): ?array
{
$data = $this->fetchFromApi($endpoint, "url=eq./{$url}");
return $data[0] ?? null;
}
protected function fetchPostRpc(string $endpoint, array $body): ?array
{
return $this->makeRequest("POST", $endpoint, ['json' => $body]);
}
}