43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from datetime import date
|
|
|
|
from fastapi import FastAPI
|
|
from starlette.middleware.cors import CORSMiddleware
|
|
|
|
from regiojet_search.cache import cache
|
|
from regiojet_search.models import Result
|
|
from regiojet_search.search import fetch_cities, search
|
|
from regiojet_search.utils import lookup_city
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
@app.get("/search", response_model=list[Result])
|
|
def search_view(origin: str, destination: str, departure: date, currency: str = "EUR"):
|
|
cities = fetch_cities()
|
|
routes = search(
|
|
from_city=lookup_city(origin, cities),
|
|
to_city=lookup_city(destination, cities),
|
|
departure_date=departure,
|
|
currency=currency,
|
|
)
|
|
return routes
|
|
|
|
|
|
@app.get("/whisper")
|
|
def whisper(text: str):
|
|
cached_cities = cache.get("cities")
|
|
|
|
if cached_cities is not None:
|
|
return [cached_city["name"] for cached_city in cached_cities if text.lower() in cached_city["name"].lower()]
|
|
|
|
return []
|