matching API andpoints
This commit is contained in:
@@ -2,14 +2,6 @@
|
|||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
require_once __DIR__ . '/../src/Calco2latoApiClient.php';
|
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) ---
|
// --- Basic CORS (adjust origin to your site/domain) ---
|
||||||
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
|
||||||
$allowedOrigin = preg_match('#^https://(www\.)?your-frontend\.example$#', $origin) ? $origin : '';
|
$allowedOrigin = preg_match('#^https://(www\.)?your-frontend\.example$#', $origin) ? $origin : '';
|
||||||
@@ -43,15 +35,26 @@ if ($bucket['count'] > 120) { // 120 requests/min/IP
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Instantiate API client ---
|
// --- read .env file with CALCO2LATO_API_BASE and CALCO2LATO_API_KEY
|
||||||
$base = getenv('CALCO2_API_BASE') ?: 'https://api.calco2la.to';
|
$env = file_get_contents(__DIR__."/../src/.env");
|
||||||
$key = getenv('CALCO2_API_KEY') ?: '';
|
$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) {
|
if (!$key) {
|
||||||
http_response_code(500);
|
http_response_code(500);
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
echo json_encode(['error' => 'Server misconfiguration: missing API key']);
|
echo json_encode(['error' => 'Server misconfiguration: missing API key']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Instantiate API client ---
|
||||||
$client = new Calco2latoApiClient($base, $key);
|
$client = new Calco2latoApiClient($base, $key);
|
||||||
|
|
||||||
// --- Whitelist router ---
|
// --- Whitelist router ---
|
||||||
@@ -67,19 +70,11 @@ try {
|
|||||||
// GET /?endpoint=airports.search&q=FRA&per_page=10
|
// GET /?endpoint=airports.search&q=FRA&per_page=10
|
||||||
$q = $_GET['q'] ?? '';
|
$q = $_GET['q'] ?? '';
|
||||||
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20;
|
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20;
|
||||||
$page= isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||||
$data = $client->searchAirports($q, $per_page, $page);
|
$data = $client->searchAirports($q, $per_page, $page);
|
||||||
echo json_encode($data);
|
echo json_encode($data);
|
||||||
break;
|
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);
|
|
||||||
echo json_encode($data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'flights.estimate':
|
case 'flights.estimate':
|
||||||
// POST with JSON body: { endpoint: "flights.estimate", params: {...} }
|
// POST with JSON body: { endpoint: "flights.estimate", params: {...} }
|
||||||
if ($method !== 'POST') throw new RuntimeException('Use POST');
|
if ($method !== 'POST') throw new RuntimeException('Use POST');
|
||||||
@@ -88,14 +83,6 @@ try {
|
|||||||
echo json_encode($data);
|
echo json_encode($data);
|
||||||
break;
|
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);
|
|
||||||
echo json_encode($data);
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
http_response_code(404);
|
http_response_code(404);
|
||||||
echo json_encode(['error' => 'Unknown or unsupported endpoint']);
|
echo json_encode(['error' => 'Unknown or unsupported endpoint']);
|
||||||
|
|||||||
@@ -83,15 +83,6 @@ export class Calco2latoClient {
|
|||||||
return items.map(a => new Airport(a));
|
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 ----------
|
// ---------- Flights ----------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,13 +96,4 @@ export class Calco2latoClient {
|
|||||||
});
|
});
|
||||||
return new Flight(data);
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
4
src/.env
4
src/.env
@@ -1,2 +1,2 @@
|
|||||||
CALCO2_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE
|
CALCO2LATO_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE
|
||||||
CALCO2_API_BASE=https://calco2la.to/
|
CALCO2LATO_API_BASE=https://api.calco2la.to/
|
||||||
2
src/.env.example
Normal file
2
src/.env.example
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
CALCO2LATO_API_KEY=...
|
||||||
|
CALCO2LATO_API_BASE=https://api.calco2la.to/
|
||||||
@@ -71,19 +71,13 @@ final class Calco2latoApiClient
|
|||||||
/** Search airports by free-text (IATA/ICAO/name/city/country) */
|
/** Search airports by free-text (IATA/ICAO/name/city/country) */
|
||||||
public function searchAirports(string $q, int $per_page = 20, int $page = 0): array
|
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,
|
'iata' => $q,
|
||||||
'per_page' => $per_page,
|
'per_page' => $per_page,
|
||||||
'page' => $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 ----------
|
// ---------- Flights ----------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -92,12 +86,6 @@ final class Calco2latoApiClient
|
|||||||
*/
|
*/
|
||||||
public function flightEstimate(array $params): array
|
public function flightEstimate(array $params): array
|
||||||
{
|
{
|
||||||
return $this->request('POST', '/api/latest/flights/estimate', [], $params);
|
return $this->request('POST', '/latest/transport/flights', [], $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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user