matching API andpoints

This commit is contained in:
2025-09-25 21:10:11 +02:00
parent 048a1f06d6
commit 4cd422a993
5 changed files with 26 additions and 67 deletions

View File

@@ -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;

View File

@@ -83,15 +83,6 @@ export class Calco2latoClient {
return items.map(a => new Airport(a));
}
/** @returns {Promise<Airport>} */
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<Flight>} */
async getFlight(id) {
const data = await this._fetchJSON({
method: 'GET',
query: { endpoint: 'flights.get', id }
});
return new Flight(data);
}
}

View File

@@ -1,2 +1,2 @@
CALCO2_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE
CALCO2_API_BASE=https://calco2la.to/
CALCO2LATO_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE
CALCO2LATO_API_BASE=https://api.calco2la.to/

2
src/.env.example Normal file
View File

@@ -0,0 +1,2 @@
CALCO2LATO_API_KEY=...
CALCO2LATO_API_BASE=https://api.calco2la.to/

View File

@@ -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);
}
}