Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import requests
from fastapi.staticfiles import StaticFiles
app = FastAPI()
# Define allowed origins and configure CORS middleware
origins = [
"http://127.0.0.1:8000",
"http://localhost:8000"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Function to fetch plane data from external API
def get_plane_data(icao_code):
base_url = "https://api.airplanes.live/v2/"
api_url = "icao/"
url = f"{base_url}{api_url}{icao_code}"
# Make the request
req = requests.get(url)
# Check response status code
if req.status_code != 200:
raise HTTPException(status_code=req.status_code, detail="Error fetching data")
jsondata = req.json() # Return JSON data from response
if len(jsondata["ac"]) > 0:
flight = jsondata["ac"][0]
return flight
else:
return "No flight found"
# Endpoint to fetch plane data
@app.get("/plane/{icao_code}")
async def read_plane_data(icao_code: str):
return get_plane_data(icao_code)
app.mount('/static', StaticFiles(directory='static',html=True))
# uvicorn data_api:app --reload