I'm new to Async programming, Asyncio, Aiohttp world.
I have multiple requests where - output of 1st request is being used as a parameter in 2nd request.
So far I have tried this much -
import aiohttp
import asyncio
async def get_response(session, url, params):
async with session.get(url=url, params=params) as response:
response = await response.json()
return response['output']
async def main():
tasks = []
url = "https://example.api.com/"
url_1 = url + 'path_1'
url_2 = url + 'path_2'
params = {'name': "Hello"}
async with aiohttp.ClientSession() as session:
a1 = get_response(session, url_1, params)
tasks.append(a1)
params = {'name': a1}
b1 = get_response(session, url_2, params)
tasks.append(b1)
responses = await asyncio.gather(*tasks, return_exceptions=True)
print(responses)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
I'm getting this error which is obvious -
TypeError("Invalid variable type: value should be str, int or float, got <coroutine object get_response at 0x00000000037FA6C0> of type <class 'coroutine'>")
So how do I pass the output of 1st request and not the coroutine object to the 2nd request -
params = {'name': a1}
b1 = get_response(session, url_2, params)