Skip to content

Commit ece9865

Browse files
authored
Merge pull request #72 from jbouzekri/requests-timeout
add requests timeout parameter
2 parents 40e68b3 + a783364 commit ece9865

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ can pass just `wp = WebPusher(subscription_info)`. This will return a `WebPusher
112112

113113
The following methods are available:
114114

115-
#### `.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aesgcm", curl=False)`
115+
#### `.send(data, headers={}, ttl=0, gcm_key="", reg_id="", content_encoding="aesgcm", curl=False, timeout=None)`
116116

117117
Send the data using additional parameters. On error, returns a `WebPushException`
118118

@@ -134,6 +134,9 @@ Developer Console.
134134
*curl* Do not execute the POST, but return as a `curl` command. This will write the encrypted content to a local file
135135
named `encrpypted.data`. This command is meant to be used for debugging purposes.
136136

137+
*timeout* timeout for requests POST query.
138+
See [requests documentation](http://docs.python-requests.org/en/master/user/quickstart/#timeouts).
139+
137140
**Example**
138141

139142
to send from Chrome using the old GCM mode:

pywebpush/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def as_curl(self, endpoint, encoded_data, headers):
212212
url=endpoint, headers="".join(header_list), data=data))
213213

214214
def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
215-
content_encoding="aesgcm", curl=False):
215+
content_encoding="aesgcm", curl=False, timeout=None):
216216
"""Encode and send the data to the Push Service.
217217
218218
:param data: A serialized block of data (see encode() ).
@@ -233,6 +233,8 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
233233
:type content_encoding: str
234234
:param curl: Display output as `curl` command instead of sending
235235
:type curl: bool
236+
:param timeout: POST requests timeout
237+
:type timeout: float or tuple
236238
237239
"""
238240
# Encode the data.
@@ -285,15 +287,17 @@ def send(self, data=None, headers=None, ttl=0, gcm_key=None, reg_id=None,
285287
return self.as_curl(endpoint, encoded_data, headers)
286288
return requests.post(endpoint,
287289
data=encoded_data,
288-
headers=headers)
290+
headers=headers,
291+
timeout=timeout)
289292

290293

291294
def webpush(subscription_info,
292295
data=None,
293296
vapid_private_key=None,
294297
vapid_claims=None,
295298
content_encoding="aesgcm",
296-
curl=False):
299+
curl=False,
300+
timeout=None):
297301
"""
298302
One call solution to endcode and send `data` to the endpoint
299303
contained in `subscription_info` using optional VAPID auth headers.
@@ -330,6 +334,8 @@ def webpush(subscription_info,
330334
:type content_encoding: str
331335
:param curl: Return as "curl" string instead of sending
332336
:type curl: bool
337+
:param timeout: POST requests timeout
338+
:type timeout: float or tuple
333339
:return requests.Response or string
334340
335341
"""
@@ -354,6 +360,7 @@ def webpush(subscription_info,
354360
vapid_headers,
355361
content_encoding=content_encoding,
356362
curl=curl,
363+
timeout=timeout,
357364
)
358365
if not curl and result.status_code > 202:
359366
raise WebPushException("Push failed: {}: {}".format(

pywebpush/tests/test_webpush.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,13 @@ def test_gcm(self, mock_post):
294294
eq_(pdata["registration_ids"][0], "regid123")
295295
eq_(pheaders.get("authorization"), "key=gcm_key_value")
296296
eq_(pheaders.get("content-type"), "application/json")
297+
298+
@patch("requests.post")
299+
def test_timeout(self, mock_post):
300+
mock_post.return_value = Mock()
301+
mock_post.return_value.status_code = 200
302+
subscription_info = self._gen_subscription_info()
303+
WebPusher(subscription_info).send(timeout=5.2)
304+
eq_(mock_post.call_args[1].get('timeout'), 5.2)
305+
webpush(subscription_info, timeout=10.001)
306+
eq_(mock_post.call_args[1].get('timeout'), 10.001)

0 commit comments

Comments
 (0)