Permalink
Cannot retrieve contributors at this time
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?
liveflight/data_api.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52 lines (39 sloc)
1.25 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |