Skip to content

Commit 3032100

Browse files
committed
fix(push): expiration time encoding
Previously, the expiration time parameter of push `send` function was mistakenly encoded (e.g. `2016-01-28T00:07:29.773363+00:00`), not encoded as expected (e.g. `2016-01-28T00:07:29.773Z`). This commit fix this encoding issue.
1 parent 8808c48 commit 3032100

2 files changed

Lines changed: 13 additions & 7 deletions

File tree

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:

tests/test_push.py

Lines changed: 4 additions & 2 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

@@ -42,7 +42,9 @@ 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()
4850
assert(notification.id)

0 commit comments

Comments
 (0)