107 lines
3.7 KiB
Python

from fastapi import FastAPI, Response
from prometheus_client import generate_latest, CONTENT_TYPE_LATEST
from .service import get_all_coordinates, get_coordinates_for_city
from .metrics import RequestTimer
app = FastAPI(title="Open-Meteo Coordinates Service")
@app.get("/coordinates")
def coordinates():
with RequestTimer(endpoint="/coordinates", method="GET") as t:
try:
return get_all_coordinates()
except Exception:
t.set_status("500")
raise
@app.get("/coordinates/{city}")
def coordinates_for_city(city: str):
with RequestTimer(endpoint="/coordinates/{city}", method="GET") as t:
try:
return get_coordinates_for_city(city)
except Exception:
t.set_status("500")
raise
@app.get("/help")
def help_endpoint():
return {
"api_name": "Open-Meteo Coordinates Service",
"description": "Get geographical coordinates for cities",
"available_cities": ["Tel Aviv", "Beersheba", "Jerusalem", "Szeged"],
"endpoints": [
{
"path": "/coordinates",
"method": "GET",
"description": "Get coordinates for all available cities",
"example_request": "GET https://open-meteo.dvirlabs.com/coordinates",
"example_response": {
"source": "cache",
"data": {
"Tel Aviv": {
"name": "Tel Aviv",
"latitude": 32.08088,
"longitude": 34.78057,
"country": "Israel"
},
"Beersheba": {
"name": "Beersheba",
"latitude": 31.25181,
"longitude": 34.7913,
"country": "Israel"
}
}
}
},
{
"path": "/coordinates/{city}",
"method": "GET",
"description": "Get coordinates for a specific city",
"example_request": "GET https://open-meteo.dvirlabs.com/coordinates/Tel Aviv",
"example_response": {
"source": "cache",
"data": {
"name": "Tel Aviv",
"latitude": 32.08088,
"longitude": 34.78057,
"country": "Israel"
}
}
},
{
"path": "/healthz",
"method": "GET",
"description": "Health check endpoint",
"example_request": "GET https://open-meteo.dvirlabs.com/healthz",
"example_response": {
"status": "ok"
}
},
{
"path": "/metrics",
"method": "GET",
"description": "Prometheus metrics endpoint",
"example_request": "GET https://open-meteo.dvirlabs.com/metrics"
}
],
"usage_examples": {
"curl_all_cities": "curl https://open-meteo.dvirlabs.com/coordinates",
"curl_single_city": "curl https://open-meteo.dvirlabs.com/coordinates/Jerusalem",
"curl_with_spaces": "curl 'https://open-meteo.dvirlabs.com/coordinates/Tel%20Aviv'",
"python": "import requests\nresponse = requests.get('https://open-meteo.dvirlabs.com/coordinates/Jerusalem')\ndata = response.json()"
}
}
@app.get("/metrics")
def metrics():
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)
@app.get("/healthz")
def healthz():
return {"status": "ok"}