You can download this code by clicking the button below.
This code is now available for download.
This function uses the Aiohttp library to asynchronously fetch user information from a random user generation API. It accepts a user ID as a parameter, constructs a URL, and then sends a GET request through an Aiohttp ClientSession, returning the JSON response.
Technology Stack : Aiohttp, asyncio
Code Type : Asynchronous HTTP request
Code Difficulty : Intermediate
import aiohttp
import asyncio
import random
def fetch_random_user(user_id):
async def fetch(session):
url = f'https://randomuser.me/api/?id={user_id}'
async with session.get(url) as response:
return await response.json()
async def main():
async with aiohttp.ClientSession() as session:
data = await fetch(session)
return data
return asyncio.run(main())