Skip to content

Commit dcd2ad5

Browse files
Merge pull request #31 from allburov/share-session
Share session between requests
2 parents fb9fd31 + 4b8ab6d commit dcd2ad5

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

pusher_push_notifications/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ def __init__(self, instance_id, secret_key, endpoint=None):
108108
self.secret_key = secret_key
109109
self._endpoint = endpoint
110110

111+
session = requests.Session()
112+
# We've had multiple support requests about this library not working
113+
# on PythonAnywhere (a popular python deployment platform)
114+
# They require that proxy servers be loaded from the environment when
115+
# making requests (on their free plan).
116+
# This reintroduces the proxy support that is the default in requests
117+
# anyway.
118+
session.proxies = _get_proxies_from_env()
119+
self.session = session
120+
121+
111122
@property
112123
def endpoint(self):
113124
"""Property method to calculate the correct Pusher API host"""
@@ -124,15 +135,6 @@ def _make_request(self, method, path, path_params, body=None):
124135
path = path.format(**path_params)
125136
url = _make_url(scheme='https', host=self.endpoint, path=path)
126137

127-
session = requests.Session()
128-
# We've had multiple support requests about this library not working
129-
# on PythonAnywhere (a popular python deployment platform)
130-
# They require that proxy servers be loaded from the environment when
131-
# making requests (on their free plan).
132-
# This reintroduces the proxy support that is the default in requests
133-
# anyway.
134-
session.proxies = _get_proxies_from_env()
135-
136138
request = requests.Request(
137139
method,
138140
url,
@@ -146,7 +148,7 @@ def _make_request(self, method, path, path_params, body=None):
146148
},
147149
)
148150

149-
response = session.send(request.prepare())
151+
response = self.session.send(request.prepare())
150152

151153
if response.status_code != 200:
152154
try:

0 commit comments

Comments
 (0)