Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/api/main.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import functions_framework
from flask import abort

from flask import Flask, request, abort
from weather import get_weather

app = Flask(__name__)

@functions_framework.http
def handle_request(request):
@app.route('/')
def handle_request():
if request.method == "GET":
city = request.args.get("city")
if not city:
Expand All @@ -18,3 +17,6 @@ def handle_request(request):
return abort(500, response)
else:
return abort(403)

if __name__ == '__main__':
app.run()
11 changes: 3 additions & 8 deletions src/api/weather.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import requests


api_key = "KEY"

api_key = "YOUR_API_KEY"

def format_weather(city: str, data: dict) -> str:
weather = data["weather"][0]["description"]
temperature = data["main"]["temp"]
return (
f"The weather in {city} is {weather} "
f"with a temperature of {temperature} Celcius."
f"with a temperature of {temperature} Celsius."
)


def get_lat_lon(city):
url = (
f"http://api.openweathermap.org/geo/1.0/direct?"
Expand All @@ -31,7 +28,6 @@ def get_lat_lon(city):
print("Failed to retrieve latitude and longitude.", data)
return None, None


def get_weather(city):
lat, lon = get_lat_lon(city)
if lat is None or lon is None:
Expand All @@ -53,7 +49,6 @@ def get_weather(city):
print("Failed to retrieve weather information.", data)
return False, "Failed to retrieve weather information."


if __name__ == "__main__":
city = "Munich"
print(get_weather(city))
print(get_weather(city))