Created
September 23, 2023 16:13
-
-
Save arjunprakash027/b2aa48b7d5de1dce7bab50c4cbe836ae to your computer and use it in GitHub Desktop.
A simple async await program that gets data of universities using api in python
This file contains hidden or 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
import asyncio | |
import time | |
import httpx | |
import json | |
url = 'http://universities.hipolabs.com/search' | |
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: | |
universities.append({ | |
'name': university['name'], | |
'domains': university['domains'] | |
}) | |
data[country] = universities | |
async def get_universities_async() -> dict: | |
data: dict = {} | |
await asyncio.gather(get_all_universities_for_country_async("turkey", data), | |
get_all_universities_for_country_async("india", data), | |
get_all_universities_for_country_async("australia", data)) | |
print(data) | |
asyncio.run(get_universities_async()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment