Minor changes to structure

This commit is contained in:
2025-10-02 09:45:00 +02:00
parent 90e958af74
commit 89f16f0f62
4 changed files with 118 additions and 66 deletions

View File

@@ -2,13 +2,12 @@
declare(strict_types=1);
require_once __DIR__ . '/../src/Calco2latoApiClient.php';
// --- Basic CORS (adjust origin to your site/domain) ---
// --- CORS (restrict to your site) ---
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
$allowedOrigin = preg_match('#^https://(www\.)?your-frontend\.example$#', $origin) ? $origin : '';
if ($allowedOrigin) {
header('Access-Control-Allow-Origin: ' . $allowedOrigin);
if (preg_match('#^https://(www\.)?your-frontend\.example$#', $origin)) {
header('Access-Control-Allow-Origin: ' . $origin);
header('Vary: Origin');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Credentials', 'true');
}
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
@@ -55,7 +54,9 @@ if (!$key) {
}
// --- Instantiate API client ---
$client = new Calco2latoApiClient($base, $key);
// Version from query (?ver=v1|test|latest), default latest
$ver = $_GET['ver'] ?? $_POST['ver'] ?? Calco2latoApiVersion::LATEST;
$client = new Calco2latoApiClient($base, $key, (string)$ver);
// --- Whitelist router ---
$input = json_decode(file_get_contents('php://input') ?: '[]', true) ?: [];
@@ -66,21 +67,32 @@ header('Content-Type: application/json; charset=utf-8');
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);
case 'health':
echo json_encode(['ok'=>true,'ver'=>$ver]);
break;
case 'airports.search':
if ($method === 'GET') {
$q = [
'page' => isset($_GET['page']) ? (int)$_GET['page'] : null,
'per_page' => isset($_GET['per_page']) ? (int)$_GET['per_page'] : null,
'sort_by' => $_GET['sort_by'] ?? null,
'order' => $_GET['order'] ?? null,
'iata' => $_GET['iata'] ?? null,
];
echo json_encode($client->airports_get($q));
break;
} elseif ($method === 'POST') {
$params = $input['params'] ?? [];
echo json_encode($client->airports_post($params));
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);
echo json_encode($client->flight_post($params));
break;
default:

View File

@@ -19,7 +19,7 @@
<div id="wrapper">
<h1>Search for airports</h1>
<form id="airport-search-form">
<input type="text" id="q" name="q" />
<input type="text" id="iata" name="iata" />
<button type="submit" name="Los" title="Los">Search ...</button>
</form>
<div id="airport-results">
@@ -55,8 +55,8 @@
e.preventDefault();
list.innerHTML = 'Loading…';
try {
const q = new FormData(searchform).get('q');
const airports = await api.searchAirports(q, 10, 1);
const iata = new FormData(searchform).get('iata');
const airports = await api.searchAirports(iata, 10, 1);
list.innerHTML = airports.map(a => `<li>${a.display}</li>`).join('');
} catch (err) {
list.innerHTML = `<li style="color:red">${err.message}</li>`;
@@ -72,7 +72,7 @@
try {
const f = new FormData(flightform);
const flight = await api.estimateFlight({"flights": [{"departure": f.get('departure'), "arrival": f.get('arrival'), "passengerCount": 1, "travelClass": f.get('cabinclass'), "departureDate": f.get('departuredate')}]});
flightresult.innerHTML = JSON.stringify(flight, null, 2);
flightresult.innerHTML = flight.summary();
} catch (err) {
flightresult.innerHTML = `<span style="color:red">${err.message}</span>`;
}

View File

@@ -17,7 +17,7 @@ export class Flight {
}
summary() {
const s = [];
if (this.departureAirport?.iata) s.push(this.departureAirport.iata);
if (this.departureAirport?.iata) s.push(this.departureAirport.iata);
if (this.arrivalAirport?.iata) s.push(this.arrivalAirport.iata);
const route = s.length ? s.join(' → ') : 'Flight';
const co2 = this.emissions?.co2_total ?? this.co2 ?? null;
@@ -77,9 +77,9 @@ export class Calco2latoClient {
async searchAirports(q, limit = 20, offset = 1) {
const data = await this._fetchJSON({
method: 'GET',
query: { endpoint: 'airports.search', q, limit, offset }
query: { endpoint: 'airports.search', iata: q, per_page: limit, page: offset }
});
const items = Array.isArray(data?.results) ? data.results : (Array.isArray(data) ? data : []);
const items = Array.isArray(data?.results) ? data.results : (Array.isArray(data) ? data : []);
return items.map(a => new Airport(a));
}
@@ -94,6 +94,6 @@ export class Calco2latoClient {
method: 'POST',
body: { endpoint: 'flights.estimate', params }
});
return new Flight(data);
return new Flight(data.flights[0]);
}
}