feat(*.php, *.psql): deduplicate API code + performance improvements
This commit is contained in:
parent
cf3dac8a46
commit
4bad005e58
31 changed files with 502 additions and 666 deletions
|
@ -7,46 +7,39 @@ use GuzzleHttp\Client;
|
|||
|
||||
class BookImportHandler extends ApiHandler
|
||||
{
|
||||
protected string $postgrestUrl;
|
||||
protected string $postgrestApiKey;
|
||||
|
||||
private string $bookImportToken;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->ensureCliAccess();
|
||||
$this->loadEnvironment();
|
||||
}
|
||||
|
||||
private function loadEnvironment(): void
|
||||
{
|
||||
$this->postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
||||
$this->postgrestApiKey =
|
||||
$_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
||||
$this->bookImportToken =
|
||||
$_ENV["BOOK_IMPORT_TOKEN"] ?? getenv("BOOK_IMPORT_TOKEN");
|
||||
$this->bookImportToken = $_ENV["BOOK_IMPORT_TOKEN"] ?? getenv("BOOK_IMPORT_TOKEN");
|
||||
}
|
||||
|
||||
public function handleRequest(): void
|
||||
{
|
||||
$input = json_decode(file_get_contents("php://input"), true);
|
||||
|
||||
if (!$input) $this->sendErrorResponse("Invalid or missing JSON body", 400);
|
||||
if (!$input) {
|
||||
$this->sendErrorResponse("Invalid or missing JSON body", 400);
|
||||
}
|
||||
|
||||
$providedToken = $input["token"] ?? null;
|
||||
$isbn = $input["isbn"] ?? null;
|
||||
|
||||
if (!$providedToken || $providedToken !== $this->bookImportToken) $this->sendErrorResponse("Unauthorized access", 401);
|
||||
if ($providedToken !== $this->bookImportToken) {
|
||||
$this->sendErrorResponse("Unauthorized access", 401);
|
||||
}
|
||||
|
||||
if (!$isbn) $this->sendErrorResponse("isbn parameter is required", 400);
|
||||
if (!$isbn) {
|
||||
$this->sendErrorResponse("isbn parameter is required", 400);
|
||||
}
|
||||
|
||||
try {
|
||||
$bookData = $this->fetchBookData($isbn);
|
||||
$this->processBook($bookData);
|
||||
|
||||
$this->sendResponse("Book imported successfully", 200);
|
||||
} catch (Exception $e) {
|
||||
$this->sendResponse(["message" => "Book imported successfully"], 200);
|
||||
} catch (\Exception $e) {
|
||||
$this->sendErrorResponse("Error: " . $e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +59,9 @@ class BookImportHandler extends ApiHandler
|
|||
$data = json_decode($response->getBody(), true);
|
||||
$bookKey = "ISBN:{$isbn}";
|
||||
|
||||
if (empty($data[$bookKey])) throw new Exception("Book data not found for ISBN: {$isbn}");
|
||||
if (empty($data[$bookKey])) {
|
||||
throw new \Exception("Book data not found for ISBN: {$isbn}");
|
||||
}
|
||||
|
||||
return $data[$bookKey];
|
||||
}
|
||||
|
@ -80,11 +75,14 @@ class BookImportHandler extends ApiHandler
|
|||
$author = $bookData["authors"][0]["name"] ?? null;
|
||||
$description = $bookData["description"] ?? ($bookData["notes"] ?? "");
|
||||
|
||||
if (!$isbn || !$title || !$author) throw new Exception("Missing essential book data (title, author, or ISBN).");
|
||||
if (!$isbn || !$title || !$author) {
|
||||
throw new \Exception("Missing essential book data (title, author, or ISBN).");
|
||||
}
|
||||
|
||||
$existingBook = $this->getBookByISBN($isbn);
|
||||
|
||||
if ($existingBook) throw new Exception("Book with ISBN {$isbn} already exists.");
|
||||
if ($existingBook) {
|
||||
throw new \Exception("Book with ISBN {$isbn} already exists.");
|
||||
}
|
||||
|
||||
$bookPayload = [
|
||||
"isbn" => $isbn,
|
||||
|
@ -95,19 +93,12 @@ class BookImportHandler extends ApiHandler
|
|||
"slug" => "/books/" . $isbn,
|
||||
];
|
||||
|
||||
$this->saveBook($bookPayload);
|
||||
}
|
||||
|
||||
private function saveBook(array $bookPayload): void
|
||||
{
|
||||
$this->fetchFromPostgREST("books", "", "POST", $bookPayload);
|
||||
$this->makeRequest("POST", "books", ["json" => $bookPayload]);
|
||||
}
|
||||
|
||||
private function getBookByISBN(string $isbn): ?array
|
||||
{
|
||||
$query = "isbn=eq." . urlencode($isbn);
|
||||
$response = $this->fetchFromPostgREST("books", $query, "GET");
|
||||
|
||||
$response = $this->fetchFromApi("books", "isbn=eq." . urlencode($isbn));
|
||||
return $response[0] ?? null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue