From 4b8ab6d71ae5151c4e2c27330a319e5e2bd2d4d0 Mon Sep 17 00:00:00 2001 From: allburov Date: Mon, 29 Jan 2024 12:29:21 +0700 Subject: [PATCH] Share session between requests --- pusher_push_notifications/__init__.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pusher_push_notifications/__init__.py b/pusher_push_notifications/__init__.py index b5ea38e..b267be7 100644 --- a/pusher_push_notifications/__init__.py +++ b/pusher_push_notifications/__init__.py @@ -108,6 +108,17 @@ def __init__(self, instance_id, secret_key, endpoint=None): self.secret_key = secret_key self._endpoint = endpoint + session = requests.Session() + # We've had multiple support requests about this library not working + # on PythonAnywhere (a popular python deployment platform) + # They require that proxy servers be loaded from the environment when + # making requests (on their free plan). + # This reintroduces the proxy support that is the default in requests + # anyway. + session.proxies = _get_proxies_from_env() + self.session = session + + @property def endpoint(self): """Property method to calculate the correct Pusher API host""" @@ -124,15 +135,6 @@ def _make_request(self, method, path, path_params, body=None): path = path.format(**path_params) url = _make_url(scheme='https', host=self.endpoint, path=path) - session = requests.Session() - # We've had multiple support requests about this library not working - # on PythonAnywhere (a popular python deployment platform) - # They require that proxy servers be loaded from the environment when - # making requests (on their free plan). - # This reintroduces the proxy support that is the default in requests - # anyway. - session.proxies = _get_proxies_from_env() - request = requests.Request( method, url, @@ -146,7 +148,7 @@ def _make_request(self, method, path, path_params, body=None): }, ) - response = session.send(request.prepare()) + response = self.session.send(request.prepare()) if response.status_code != 200: try: