0

I got this function:

async def download(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            if resp.status == 200:
                return resp

To get the data, I'd need to write:

text = await (await utils.download(url)).text()

How to change download so I can write like this

text = await utils.download(url).text()

without getting AttributeError: 'coroutine' object has no attribute 'text'?

Dlean Jeans
  • 966
  • 1
  • 8
  • 23

1 Answers1

0

May be this can help simplifying the usage:

async def download(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            if resp.status == 200:
                return await resp.text()

Then to use it:

async def main():
    text = await download("http://python.org")
Tal L
  • 342
  • 1
  • 14
  • Yes, but I also want to call read() or json() or get some attribute – Dlean Jeans Apr 15 '20 at 06:29
  • @DleanJeans not sure that's possible as is, if I understand correctly the need. But you could tuple the results you need, if you know in advance what is needed: In download: return await resp.text(), await resp.read() In usage: text, read = await download(url) – Tal L Apr 15 '20 at 06:52