Skip to content

Aiohttp request #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Aiohttp request #126

wants to merge 4 commits into from

Conversation

Poolitzer
Copy link
Member

This is my take on solving python-telegram-bot/python-telegram-bot#4560. I think the implementation is correct, I copied all appropriate httpx tests over.

There are open points I need feedback on:
Implementation:

  • read_timeout property needs to be implemented, but this doesn't make sense since aiohttp doesn't have that timeout. Why do even have this property do we actually need it? Should I just pass the most fitting timeout like I do?
  • same, do_request needs all the httpx timeout params. They dont make sense at all with aiohttp, should I raise an error? I just ignore them anyway lol

Tests:

  • the two init tests are skipped because I dont get at all which params are selected in the httpx test suit to be passed here and which aren't, can someone explain?
  • the last test is skipped for now because I want to understand if I have to build my own bot instance or not
  • the failing test test_multiple_init_cycles I don't understand at all why it fails. Because the async with block should (as implemented and kinda tested above) properly shut down and initialize the instance, why doesn't it
  • and the timeout test raise makes the text xfail which is a) really funny and b) also kinda a successful test in its own, right?

Copy link
Member

@Bibo-Joshi Bibo-Joshi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. Thanks for taking the time to PR for that :) I hope the authors of python-telegram-bot/python-telegram-bot#4560 appreciate it :)

  • read_timeout property needs to be implemented, but this doesn't make sense since aiohttp doesn't have that timeout. Why do even have this property do we actually need it? Should I just pass the most fitting timeout like I do?

This was introduced in python-telegram-bot/python-telegram-bot#3963 and enables Bot.get_updates to use the default read timeout specified in the request object rather than hard-coding a custom default value.

  • same, do_request needs all the httpx timeout params. They dont make sense at all with aiohttp, should I raise an error? I just ignore them anyway lol

IMO you should ensure that aiohttprequest.do_request(…, read_timeout=aiohttprequest.read_timeout) behaves the same as aiohttprequest.do_request(…). If aiohttp has a different set of timeout settings, then you should map our parameters to them as good as possible.
If you do not use all of them, I'd personally recommend to raise a warning (in case the passed value differs from DEFAULT_NONE) but not raise an exception.


I did not look at the tests yet

@@ -0,0 +1,46 @@
# AiohttpRequest instance

This is an implementation of aiohttp, to be used as a replacement for httpx based on "popular" demand [#4560](python-telegram-bot/python-telegram-bot#4560).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
This is an implementation of aiohttp, to be used as a replacement for httpx based on "popular" demand [#4560](python-telegram-bot/python-telegram-bot#4560).
This is an implementation of [`BaseRequest`](docs.ptb.org/…) based on [aiohttp](https://aiohttp-docs), to be used as alternative for [`HTTPXRequest`](docs.ptb.org/…) based on the request in [#4560](python-telegram-bot/python-telegram-bot#4560).



async def main():
bot = telegram.Bot("TOKEN", request=AiohttpRequest())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bot = telegram.Bot("TOKEN", request=AiohttpRequest())
bot = telegram.Bot("TOKEN", request=AiohttpRequest(), get_updates_request=AiohttpRequest())

Comment on lines +25 to +26
from telegram._utils.logging import get_logger
from telegram._utils.types import ODVInput
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please do not use ptb internals

from typing import Any, Optional, Union

import aiohttp
import yarl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not listed in requirements.txt

Comment on lines +61 to +67
This parameter is intended for advanced users that want to fine-tune the behavior
of the underlying ``aiohttp`` clientSession. The values passed here will override
all the defaults set by ``python-telegram-bot`` and all other parameters passed to
:class:`ClientSession`. The only exception is the :paramref:`media_write_timeout`
parameter, which is not passed to the client constructor.
No runtime warnings will be issued about parameters that are overridden in this
way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update this. AiohttpRequest is no PTB class and there is also no parameter media_write_timeout here.

Comment on lines +90 to +91
# this is needed because there are errors if one uses async with or a normal def
# with ApplicationBuilder, apparently. I am confused. But it works.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is "this" here? I am confused about what you're confused about.

Comment on lines +116 to +117
This makes not a lot of sense to implement since there is no actual read_timeout.
But what can I do.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can write a docstring that tells the user which value you return here and why :)

)

# I dont think it makes sense to support the timeout params.
# PylintDoesntAllowMeToWriteTOLiDO if no one complains in initial PR
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

) from err
raise TimedOut from err
except aiohttp.ClientError as err:
# HTTPError must come last as its the base httpx exception class
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# HTTPError must come last as its the base httpx exception class
# HTTPError must come last as its the base aiohttp exception class

?

timeout=timeout,
data=data,
)
except TimeoutError as err:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimeoutError is not imported from anywhere

@Bibo-Joshi
Copy link
Member

@Poolitzer are you still interested in working on this?

@Poolitzer
Copy link
Member Author

yes just got busy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants