discord.py icon indicating copy to clipboard operation
discord.py copied to clipboard

(Re-)add support for aiohttp.ClientResponse in discord.File

Open Terrance opened this issue 5 years ago • 3 comments

The Problem

During the rewrite alpha, one could pass the content from an aiohttp response object directly to a new discord.File instance:

with aiohttp.ClientSession() as sess:
    with sess.get(...) as resp:
        f = discord.File(resp.content, filename)

It looks like the file code has been refactored shortly before the stable release (5e65ec9) and this is no longer possible. Judging from the docs, this was probably an unintended feature.

The Ideal Solution

It would be nice to re-add proper support of this, such that a response body can be streamed into the upload to Discord without fetching the entire file into memory.

The Current Solution

The content needs to be loaded into a BytesIO instance:

with aiohttp.ClientSession() as sess:
    with sess.get(...) as resp:
        content = io.BytesIO(await resp.content.read())
        f = discord.File(content, filename)

Terrance avatar Apr 11 '19 21:04 Terrance

I'm assuming this hasn't been implemented, and is being closed as part of tidying up old issues?

(I can't see any related commits; this would still be useful for what it's worth, unless there's a better pattern now.)

Terrance avatar Mar 16 '22 08:03 Terrance

Correct, I was just doing some cleaning and this one was caught. I'm still not really opposed to this.

Rapptz avatar Mar 16 '22 08:03 Rapptz

I would like to reimplement this feature, however I have a question due to the ideal solution you've outlined.

The StreamReader provided by aiohttp is single-use, as it just passes the data from one end to the other, meaning if the request to send the message fails for a reason we usually retry on we can't do so.

Would storing the data while streaming it to allow retrying at the cost of temporarily using more memory be okay, or are you looking for this in order to completely avoid that?

LostLuma avatar Apr 29 '22 22:04 LostLuma