forked from sumanentc/python-sample-FastAPI-application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
universities.py
29 lines (24 loc) · 1007 Bytes
/
universities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import httpx
import json
from sql_app.schemas import University
url = 'http://universities.hipolabs.com/search'
def get_all_universities_for_country(country: str) -> dict:
params = {'country': country}
client = httpx.Client()
response = client.get(url, params=params)
response_json = json.loads(response.text)
universities = []
for university in response_json:
university_obj = University.parse_obj(university)
universities.append(university_obj)
return {country: universities}
async def get_all_universities_for_country_async(country: str, data: dict) -> None:
params = {'country': country}
async with httpx.AsyncClient() as client:
response = await client.get(url, params=params)
response_json = json.loads(response.text)
universities = []
for university in response_json:
university_obj = University.parse_obj(university)
universities.append(university_obj)
data[country] = universities