Skip to content

Commit

Permalink
Merge pull request #31 from allburov/share-session
Browse files Browse the repository at this point in the history
Share session between requests
  • Loading branch information
evgeniibreikin authored Jan 3, 2025
2 parents fb9fd31 + 4b8ab6d commit dcd2ad5
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions pusher_push_notifications/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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,
Expand All @@ -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:
Expand Down

0 comments on commit dcd2ad5

Please sign in to comment.