Skip to content

Commit af628d5

Browse files
committed
feat(push): support flow_control
see also leancloud/docs#3329
1 parent e360823 commit af628d5

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

leancloud/push.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def _encode_time(time):
3838
return arrow.get(time, tzinfo).to('utc').format('YYYY-MM-DDTHH:mm:ss.SSS') + 'Z'
3939

4040

41-
def send(data, channels=None, push_time=None, expiration_time=None, expiration_interval=None, where=None, cql=None):
41+
def send(data, channels=None, push_time=None, expiration_time=None, expiration_interval=None, where=None, cql=None, flow_control=None):
4242
"""
4343
发送推送消息。返回结果为此条推送对应的 _Notification 表中的对象,但是如果需要使用其中的数据,需要调用 fetch() 方法将数据同步至本地。
4444
@@ -56,6 +56,8 @@ def send(data, channels=None, push_time=None, expiration_time=None, expiration_i
5656
:type cql: string_types
5757
:param data: 推送给设备的具体信息,详情查看 https://leancloud.cn/docs/push_guide.html#消息内容_Data
5858
:rtype: Notification
59+
:param flow_control: 不为 None 时开启平滑推送,值为每秒推送的目标终端用户数。开启时指定低于 1000 的值,按 1000 计。
60+
:type: flow_control: int
5961
"""
6062
if expiration_interval and expiration_time:
6163
raise TypeError('Both expiration_time and expiration_interval can\'t be set')
@@ -79,6 +81,10 @@ def send(data, channels=None, push_time=None, expiration_time=None, expiration_i
7981
params['where'] = where.dump().get('where', {})
8082
if cql:
8183
params['cql'] = cql
84+
# Do not change this to `if flow_control`, because 0 is falsy in Python,
85+
# but `flow_control = 0` will enable smooth push, and it is in fact equivlent to `flow_control = 1000`.
86+
if flow_control is not None:
87+
params['flow_control'] = flow_control
8288

8389
result = client.post('/push', params=params).json()
8490

tests/test_push.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ def test_basic_push(): # type: () -> None
4444
query = leancloud.Query('_Installation').equal_to('objectId', 'xxx')
4545
now = datetime.now()
4646
two_hours_later = now + timedelta(hours=2)
47-
notification = push.send(data, where=query, push_time=now, expiration_time=two_hours_later)
47+
notification = push.send(data, where=query, push_time=now, expiration_time=two_hours_later, flow_control=0)
48+
# flow_control = 0 <=> flow_control = 1000 by rest api design
4849
time.sleep(5) # notification write may have delay
4950
notification.fetch()
5051
assert notification.id

0 commit comments

Comments
 (0)