Skip to content

Commit b4ae4e1

Browse files
author
Matej Vetrak
committed
Add option to use requests.Session object
1 parent 1d1e1d4 commit b4ae4e1

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pywebpush/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class WebPusher:
9595
"aes128gcm" # draft-httpbis-encryption-encoding-04
9696
]
9797

98-
def __init__(self, subscription_info):
98+
def __init__(self, subscription_info, requests_session=None):
9999
"""Initialize using the info provided by the client PushSubscription
100100
object (See
101101
https://developer.mozilla.org/en-US/docs/Web/API/PushManager/subscribe)
@@ -104,7 +104,16 @@ def __init__(self, subscription_info):
104104
the client.
105105
:type subscription_info: dict
106106
107+
:param requests_session: a requests.Session object to optimize requests
108+
to the same client.
109+
:type requests_session: requests.Session
110+
107111
"""
112+
if requests_session is None:
113+
self.requests_method = requests
114+
else:
115+
self.requests_method = requests_session
116+
108117
if 'endpoint' not in subscription_info:
109118
raise WebPushException("subscription_info missing endpoint URL")
110119
self.subscription_info = subscription_info
@@ -285,7 +294,7 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
285294
# Authorization / Crypto-Key (VAPID headers)
286295
if curl:
287296
return self.as_curl(endpoint, encoded_data, headers)
288-
return requests.post(endpoint,
297+
return self.requests_method.post(endpoint,
289298
data=encoded_data,
290299
headers=headers,
291300
timeout=timeout)

0 commit comments

Comments
 (0)