From 4cd422a9937a6ff99b849de531ca60fe7c0635d5 Mon Sep 17 00:00:00 2001 From: zemion Date: Thu, 25 Sep 2025 21:10:11 +0200 Subject: [PATCH] matching API andpoints --- public/api-proxy.php | 53 ++++++++++++++----------------------- public/js/calco2lato.js | 18 ------------- src/.env | 4 +-- src/.env.example | 2 ++ src/Calco2latoApiClient.php | 16 ++--------- 5 files changed, 26 insertions(+), 67 deletions(-) create mode 100644 src/.env.example diff --git a/public/api-proxy.php b/public/api-proxy.php index 7b87d20..06524ca 100644 --- a/public/api-proxy.php +++ b/public/api-proxy.php @@ -2,14 +2,6 @@ declare(strict_types=1); require_once __DIR__ . '/../src/Calco2latoApiClient.php'; -$env = file_get_contents(__DIR__."/../src/.env"); -$lines = explode("\n",$env); - -foreach($lines as $line){ - preg_match("/([^#]+)\=(.*)/",$line,$matches); - if(isset($matches[2])){ putenv(trim($line)); } -} - // --- Basic CORS (adjust origin to your site/domain) --- $origin = $_SERVER['HTTP_ORIGIN'] ?? ''; $allowedOrigin = preg_match('#^https://(www\.)?your-frontend\.example$#', $origin) ? $origin : ''; @@ -43,15 +35,26 @@ if ($bucket['count'] > 120) { // 120 requests/min/IP exit; } -// --- Instantiate API client --- -$base = getenv('CALCO2_API_BASE') ?: 'https://api.calco2la.to'; -$key = getenv('CALCO2_API_KEY') ?: ''; +// --- read .env file with CALCO2LATO_API_BASE and CALCO2LATO_API_KEY +$env = file_get_contents(__DIR__."/../src/.env"); +$lines = explode("\n",$env); + +foreach($lines as $line){ + preg_match("/([^#]+)\=(.*)/",$line,$matches); + if(isset($matches[2])){ putenv(trim($line)); } +} + +// --- Read config --- +$base = getenv('CALCO2LATO_API_BASE') ?: 'https://api.calco2la.to'; +$key = getenv('CALCO2LATO_API_KEY') ?: ''; if (!$key) { http_response_code(500); header('Content-Type: application/json'); echo json_encode(['error' => 'Server misconfiguration: missing API key']); exit; } + +// --- Instantiate API client --- $client = new Calco2latoApiClient($base, $key); // --- Whitelist router --- @@ -65,34 +68,18 @@ try { switch ($endpoint) { case 'airports.search': // GET /?endpoint=airports.search&q=FRA&per_page=10 - $q = $_GET['q'] ?? ''; - $per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20; - $page= isset($_GET['page']) ? (int)$_GET['page'] : 1; - $data = $client->searchAirports($q, $per_page, $page); - echo json_encode($data); - break; - - case 'airports.get': - // GET /?endpoint=airports.get&code=FRA - $code = $_GET['code'] ?? ''; - if ($code === '') throw new InvalidArgumentException('Missing airport code'); - $data = $client->getAirport($code); + $q = $_GET['q'] ?? ''; + $per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20; + $page = isset($_GET['page']) ? (int)$_GET['page'] : 1; + $data = $client->searchAirports($q, $per_page, $page); echo json_encode($data); break; case 'flights.estimate': // POST with JSON body: { endpoint: "flights.estimate", params: {...} } if ($method !== 'POST') throw new RuntimeException('Use POST'); - $params = $input['params'] ?? []; - $data = $client->flightEstimate($params); - echo json_encode($data); - break; - - case 'flights.get': - // GET /?endpoint=flights.get&id=abc123 - $id = $_GET['id'] ?? ''; - if ($id === '') throw new InvalidArgumentException('Missing flight id'); - $data = $client->getFlightById($id); + $params = $input['params'] ?? []; + $data = $client->flightEstimate($params); echo json_encode($data); break; diff --git a/public/js/calco2lato.js b/public/js/calco2lato.js index 6d58234..9ae5299 100644 --- a/public/js/calco2lato.js +++ b/public/js/calco2lato.js @@ -83,15 +83,6 @@ export class Calco2latoClient { return items.map(a => new Airport(a)); } - /** @returns {Promise} */ - async getAirport(code) { - const data = await this._fetchJSON({ - method: 'GET', - query: { endpoint: 'airports.get', code } - }); - return new Airport(data); - } - // ---------- Flights ---------- /** @@ -105,13 +96,4 @@ export class Calco2latoClient { }); return new Flight(data); } - - /** @returns {Promise} */ - async getFlight(id) { - const data = await this._fetchJSON({ - method: 'GET', - query: { endpoint: 'flights.get', id } - }); - return new Flight(data); - } } diff --git a/src/.env b/src/.env index 0834878..6d4b7d6 100644 --- a/src/.env +++ b/src/.env @@ -1,2 +1,2 @@ -CALCO2_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE -CALCO2_API_BASE=https://calco2la.to/ \ No newline at end of file +CALCO2LATO_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE +CALCO2LATO_API_BASE=https://api.calco2la.to/ \ No newline at end of file diff --git a/src/.env.example b/src/.env.example new file mode 100644 index 0000000..0cdaf57 --- /dev/null +++ b/src/.env.example @@ -0,0 +1,2 @@ +CALCO2LATO_API_KEY=... +CALCO2LATO_API_BASE=https://api.calco2la.to/ \ No newline at end of file diff --git a/src/Calco2latoApiClient.php b/src/Calco2latoApiClient.php index bb87c5e..978f28b 100644 --- a/src/Calco2latoApiClient.php +++ b/src/Calco2latoApiClient.php @@ -71,19 +71,13 @@ final class Calco2latoApiClient /** Search airports by free-text (IATA/ICAO/name/city/country) */ public function searchAirports(string $q, int $per_page = 20, int $page = 0): array { - return $this->request('GET', '/api/latest/airports', [ + return $this->request('GET', '/latest/transport/airports', [ 'iata' => $q, 'per_page' => $per_page, 'page' => $page, ]); } - /** Get a single airport by IATA or ICAO code */ - public function getAirport(string $code): array - { - return $this->request('GET', '/api/latest/airports/' . urlencode($code)); - } - // ---------- Flights ---------- /** @@ -92,12 +86,6 @@ final class Calco2latoApiClient */ public function flightEstimate(array $params): array { - return $this->request('POST', '/api/latest/flights/estimate', [], $params); - } - - /** Optional: resolve a route or fetch a published flight record */ - public function getFlightById(string $id): array - { - return $this->request('GET', '/api/latest/flights/' . urlencode($id)); + return $this->request('POST', '/latest/transport/flights', [], $params); } }