129 lines
3.3 KiB
PHP
129 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Classes;
|
|
|
|
require __DIR__ . "/../../vendor/autoload.php";
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
abstract class BaseHandler
|
|
{
|
|
protected string $postgrestUrl;
|
|
protected string $postgrestApiKey;
|
|
protected ?\Redis $cache = null;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->loadEnvironment();
|
|
}
|
|
|
|
private function loadEnvironment(): void
|
|
{
|
|
$this->postgrestUrl =
|
|
$_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL") ?: "";
|
|
$this->postgrestApiKey =
|
|
$_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY") ?: "";
|
|
}
|
|
|
|
protected function makeRequest(
|
|
string $method,
|
|
string $endpoint,
|
|
array $options = []
|
|
): array {
|
|
$client = new Client();
|
|
$url = rtrim($this->postgrestUrl, "/") . "/" . ltrim($endpoint, "/");
|
|
|
|
try {
|
|
$response = $client->request(
|
|
$method,
|
|
$url,
|
|
array_merge($options, [
|
|
"headers" => [
|
|
"Authorization" => "Bearer {$this->postgrestApiKey}",
|
|
"Content-Type" => "application/json",
|
|
],
|
|
])
|
|
);
|
|
|
|
$responseBody = $response->getBody()->getContents();
|
|
|
|
if (empty($responseBody)) return [];
|
|
|
|
$responseData = json_decode($responseBody, true);
|
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) throw new \Exception("Invalid JSON response: {$responseBody}");
|
|
|
|
return $responseData;
|
|
} catch (RequestException $e) {
|
|
$response = $e->getResponse();
|
|
$statusCode = $response ? $response->getStatusCode() : "N/A";
|
|
$responseBody = $response
|
|
? $response->getBody()->getContents()
|
|
: "No response body";
|
|
|
|
throw new \Exception(
|
|
"Request to {$url} failed with status {$statusCode}. Response: {$responseBody}"
|
|
);
|
|
} catch (\Exception $e) {
|
|
throw new \Exception("Request to {$url} failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
protected function sendResponse(array $data, int $statusCode = 200): void
|
|
{
|
|
http_response_code($statusCode);
|
|
header("Content-Type: application/json");
|
|
echo json_encode($data);
|
|
exit();
|
|
}
|
|
|
|
protected function sendErrorResponse(
|
|
string $message,
|
|
int $statusCode = 500
|
|
): void {
|
|
$this->sendResponse(["error" => $message], $statusCode);
|
|
}
|
|
|
|
protected function fetchFromApi(string $endpoint, string $query): array
|
|
{
|
|
$client = new Client();
|
|
$url =
|
|
rtrim($this->postgrestUrl, "/") .
|
|
"/" .
|
|
ltrim($endpoint, "/") .
|
|
"?" .
|
|
$query;
|
|
|
|
try {
|
|
$response = $client->request("GET", $url, [
|
|
"headers" => [
|
|
"Content-Type" => "application/json",
|
|
"Authorization" => "Bearer {$this->postgrestApiKey}",
|
|
],
|
|
]);
|
|
|
|
if ($response->getStatusCode() !== 200) throw new Exception("API call to {$url} failed with status code " . $response->getStatusCode());
|
|
|
|
return json_decode($response->getBody(), true);
|
|
} catch (RequestException $e) {
|
|
throw new Exception("Error fetching from API: " . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
protected function initializeCache(): void
|
|
{
|
|
if (class_exists("Redis")) {
|
|
$redis = new \Redis();
|
|
try {
|
|
$redis->connect("127.0.0.1", 6379);
|
|
$this->cache = $redis;
|
|
} catch (Exception $e) {
|
|
error_log("Redis connection failed: " . $e->getMessage());
|
|
$this->cache = null;
|
|
}
|
|
} else {
|
|
$this->cache = null;
|
|
}
|
|
}
|
|
}
|