Commit be2dc8af authored by hupantingxue's avatar hupantingxue

1. Modify device tag/alias/registration_id entity name to avoid conflicting with push namespace;

2. Add device example;
parent 8a860036
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
device = _jpush.create_device()
reg_id = '090c1f59f89'
device.get_deviceinfo()
......@@ -22,6 +22,14 @@ from .push import (
message,
)
from .device import (
add,
remove,
device_tag,
device_alias,
device_regid,
)
__all__ = [
JPush,
JPushFailure,
......
......@@ -5,6 +5,11 @@ import logging
BASE_URL = "https://api.jpush.cn/"
PUSH_URL = BASE_URL + 'v3/push'
DEVICE_BASEURL = "https://device.jpush.cn/"
DEVICE_URL = DEVICE_BASEURL + "v3/device/"
TAG_URL = DEVICE_BASEURL + "v3/tag/"
ALIAS_URL = DEVICE_BASEURL + "v3/alias/"
logger = logging.getLogger('jpush')
class Unauthorized(Exception):
......
......@@ -6,6 +6,7 @@ import requests
from . import common
from .push import Push
from .devcie import Device
logger = logging.getLogger('jpush')
......@@ -63,3 +64,7 @@ class JPush(object):
def create_push(self):
"""Create a Push notification."""
return Push(self)
def create_device(self):
"""Create a Device information."""
return Device(self)
__author__ = "wuliang"
__email__ = "wuliang@jpush.cn"
from .core import Device
from .entity import (
add,
remove,
device_tag,
device_alias,
device_regid,
)
__all__ = [
Device,
add,
remove,
device_tag,
device_alias,
device_regid,
]
#!/usr/bin/env python
from jpush import common
class Device(object):
"""Device info query/update..
"""
def __init__(self):
def __init__(self, jpush):
self._jpush = jpush
self.entity = None
def send(self, method, url, body, params, content_type=None, version=3):
"""Send the request
class DeviceResponse(object):
"""
response = self._jpush._request(method, body,
url, content_type, version=3)
data = response.json()
return DeviceResponse(response)
def get_deviceinfo(self, registration_id):
"""Get deviceinfo with registration id
"""
url = common.DEVICE_URL + registration_id
info = self.send("GET", None, None)
print info
class DeviceResponse(object):
"""Response to a successful device request send.
Right now this is a fairly simple wrapper around the json payload response,
but making it an object gives us some flexibility to add functionality
later.
"""
payload = None
def __init__(self, response):
data = response.json()
self.payload = data
def __str__(self):
return "Device response Payload: {0}".format(self.payload)
......@@ -27,14 +27,14 @@ def remove(*types):
vremove = [v for v in types]
return {"remove": vremove}
def tag(*types):
def device_tag(*types):
"""Get a tag object
>>> tag("")
>>> device_tag("")
{'tag': ''}
>>> tag("tag1")
>>> device_tag("tag1")
{'tag': 'tag1'}
>>> tag(add("tag1", "tag2"), remove("tag3", "tag4"))
>>> device_tag(add("tag1", "tag2"), remove("tag3", "tag4"))
{'tag': {'add': ['tag1', 'tag2'], 'remove': ['tag3', 'tag4']}}
"""
tag = {}
......@@ -49,14 +49,14 @@ def tag(*types):
tag["tag"][key] = t[key]
return tag
def alias(*types):
def device_alias(*types):
"""Get an alias object
>>> alias("")
>>> device_alias("")
{'alias': ''}
>>> alias("alias1")
>>> device_alias("alias1")
{'alias': 'alias1'}
>>> alias(add("alias1", "alias2"), remove("alias3", "alias4"))
>>> device_alias(add("alias1", "alias2"), remove("alias3", "alias4"))
{'alias': {'add': ['alias1', 'alias2'], 'remove': ['alias3', 'alias4']}}
"""
alias = {}
......@@ -71,14 +71,14 @@ def alias(*types):
alias["alias"][key] = t[key]
return alias
def registration_id(*types):
def device_regid(*types):
"""Get a registration_id object
>>> registration_id("")
>>> device_regid("")
{'registration_id': ''}
>>> registration_id("registration_id1")
>>> device_regid("registration_id1")
{'registration_id': 'registration_id1'}
>>> registration_id(add("registration_id1", "registration_id2"), remove("registration_id3", "registration_id4"))
>>> device_regid(add("registration_id1", "registration_id2"), remove("registration_id3", "registration_id4"))
{'registration_id': {'add': ['registration_id1', 'registration_id2'], 'remove': ['registration_id3', 'registration_id4']}}
"""
registration_id = {}
......@@ -95,4 +95,4 @@ def registration_id(*types):
if "__main__" == __name__:
print add("1", "2")
print tag(add("a", "b"), remove('1', '2'))
print device_tag(add("a", "b"), remove('1', '2'))
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