-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get request that takes city name and returns longitude and latitude data for given city from openweather api.
- Loading branch information
1 parent
6ac1465
commit 77cb1d3
Showing
4 changed files
with
24 additions
and
13 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
_pycache_/ | ||
openweather-venv/ | ||
creds.env | ||
|
||
|
||
.env |
Binary file not shown.
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
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} |
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
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 |