Skip to content

Commit 05d66a1

Browse files
authored
Merge pull request #407 from weakish/push-expiration-time-encoding
push expiration time encoding
2 parents 8808c48 + feddfc6 commit 05d66a1

5 files changed

Lines changed: 37 additions & 27 deletions

File tree

Pipfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ arrow = "*"
1919
"iso8601" = "*"
2020
six = ">=1.11.0"
2121
qiniu = ">=7.1.4,<7.2.4"
22-
requests = ">=2.22.0"
22+
"urllib3" = ">=1.24.3,<1.25.0"
23+
requests = ">=2.20.0,<2.22.0"
2324
werkzeug = "*"
2425
gevent = ">=1.0.2,<2.0.0"
2526
typing = { version = "*", markers = "python_version < '3.5.0'" }

Pipfile.lock

Lines changed: 15 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

leancloud/push.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ def save(self, *args, **kwargs):
3131
raise LeanCloudError(code=1, error='Notification does not support modify')
3232

3333

34+
def _encode_time(time):
35+
tzinfo = time.tzinfo
36+
if tzinfo is None:
37+
tzinfo = tz.tzlocal()
38+
return arrow.get(time, tzinfo).to('utc').format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'
39+
40+
3441
def send(data, channels=None, push_time=None, expiration_time=None, expiration_interval=None, where=None, cql=None):
3542
"""
3643
发送推送消息。返回结果为此条推送对应的 _Notification 表中的对象,但是如果需要使用其中的数据,需要调用 fetch() 方法将数据同步至本地。
@@ -63,12 +70,9 @@ def send(data, channels=None, push_time=None, expiration_time=None, expiration_i
6370
if channels:
6471
params['channels'] = channels
6572
if push_time:
66-
tzinfo = push_time.tzinfo
67-
if tzinfo is None:
68-
tzinfo = tz.tzlocal()
69-
params['push_time'] = arrow.get(push_time, tzinfo).to('utc').format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'
73+
params['push_time'] = _encode_time(push_time)
7074
if expiration_time:
71-
params['expiration_time'] = expiration_time.isoformat()
75+
params['expiration_time'] = _encode_time(expiration_time)
7276
if expiration_interval:
7377
params['expiration_interval'] = expiration_interval
7478
if where:

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
'iso8601',
1111
'six>=1.11.0',
1212
'qiniu>=7.1.4,<7.2.4',
13-
'requests>=2.12.1',
13+
'requests>=2.20.0,<2.22.0',
14+
'urllib3>=1.24.3,<1.25.0'
1415
'werkzeug',
1516
'gevent>=1.0.2,<2.0.0'
1617
]

tests/test_push.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import os
88
import time
9-
from datetime import datetime
9+
from datetime import datetime, timedelta
1010

1111
from nose.tools import with_setup # type: ignore
1212

@@ -25,10 +25,10 @@ def setup_func():
2525

2626
@with_setup(setup_func)
2727
def test_basic_push(): # type: () -> None
28-
instanlation = leancloud.Installation()
29-
instanlation.set('deviceType', 'ios')
30-
instanlation.set('deviceToken', 'xxx')
31-
instanlation.save()
28+
installation = leancloud.Installation()
29+
installation.set('deviceType', 'ios')
30+
installation.set('deviceToken', 'xxx')
31+
installation.save()
3232

3333
data = {
3434
"alert": {
@@ -42,10 +42,12 @@ def test_basic_push(): # type: () -> None
4242
}
4343
}
4444
query = leancloud.Query('_Installation').equal_to('objectId', 'xxx')
45-
notification = push.send(data, where=query, push_time=datetime.now())
45+
now = datetime.now()
46+
two_hours_later = now + timedelta(hours=2)
47+
notification = push.send(data, where=query, push_time=now, expiration_time=two_hours_later)
4648
time.sleep(5) # notification write may have delay
4749
notification.fetch()
48-
assert(notification.id)
50+
assert notification.id
4951

5052
try:
5153
notification.save()

0 commit comments

Comments
 (0)