From e60c0f06f7f59790ad0c1e41afeff9452f285d37 Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:54:33 +0200 Subject: [PATCH 1/2] Add Parameter `httpx_kwargs` to `HTTPXRequest` --- telegram/request/_httpxrequest.py | 20 ++++++++++++++++++-- tests/request/test_request.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/telegram/request/_httpxrequest.py b/telegram/request/_httpxrequest.py index 3dc6cf05fba..d74ed00d870 100644 --- a/telegram/request/_httpxrequest.py +++ b/telegram/request/_httpxrequest.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Lesser Public License # along with this program. If not, see [http://www.gnu.org/licenses/]. """This module contains methods to make POST and GET requests using the httpx library.""" -from typing import Collection, Optional, Tuple, Union +from typing import Any, Collection, Dict, Optional, Tuple, Union import httpx @@ -122,6 +122,20 @@ class HTTPXRequest(BaseRequest): :meth:`do_request`. Defaults to ``20`` seconds. .. versionadded:: 21.0 + httpx_kwargs (Dict[:obj:`str`, Any], optional): Additional keyword arguments to be passed + to the `httpx.AsyncClient `_ + constructor. + + Warning: + This parameter is intended for advanced users that want to fine-tune the behavior + of the underlying ``httpx`` client. The values passed here will override all the + defaults set by ``python-telegram-bot`` and all other parameters passed to + :class:`HTTPXRequest`. 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. + + .. versionadded:: NEXT.VERSION """ @@ -139,6 +153,7 @@ def __init__( socket_options: Optional[Collection[SocketOpt]] = None, proxy: Optional[Union[str, httpx.Proxy, httpx.URL]] = None, media_write_timeout: Optional[float] = 20.0, + httpx_kwargs: Optional[Dict[str, Any]] = None, ): if proxy_url is not None and proxy is not None: raise ValueError("The parameters `proxy_url` and `proxy` are mutually exclusive.") @@ -183,6 +198,7 @@ def __init__( "limits": limits, "transport": transport, **http_kwargs, + **(httpx_kwargs or {}), } try: @@ -221,7 +237,7 @@ def read_timeout(self) -> Optional[float]: return self._client.timeout.read def _build_client(self) -> httpx.AsyncClient: - return httpx.AsyncClient(**self._client_kwargs) # type: ignore[arg-type] + return httpx.AsyncClient(**self._client_kwargs) async def initialize(self) -> None: """See :meth:`BaseRequest.initialize`.""" diff --git a/tests/request/test_request.py b/tests/request/test_request.py index 6adcdf7c068..2bbfcf52de6 100644 --- a/tests/request/test_request.py +++ b/tests/request/test_request.py @@ -134,6 +134,37 @@ def test_slot_behaviour(self): assert getattr(inst, at, "err") != "err", f"got extra slot '{at}'" assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot" + def test_httpx_kwargs(self, monkeypatch): + self.test_flag = {} + + orig_init = httpx.AsyncClient.__init__ + + class Client(httpx.AsyncClient): + def __init__(*args, **kwargs): + orig_init(*args, **kwargs) + self.test_flag["args"] = args + self.test_flag["kwargs"] = kwargs + + monkeypatch.setattr(httpx, "AsyncClient", Client) + + HTTPXRequest( + connect_timeout=1, + connection_pool_size=42, + http_version="2", + httpx_kwargs={ + "timeout": httpx.Timeout(7), + "limits": httpx.Limits(max_connections=7), + "http1": True, + "verify": False, + }, + ) + kwargs = self.test_flag["kwargs"] + + assert kwargs["timeout"].connect == 7 + assert kwargs["limits"].max_connections == 7 + assert kwargs["http1"] is True + assert kwargs["verify"] is False + async def test_context_manager(self, monkeypatch): async def initialize(): self.test_flag = ["initialize"] From 32f5a45e25f19458bf8c9a6af406f290fbc8d20b Mon Sep 17 00:00:00 2001 From: Hinrich Mahler <22366557+Bibo-Joshi@users.noreply.github.com> Date: Mon, 2 Sep 2024 22:02:26 +0200 Subject: [PATCH 2/2] Adapt AppBuilder tests --- tests/ext/test_applicationbuilder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ext/test_applicationbuilder.py b/tests/ext/test_applicationbuilder.py index 189164d1b8c..235a80f682d 100644 --- a/tests/ext/test_applicationbuilder.py +++ b/tests/ext/test_applicationbuilder.py @@ -74,7 +74,7 @@ def test_all_methods_request(self, builder, get_updates): arguments = inspect.signature(HTTPXRequest.__init__).parameters.keys() prefix = "get_updates_" if get_updates else "" for argument in arguments: - if argument == "self": + if argument in ("self", "httpx_kwargs"): continue if argument == "media_write_timeout" and get_updates: # get_updates never makes media requests pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy