Commit 1b3f5d9d authored by hupantingxue's avatar hupantingxue

Add audience payload;

parent 5fdb139b
......@@ -16,7 +16,9 @@ from .push import (
ios,
android,
winphone,
platform,
platform,
audience,
options,
message,
)
......@@ -36,6 +38,8 @@ __all__ = [
winphone,
message,
platform,
audience,
options,
]
# Silence urllib3 INFO logging by default
......
......@@ -14,6 +14,8 @@ from .payload import (
platform,
notification,
message,
audience,
options,
)
# Common selector for audience & platform
......@@ -24,7 +26,6 @@ all_ = "all"
Used in both ``audience`` and ``platform``.
"""
__all__ = [
all_,
Push,
......@@ -35,4 +36,6 @@ __all__ = [
notification,
message,
platform,
audience,
options,
]
......@@ -2,32 +2,22 @@ import re
# Value selectors; aliases, tags, etc.
def tag(tag):
def tag(*tags):
"""Select a single tag."""
return {"tag": tag}
vtag = [t for t in tags]
return {"tag": vtag}
def tag_and(tag_and):
def tag_and(*tag_ands):
"""Select a single tag_and."""
return {"tag_and": tag_and}
vtag_and = [t for t in tag_ands]
return {"tag_and": vtag_and}
def alias(alias):
def alias(*alias):
"""Select a single alias."""
return {"alias": alias}
valias = [t for t in alias]
return {"alias": valias}
def registration_id(registration_id):
def registration_id(*reg_ids):
"""Select a (list of) registration_id(s)."""
return {"registration_id": registration_id}
def audience(*children):
"""Select audience that match all of the given selectors.
>>> audience(tag('sports'), tag_and('business'))
{'audience': {'tag':'sports', 'tag_and':'business'}}
"""
if len(types) == 1 and types[0] == 'all':
return {'audience':'all'}
for t in types:
if t not in ('tag', 'tag_and', 'alias', 'registration_id'):
raise ValueError("Invalid platform '%s'" % t)
return [t for t in types]
vregistration_id = [t for t in reg_ids]
return {"registration_id": vregistration_id}
......@@ -84,7 +84,7 @@ def android(alert, title=None, builder_id=None, extras=None):
payload['title'] = title
if builder_id is not None:
payload['builder_id'] = builder_id
if extra is not None:
if extras is not None:
payload['extras'] = extras
return payload
......@@ -149,3 +149,20 @@ def platform(*types):
def options(options):
"""Create options object."""
return {"options": options}
def audience(*types):
"""Select audience that match all of the given selectors.
>>> audience(tag('sports'), tag_and('business'))
{'audience': {'tag':['sports'], 'tag_and':['business']}}
"""
if 1 == len(types) and 'all' == types[0]:
return "all"
audience = {}
for t in types:
for key in t:
if key not in ('tag', 'tag_and', 'alias', 'registration_id'):
raise ValueError("Invalid audience '%s'" % t)
audience[key] = t[key]
return audience
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment