Skip to content

Commit a5c04bb

Browse files
committed
fully async flask app based on quart and aiohttp
1 parent aa607bd commit a5c04bb

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

src/10-async-web/.idea/dictionaries/screencaster.xml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/10-async-web/acityscape_api/requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
quart
33

44
# Calling services requirements
5-
requests
5+
aiohttp
6+
aiodns
7+
cchardet
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import random
22
import time
33
from typing import Tuple
4-
import requests
4+
5+
import aiohttp
56

67
use_cached_data = False
78

@@ -10,18 +11,19 @@
1011
0.535646, 0.527148, 0.533472, 0.53351, 0.523462]
1112

1213

13-
def get_lat_long(zip_code: str, country: str) -> Tuple[float, float]:
14+
async def get_lat_long(zip_code: str, country: str) -> Tuple[float, float]:
1415
key = f'{zip_code}, {country}'
1516
url = f'http://www.datasciencetoolkit.org/street2coordinates/{key.replace(" ", "+")}'
1617

1718
if use_cached_data:
1819
time.sleep(random.choice(measured_latency_in_sec))
1920
return 45.50655, -122.733888
2021
else:
21-
resp = requests.get(url)
22-
resp.raise_for_status()
22+
async with aiohttp.ClientSession() as session:
23+
async with session.get(url) as resp:
24+
resp.raise_for_status()
2325

24-
data = resp.json()
26+
data = await resp.json()
2527

2628
city_data = data.get(f'{zip_code}, {country}', dict())
2729
return city_data.get('latitude', 0.00), city_data.get('longitude', 0.00)

src/10-async-web/acityscape_api/services/sun_service.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
import random
33
import time
44

5-
import requests
5+
import aiohttp
66

77
measured_latency_in_sec = [0.399203, 0.7046, 0.422959, 0.741911, 0.404674]
88
use_cached_data = False
99

1010

11-
def for_today(latitude: float, longitude: float) -> dict:
11+
async def for_today(latitude: float, longitude: float) -> dict:
1212
url = f'https://api.sunrise-sunset.org/json?lat={latitude}&lng={longitude}'
1313

1414
if use_cached_data: # Set in config/dev.json or config/prod.json
@@ -18,10 +18,12 @@ def for_today(latitude: float, longitude: float) -> dict:
1818
'nautical_twilight_begin': '04:49:54 AM', 'nautical_twilight_end': '09:43:03 PM',
1919
'astronomical_twilight_begin': '04:03:13 AM', 'astronomical_twilight_end': '10:29:44 PM'}
2020
else:
21-
resp = requests.get(url)
22-
resp.raise_for_status()
21+
async with aiohttp.ClientSession() as session:
22+
async with session.get(url) as resp:
23+
24+
data = await resp.json()
25+
sun_data = data.get('results', {})
2326

24-
sun_data = resp.json().get('results', {})
2527
for k, v in list(sun_data.items()):
2628
if 'AM' not in v and 'PM' not in v:
2729
continue
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import requests
1+
import aiohttp
22

33
__api_key = ''
44

@@ -8,9 +8,10 @@ def global_init(api_key: str):
88
__api_key = api_key
99

1010

11-
def get_current(zip_code: str, country_code: str) -> dict:
11+
async def get_current(zip_code: str, country_code: str) -> dict:
1212
url = f'https://api.openweathermap.org/data/2.5/weather?zip={zip_code},{country_code}&appid={__api_key}'
13-
resp = requests.get(url)
14-
resp.raise_for_status()
13+
async with aiohttp.ClientSession() as session:
14+
async with session.get(url) as resp:
15+
resp.raise_for_status()
1516

16-
return resp.json()
17+
return await resp.json()

src/10-async-web/acityscape_api/views/city_api.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55

66

77
@blueprint.route('/api/weather/<zip_code>/<country>', methods=['GET'])
8-
def weather(zip_code: str, country: str):
9-
weather_data = weather_service.get_current(zip_code, country)
8+
async def weather(zip_code: str, country: str):
9+
weather_data = await weather_service.get_current(zip_code, country)
1010
if not weather_data:
1111
quart.abort(404)
1212
return quart.jsonify(weather_data)
1313

1414

1515
@blueprint.route('/api/sun/<zip_code>/<country>', methods=['GET'])
16-
def sun(zip_code: str, country: str):
17-
lat, long = location_service.get_lat_long(zip_code, country)
18-
sun_data = sun_service.for_today(lat, long)
16+
async def sun(zip_code: str, country: str):
17+
lat, long = await location_service.get_lat_long(zip_code, country)
18+
sun_data = await sun_service.for_today(lat, long)
1919
if not sun_data:
2020
quart.abort(404)
2121
return quart.jsonify(sun_data)

0 commit comments

Comments
 (0)