Skip to content

Share session between requests #31

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

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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