matching API andpoints
This commit is contained in:
@@ -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 ---
|
||||
@@ -72,14 +75,6 @@ try {
|
||||
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);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
case 'flights.estimate':
|
||||
// POST with JSON body: { endpoint: "flights.estimate", params: {...} }
|
||||
if ($method !== 'POST') throw new RuntimeException('Use POST');
|
||||
@@ -88,14 +83,6 @@ try {
|
||||
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);
|
||||
echo json_encode($data);
|
||||
break;
|
||||
|
||||
default:
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Unknown or unsupported endpoint']);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
4
src/.env
4
src/.env
@@ -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
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) */
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user