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: