editazzz commited on
Commit
4a49187
·
verified ·
1 Parent(s): 4c898be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -1,40 +1,38 @@
1
-
2
- from fastapi import FastAPI, Query
3
- from pydantic import BaseModel
4
- import requests
5
- from datetime import date
6
- import uvicorn
7
- app = FastAPI()
8
- class WeatherRequest(BaseModel):
9
- location: str
10
- API_KEY = "d66b99384fc0495b9bb43946242607"
11
- BASE_URL = "http://api.weatherapi.com/v1/current.json"
12
- def get_weather(location):
13
- params = {
14
- "key": API_KEY,
15
- "q": location,
16
- "dt": date.today().isoformat()
17
- }
18
-
19
- response = requests.get(BASE_URL, params=params)
20
-
21
- if response.status_code == 200:
22
- data = response.json()
23
- current = data['current']
24
- return f"Temperature: {current['temp_c']}°C, Condition: {current['condition']['text']}"
25
- else:
26
- return "Unable to fetch weather data"
27
- @app.get("/")
28
- def home():
29
- return {"message": "Welcome to the Weather Finder API"}
30
- @app.get("/weather")
31
- def get_weather_endpoint(location: str = Query(..., description="Location to get weather for")):
32
- weather_info = get_weather(location)
33
- return {"location": location, "weather_info": weather_info}
34
- @app.post("/weather")
35
- def post_weather_endpoint(request: WeatherRequest):
36
- weather_info = get_weather(request.location)
37
- return {"location": request.location, "weather_info": weather_info}
38
-
39
- if __name__ == "__main__":
40
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+
2
+ from fastapi import FastAPI, Query
3
+ from pydantic import BaseModel
4
+ import requests
5
+ from datetime import date
6
+ import uvicorn
7
+ app = FastAPI()
8
+ class WeatherRequest(BaseModel):
9
+ location: str
10
+ API_KEY = "d66b99384fc0495b9bb43946242607"
11
+ BASE_URL = "http://api.weatherapi.com/v1/current.json"
12
+ def get_weather(location):
13
+ params = {
14
+ "key": API_KEY,
15
+ "q": location,
16
+ "dt": date.today().isoformat()
17
+ }
18
+
19
+ response = requests.get(BASE_URL, params=params)
20
+
21
+ if response.status_code == 200:
22
+ data = response.json()
23
+ current = data['current']
24
+ return f"Temperature: {current['temp_c']}°C, Condition: {current['condition']['text']}"
25
+ else:
26
+ return "Unable to fetch weather data"
27
+ @app.get("/")
28
+ def home():
29
+ return {"message": "Welcome to the Weather Finder API"}
30
+ @app.get("/weather")
31
+ def get_weather_endpoint(location: str = Query(..., description="Location to get weather for")):
32
+ weather_info = get_weather(location)
33
+ return {"location": location, "weather_info": weather_info}
34
+ @app.post("/weather")
35
+ def post_weather_endpoint(request: WeatherRequest):
36
+ weather_info = get_weather(request.location)
37
+ return {"location": request.location, "weather_info": weather_info}
38
+