The interface for sending requests synchronously is very simple:
# using requests package
content = requests.get('http://www.example.com').content
# using httpx package:
content = httpx.get('http://www.example.com').content
However, when sending requests asynchronously, it gets a bit more complex:
# using aiohttp package
async with aiohttp.ClientSession() as session:
async with session.get('http://www.example.org') as response:
content = await response.text()
# using httpx package
async with httpx.AsyncClient() as client:
response = await client.get('http://www.example.org')
content = response.content
Why must we use these Client
objects when sending asynchronous requests?
Why can't the interface be as simple as when sending synchronous requests:
# using imaginary package
response = await aiopackage.get('http://www.example.com')
content = response.content