Commit 99517e92 authored by hupantingxue's avatar hupantingxue

Add device api sample;

parent cf31124d
app_key = u'dd1066407b044738b6479275'
master_secret = u'2b38ce69b1de2a7fa95706ea'
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'
entity = jpush.device_tag("")
device.set_deviceinfo(reg_id, entity)
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(reg_id)
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
device = _jpush.create_device()
device.delete_tag(tag)
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
device = _jpush.create_device()
device.get_taglist()
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
device = _jpush.create_device()
device.get_taglist()
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
device = _jpush.create_device()
device.get_taglist()
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'
entity = jpush.device_tag(jpush.add("ddd", "tageee"))
device.set_deviceinfo(reg_id, entity)
...@@ -22,7 +22,7 @@ from .push import ( ...@@ -22,7 +22,7 @@ from .push import (
message, message,
) )
from device import ( from .device import (
Device, Device,
add, add,
remove, remove,
......
...@@ -8,6 +8,7 @@ PUSH_URL = BASE_URL + 'v3/push' ...@@ -8,6 +8,7 @@ PUSH_URL = BASE_URL + 'v3/push'
DEVICE_BASEURL = "https://device.jpush.cn/" DEVICE_BASEURL = "https://device.jpush.cn/"
DEVICE_URL = DEVICE_BASEURL + "v3/device/" DEVICE_URL = DEVICE_BASEURL + "v3/device/"
TAG_URL = DEVICE_BASEURL + "v3/tag/" TAG_URL = DEVICE_BASEURL + "v3/tag/"
TAGLIST_URL = TAG_URL + "list/"
ALIAS_URL = DEVICE_BASEURL + "v3/alias/" ALIAS_URL = DEVICE_BASEURL + "v3/alias/"
logger = logging.getLogger('jpush') logger = logging.getLogger('jpush')
......
...@@ -6,7 +6,7 @@ import requests ...@@ -6,7 +6,7 @@ import requests
from . import common from . import common
from .push import Push from .push import Push
from device import Device from .device import Device
logger = logging.getLogger('jpush') logger = logging.getLogger('jpush')
......
#!/usr/bin/env python #!/usr/bin/env python
#from jpush import common from jpush import common
import json
DEVICE_BASEURL = "https://device.jpush.cn/" DEVICE_BASEURL = "https://device.jpush.cn/"
DEVICE_URL = DEVICE_BASEURL + "v3/device/" DEVICE_URL = DEVICE_BASEURL + "v3/device/"
TAG_URL = DEVICE_BASEURL + "v3/tag/" TAG_URL = DEVICE_BASEURL + "v3/tag/"
TAGLIST_URL = TAG_URL + "list/"
ALIAS_URL = DEVICE_BASEURL + "v3/alias/" ALIAS_URL = DEVICE_BASEURL + "v3/alias/"
class Device(object): class Device(object):
...@@ -14,20 +16,46 @@ class Device(object): ...@@ -14,20 +16,46 @@ class Device(object):
self._jpush = jpush self._jpush = jpush
self.entity = None self.entity = None
def send(self, method, url, body, params, content_type=None, version=3): def send(self, method, url, body, content_type=None, version=3):
"""Send the request """Send the request
""" """
response = self._jpush._request(method, body, response = self._jpush._request(method, body,
url, content_type, version=3) url, content_type, version=3)
data = response.json()
return DeviceResponse(response) return DeviceResponse(response)
def get_taglist(self):
"""Get deviceinfo with registration id.
"""
url = common.TAGLIST_URL
info = self.send("GET", url, None)
print info
def get_deviceinfo(self, registration_id): def get_deviceinfo(self, registration_id):
"""Get deviceinfo with registration id """Get deviceinfo with registration id.
"""
url = common.DEVICE_URL + registration_id + "/"
info = self.send("GET", url, None)
print info
def set_deviceinfo(self, registration_id, entity):
"""Update deviceinfo with registration id.
"""
url = common.DEVICE_URL + registration_id + "/"
body = json.dumps(entity)
print url, body
info = self.send("POST", url, body)
print info
def delete_tag(self, registration_id, tag):
"""Delete registration id tag.
""" """
url = DEVICE_URL + registration_id url = common.DEVICE_URL + registration_id + "/"
info = self.send("GET", None, None) body = json.dumps(entity)
print url, body
info = self.send("POST", url, body)
print info print info
class DeviceResponse(object): class DeviceResponse(object):
...@@ -41,8 +69,11 @@ class DeviceResponse(object): ...@@ -41,8 +69,11 @@ class DeviceResponse(object):
payload = None payload = None
def __init__(self, response): def __init__(self, response):
data = response.json() if 0 != len(response.content):
self.payload = data data = response.json()
self.payload = data
elif 200 == response.status_code:
self.payload = "success"
def __str__(self): def __str__(self):
return "Device response Payload: {0}".format(self.payload) return "Device response Payload: {0}".format(self.payload)
...@@ -18,7 +18,7 @@ setup( ...@@ -18,7 +18,7 @@ setup(
author='jpush', author='jpush',
author_email='support@jpush.cn', author_email='support@jpush.cn',
packages=['jpush', 'jpush.push'], packages=['jpush', 'jpush.push', 'jpush.device'],
platforms='any', platforms='any',
classifiers=[ classifiers=[
'Environment :: Console', 'Environment :: Console',
......
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