first working draft
This commit is contained in:
@@ -1,6 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
require_once __DIR__ . '/../src/Calco2ApiClient.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'] ?? '';
|
||||||
@@ -44,7 +52,7 @@ if (!$key) {
|
|||||||
echo json_encode(['error' => 'Server misconfiguration: missing API key']);
|
echo json_encode(['error' => 'Server misconfiguration: missing API key']);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
$client = new Calco2ApiClient($base, $key);
|
$client = new Calco2latoApiClient($base, $key);
|
||||||
|
|
||||||
// --- Whitelist router ---
|
// --- Whitelist router ---
|
||||||
$input = json_decode(file_get_contents('php://input') ?: '[]', true) ?: [];
|
$input = json_decode(file_get_contents('php://input') ?: '[]', true) ?: [];
|
||||||
@@ -56,11 +64,11 @@ header('Content-Type: application/json; charset=utf-8');
|
|||||||
try {
|
try {
|
||||||
switch ($endpoint) {
|
switch ($endpoint) {
|
||||||
case 'airports.search':
|
case 'airports.search':
|
||||||
// GET /?endpoint=airports.search&q=FRA&limit=10
|
// GET /?endpoint=airports.search&q=FRA&per_page=10
|
||||||
$q = $_GET['q'] ?? '';
|
$q = $_GET['q'] ?? '';
|
||||||
$limit = isset($_GET['limit']) ? (int)$_GET['limit'] : 20;
|
$per_page = isset($_GET['per_page']) ? (int)$_GET['per_page'] : 20;
|
||||||
$offset= isset($_GET['offset']) ? (int)$_GET['offset'] : 0;
|
$page= isset($_GET['page']) ? (int)$_GET['page'] : 1;
|
||||||
$data = $client->searchAirports($q, $limit, $offset);
|
$data = $client->searchAirports($q, $per_page, $page);
|
||||||
echo json_encode($data);
|
echo json_encode($data);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
<script type="module">
|
<form id="airport-search-form">
|
||||||
import { Calco2Client } from '/js/calco2lato.js';
|
<input type="text" id="q" name="q" />
|
||||||
|
<button type="submit" name="Los" title="Los">Los</button>
|
||||||
|
</form>
|
||||||
|
<div id="airport-results"></div>
|
||||||
|
|
||||||
const api = new Calco2Client('/api-proxy.php');
|
<script type="module">
|
||||||
|
import { Calco2latoClient } from '/js/calco2lato.js';
|
||||||
|
|
||||||
|
const api = new Calco2latoClient('/api-proxy.php');
|
||||||
|
|
||||||
const form = document.querySelector('#airport-search-form');
|
const form = document.querySelector('#airport-search-form');
|
||||||
const list = document.querySelector('#airport-results');
|
const list = document.querySelector('#airport-results');
|
||||||
@@ -11,7 +17,7 @@
|
|||||||
list.innerHTML = 'Loading…';
|
list.innerHTML = 'Loading…';
|
||||||
try {
|
try {
|
||||||
const q = new FormData(form).get('q');
|
const q = new FormData(form).get('q');
|
||||||
const airports = await api.searchAirports(q, 10, 0);
|
const airports = await api.searchAirports(q, 10, 1);
|
||||||
list.innerHTML = airports.map(a => `<li>${a.display}</li>`).join('');
|
list.innerHTML = airports.map(a => `<li>${a.display}</li>`).join('');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
list.innerHTML = `<li style="color:red">${err.message}</li>`;
|
list.innerHTML = `<li style="color:red">${err.message}</li>`;
|
||||||
@@ -25,7 +25,7 @@ export class Flight {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Calco2Client {
|
export class Calco2latoClient {
|
||||||
/**
|
/**
|
||||||
* @param {string} proxyUrl e.g. "/api-proxy.php"
|
* @param {string} proxyUrl e.g. "/api-proxy.php"
|
||||||
* @param {object} [options]
|
* @param {object} [options]
|
||||||
@@ -71,15 +71,15 @@ export class Calco2Client {
|
|||||||
/**
|
/**
|
||||||
* @param {string} q
|
* @param {string} q
|
||||||
* @param {number} [limit=20]
|
* @param {number} [limit=20]
|
||||||
* @param {number} [offset=0]
|
* @param {number} [offset=1]
|
||||||
* @returns {Promise<Airport[]>}
|
* @returns {Promise<Airport[]>}
|
||||||
*/
|
*/
|
||||||
async searchAirports(q, limit = 20, offset = 0) {
|
async searchAirports(q, limit = 20, offset = 1) {
|
||||||
const data = await this._fetchJSON({
|
const data = await this._fetchJSON({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
query: { endpoint: 'airports.search', q, limit, offset }
|
query: { endpoint: 'airports.search', q, limit, offset }
|
||||||
});
|
});
|
||||||
const items = Array.isArray(data?.items) ? data.items : (Array.isArray(data) ? data : []);
|
const items = Array.isArray(data?.results) ? data.results : (Array.isArray(data) ? data : []);
|
||||||
return items.map(a => new Airport(a));
|
return items.map(a => new Airport(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
2
src/.env
Normal file
2
src/.env
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
CALCO2_API_KEY=1GN1pt6mEwjZTihyXuO5sdl3a2eUVr6RZ6eUqd4tvdE
|
||||||
|
CALCO2_API_BASE=https://calco2la.to/
|
||||||
@@ -69,12 +69,12 @@ final class Calco2latoApiClient
|
|||||||
// ---------- Airports ----------
|
// ---------- Airports ----------
|
||||||
|
|
||||||
/** 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 $limit = 20, int $offset = 0): array
|
public function searchAirports(string $q, int $per_page = 20, int $page = 0): array
|
||||||
{
|
{
|
||||||
return $this->request('GET', '/api/latest/airports/search', [
|
return $this->request('GET', '/api/latest/airports', [
|
||||||
'q' => $q,
|
'iata' => $q,
|
||||||
'limit' => $limit,
|
'per_page' => $per_page,
|
||||||
'offset' => $offset,
|
'page' => $page,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user