From 92f555f5fa537c7242262af218362b956b1f90f8 Mon Sep 17 00:00:00 2001 From: Andrew Morgan <1342360+anoadragon453@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:51:22 +0100 Subject: [PATCH] Fix breakage with `aiohttp>=3.10.0` when using GCM and an HTTP proxy (#395) --- changelog.d/395.bugfix | 1 + sygnal/gcmpushkin.py | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 changelog.d/395.bugfix diff --git a/changelog.d/395.bugfix b/changelog.d/395.bugfix new file mode 100644 index 00000000..8c4e4494 --- /dev/null +++ b/changelog.d/395.bugfix @@ -0,0 +1 @@ +Fix incompatibility with `aiohttp>=3.10.0` when using GCM with an HTTP proxy. \ No newline at end of file diff --git a/sygnal/gcmpushkin.py b/sygnal/gcmpushkin.py index 0f83f1ff..b454da06 100644 --- a/sygnal/gcmpushkin.py +++ b/sygnal/gcmpushkin.py @@ -209,16 +209,8 @@ def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]) -> None: f"`service_account_file` must be valid: {str(e)}", ) - session = None - if proxy_url: - # `ClientSession` can't directly take the proxy URL, so we need to - # set the usual env var and use `trust_env=True` - os.environ["HTTPS_PROXY"] = proxy_url - session = aiohttp.ClientSession(trust_env=True, auto_decompress=False) - - self.google_auth_request = google.auth.transport._aiohttp_requests.Request( - session=session - ) + # This is instantiated in `self.create` + self.google_auth_request: google.auth.transport._aiohttp_requests.Request # Use the fcm_options config dictionary as a foundation for the body; # this lets the Sygnal admin choose custom FCM options @@ -241,6 +233,21 @@ async def create( Returns: an instance of this Pushkin """ + session = None + proxy_url = sygnal.config.get("proxy") + if proxy_url: + # `ClientSession` can't directly take the proxy URL, so we need to + # set the usual env var and use `trust_env=True` + os.environ["HTTPS_PROXY"] = proxy_url + + # ClientSession must be instantiated by an async function, hence we do this + # here instead of `__init__`. + session = aiohttp.ClientSession(trust_env=True, auto_decompress=False) + + cls.google_auth_request = google.auth.transport._aiohttp_requests.Request( + session=session + ) + return cls(name, sygnal, config) async def _perform_http_request(