Skip to content

Commit

Permalink
Add city get endpoint
Browse files Browse the repository at this point in the history
get request that takes city name and returns longitude and latitude data for given city from openweather api.
  • Loading branch information
Robin-Britz committed Sep 18, 2024
1 parent 6ac1465 commit 77cb1d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
_pycache_/
openweather-venv/
creds.env


.env
Binary file modified __pycache__/main.cpython-312.pyc
Binary file not shown.
27 changes: 18 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import typing
import fastapi
import dotenv
import os
import requests

app = fastapi.FastAPI()

with open("creds.env", "r") as key:
api_key = key.readline()
dotenv.load_dotenv()
api_key = os.getenv("API_KEY")

# print(api_key)
app = fastapi.FastAPI()


@app.get("/")
def read_root():
return {"Hello": "World"}
return {"OpenWeather": "API"}


@app.get("/city/")
def get_city(city: str = fastapi.Query(description="City name")):
"""Retrieve latitude and longitude for given city."""
open_weather_api = f"http://api.openweathermap.org/geo/1.0/direct?q={city}=&appid={api_key}"
city_results = requests.get(open_weather_api).json()
if city_results:
latitude = city_results[0]["lat"]
longitude = city_results[0]["lon"]
return {"Latitude": latitude, "Longitude": longitude}
return {"Error": "No results found"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: typing.Union[str, None] = None):
return {"item_id": item_id, "q": q}
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
python-dotenv==1.0.1
fastapi==0.114.2
pydantic==2.9.1
pydantic_core==2.23.3
uvicorn==0.30.6
requests==2.32.3
requests==2.32.3
typing_extensions==4.12.2
python-dotenv==1.0.1

0 comments on commit 77cb1d3

Please sign in to comment.