From fd3d3c1a204eb540c2a9175282cce6ef6b2b8b21 Mon Sep 17 00:00:00 2001 From: fendouai Date: Thu, 13 Oct 2016 14:55:53 +0800 Subject: [PATCH 1/3] jpush python http2 --- README.md | 5 +- examples/device_examples/conf.py | 2 +- examples/device_examples/example_clrtag.py | 2 +- examples/device_examples/example_getdevice.py | 2 +- examples/device_examples/example_tagexist.py | 4 +- examples/device_examples/example_updevice.py | 5 +- .../device_examples/example_updevicemobile.py | 4 +- examples/push_examples/conf.py | 2 +- examples/push_examples/example_all.py | 12 +---- examples/report_examples/conf.py | 2 +- examples/report_examples/example_messages.py | 2 +- examples/report_examples/example_received.py | 2 +- examples/report_examples/example_users.py | 2 +- examples/schedule_examples/conf.py | 2 +- .../schedule_examples/example_get_schedule.py | 2 +- .../example_post_schedule.py | 4 +- jpush/common.py | 26 +++++----- jpush/core.py | 39 +++++++------- jpush/device/core.py | 48 ++++++++++++------ jpush/push/core.py | 28 ++-------- jpush/report/core.py | 12 ++--- jpush/schedule/core.py | 18 ++++--- requirements.txt | 3 ++ tests/conf.py | 2 +- tests/devices/conf.py | 2 +- tests/devices/test_devices.py | 22 ++++---- tests/myapp.log | Bin 0 -> 96266 bytes tests/schedule/test_schedule.py | 14 ++--- 28 files changed, 129 insertions(+), 139 deletions(-) create mode 100644 requirements.txt create mode 100644 tests/myapp.log diff --git a/README.md b/README.md index e722085..62ef8e7 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ # JPush API Python Client ## 概述 -这是 JPush REST API 的 Python 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能。 +这是 JPush REST API 的 Python 版本封装开发包,依赖 Hyper: HTTP/2 Client for Python。 + +Hyper: https://github.com/Lukasa/hyper 对应的 REST API 文档: ## 兼容版本 + Python 2.7 -+ Python 3 ## 环境配置 diff --git a/examples/device_examples/conf.py b/examples/device_examples/conf.py index 5b8764c..3fdc12c 100644 --- a/examples/device_examples/conf.py +++ b/examples/device_examples/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' diff --git a/examples/device_examples/example_clrtag.py b/examples/device_examples/example_clrtag.py index 400047f..1d9bc4b 100644 --- a/examples/device_examples/example_clrtag.py +++ b/examples/device_examples/example_clrtag.py @@ -3,6 +3,6 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") device = _jpush.create_device() -reg_id = '090c1f59f89' +reg_id = '160a3797c80d93ce897' entity = jpush.device_tag("") device.set_deviceinfo(reg_id, entity) diff --git a/examples/device_examples/example_getdevice.py b/examples/device_examples/example_getdevice.py index d9afb2f..4fb5751 100644 --- a/examples/device_examples/example_getdevice.py +++ b/examples/device_examples/example_getdevice.py @@ -3,5 +3,5 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") device = _jpush.create_device() -reg_id = '090c1f59f89' +reg_id = '160a3797c80d93ce897' device.get_deviceinfo(reg_id) diff --git a/examples/device_examples/example_tagexist.py b/examples/device_examples/example_tagexist.py index 22a73dc..3321f0e 100644 --- a/examples/device_examples/example_tagexist.py +++ b/examples/device_examples/example_tagexist.py @@ -4,5 +4,5 @@ _jpush.set_logging("DEBUG") device = _jpush.create_device() tag = "ddd" -registration_id = '090c1f59f89' -device.check_taguserexist(tag, registration_id) +reg_id = '160a3797c80d93ce897' +device.check_taguserexist(tag, reg_id) diff --git a/examples/device_examples/example_updevice.py b/examples/device_examples/example_updevice.py index b08f3de..7d5e825 100644 --- a/examples/device_examples/example_updevice.py +++ b/examples/device_examples/example_updevice.py @@ -3,8 +3,7 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") device = _jpush.create_device() -reg_id = '1507bfd3f7c466c355c' +reg_id = '160a3797c80d93ce897' entity = jpush.device_tag(jpush.add("ddd", "tageee")) result=device.set_devicemobile(reg_id, entity) -print (result.status_code) -print (result.payload) \ No newline at end of file +print (result.status_code) \ No newline at end of file diff --git a/examples/device_examples/example_updevicemobile.py b/examples/device_examples/example_updevicemobile.py index f24b027..6b511e3 100644 --- a/examples/device_examples/example_updevicemobile.py +++ b/examples/device_examples/example_updevicemobile.py @@ -3,6 +3,6 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") device = _jpush.create_device() -reg_id = '1507bfd3f7c466c355c' -entity = jpush.device_mobile("18588232140") +reg_id = '160a3797c80d93ce897' +entity = jpush.device_mobile("13488888888") device.set_devicemobile(reg_id, entity) diff --git a/examples/push_examples/conf.py b/examples/push_examples/conf.py index 5b8764c..3fdc12c 100644 --- a/examples/push_examples/conf.py +++ b/examples/push_examples/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' diff --git a/examples/push_examples/example_all.py b/examples/push_examples/example_all.py index 0aa760d..d6fad79 100644 --- a/examples/push_examples/example_all.py +++ b/examples/push_examples/example_all.py @@ -1,6 +1,5 @@ import jpush as jpush from conf import app_key, master_secret -from jpush import common _jpush = jpush.JPush(app_key, master_secret) @@ -11,13 +10,4 @@ push.audience = jpush.all_ push.notification = jpush.notification(alert="hello python jpush api") push.platform = jpush.all_ -try: - response=push.send() -except common.Unauthorized: - raise common.Unauthorized("Unauthorized") -except common.APIConnectionException: - raise common.APIConnectionException("conn") -except common.JPushFailure: - print ("JPushFailure") -except: - print ("Exception") \ No newline at end of file +push.send() diff --git a/examples/report_examples/conf.py b/examples/report_examples/conf.py index 5b8764c..3fdc12c 100644 --- a/examples/report_examples/conf.py +++ b/examples/report_examples/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' diff --git a/examples/report_examples/example_messages.py b/examples/report_examples/example_messages.py index 94d3039..e2e8f5d 100644 --- a/examples/report_examples/example_messages.py +++ b/examples/report_examples/example_messages.py @@ -3,4 +3,4 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") report=_jpush.create_report(); -report.get_messages("3289406737") \ No newline at end of file +print (report.get_messages("3289406737")) \ No newline at end of file diff --git a/examples/report_examples/example_received.py b/examples/report_examples/example_received.py index 7131d5b..a34674f 100644 --- a/examples/report_examples/example_received.py +++ b/examples/report_examples/example_received.py @@ -3,4 +3,4 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") report=_jpush.create_report(); -report.get_received("3289406737") \ No newline at end of file +print (report.get_received("3951136871")) \ No newline at end of file diff --git a/examples/report_examples/example_users.py b/examples/report_examples/example_users.py index 0a4ae79..4642cdb 100644 --- a/examples/report_examples/example_users.py +++ b/examples/report_examples/example_users.py @@ -3,4 +3,4 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") report=_jpush.create_report(); -report.get_users("DAY","2016-04-10","3") \ No newline at end of file +print (report.get_users("DAY","2017-04-10","3")) \ No newline at end of file diff --git a/examples/schedule_examples/conf.py b/examples/schedule_examples/conf.py index 5b8764c..3fdc12c 100644 --- a/examples/schedule_examples/conf.py +++ b/examples/schedule_examples/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' diff --git a/examples/schedule_examples/example_get_schedule.py b/examples/schedule_examples/example_get_schedule.py index 67f44bf..97f8421 100644 --- a/examples/schedule_examples/example_get_schedule.py +++ b/examples/schedule_examples/example_get_schedule.py @@ -3,4 +3,4 @@ _jpush = jpush.JPush(app_key, master_secret) _jpush.set_logging("DEBUG") schedule = _jpush.create_schedule() -schedule.get_schedule_by_id("e9c553d0-0850-11e6-b6d4-0021f652c102") +schedule.get_schedule_by_id("8f2951c8-86cd-11e6-ae78-0021f653c902") diff --git a/examples/schedule_examples/example_post_schedule.py b/examples/schedule_examples/example_post_schedule.py index b92728e..9351033 100644 --- a/examples/schedule_examples/example_post_schedule.py +++ b/examples/schedule_examples/example_post_schedule.py @@ -11,7 +11,7 @@ push.platform = jpush.all_ push=push.payload -trigger=jpush.schedulepayload.trigger("2016-07-17 12:00:00") +trigger=jpush.schedulepayload.trigger("2017-07-17 12:00:00") schedulepayload=jpush.schedulepayload.schedulepayload("name",True,trigger,push) result=schedule.post_schedule(schedulepayload) -print (result.status_code) +print (result) diff --git a/jpush/common.py b/jpush/common.py index e3c1145..a4af66f 100644 --- a/jpush/common.py +++ b/jpush/common.py @@ -2,23 +2,23 @@ import logging import requests -BASE_URL = "https://api.jpush.cn/" -PUSH_URL = BASE_URL + 'v3/push' -VALIDATE_PUSH_URL = BASE_URL + 'v3/push/validate' +PUSH_BASEURL = "api.jpush.cn" +PUSH_URL = '/v3/push' +VALIDATE_PUSH_URL = '/v3/push/validate' -DEVICE_BASEURL = "https://device.jpush.cn/" -DEVICE_URL = DEVICE_BASEURL + "v3/devices/" -TAG_URL = DEVICE_BASEURL + "v3/tags/" +DEVICE_BASEURL = "device.jpush.cn" +DEVICE_URL = "/v3/devices/" +TAG_URL = "/v3/tags/" TAGLIST_URL = TAG_URL -ALIAS_URL = DEVICE_BASEURL + "v3/aliases/" +ALIAS_URL = "/v3/aliases/" -REPORT_BASEURL="https://report.jpush.cn/" -RECEIVED_URL=REPORT_BASEURL+"v3/received?msg_ids=" -MESSAGES_URL=REPORT_BASEURL+"v3/messages?msg_ids=" -USERS_URL=REPORT_BASEURL+"v3/users?" +REPORT_BASEURL="report.jpush.cn" +RECEIVED_URL="/v3/received?msg_ids=" +MESSAGES_URL="/v3/messages?msg_ids=" +USERS_URL="/v3/users?" -BASE_SCHEDULEURL="https://api.jpush.cn/v3/schedules/" -BASE_LISTURL="https://api.jpush.cn/v3/schedules?page=" +BASE_SCHEDULEURL="/v3/schedules/" +BASE_LISTURL="/v3/schedules?page=" logger = logging.getLogger('jpush') diff --git a/jpush/core.py b/jpush/core.py index 47770fb..cdb2078 100644 --- a/jpush/core.py +++ b/jpush/core.py @@ -1,8 +1,9 @@ import json import logging import warnings - +import base64 import requests +from hyper import HTTP20Connection from . import common from .push import Push from .device import Device @@ -20,7 +21,7 @@ def __init__(self, key, secret): self.session = requests.Session() self.session.auth = (key, secret) - def _request(self, method, body, url, content_type=None, version=None, params=None): + def _request(self, method, body, url,base_url=None): headers = {} headers['user-agent'] = 'jpush-api-python-client' headers['connection'] = 'keep-alive' @@ -28,21 +29,18 @@ def _request(self, method, body, url, content_type=None, version=None, params=No logger.debug("Making %s request to %s. Headers:\n\t%s\nBody:\n\t%s", method, url, '\n\t'.join('%s: %s' % (key, value) for (key, value) in headers.items()), body) - try: - response = self.session.request(method, url, data=body, params=params, headers=headers, timeout=30) - except requests.exceptions.ConnectTimeout: - raise common.APIConnectionException("Connection to api.jpush.cn timed out.") - except: - raise common.APIConnectionException("Connection to api.jpush.cn error.") - - logger.debug("Received %s response. Headers:\n\t%s\nBody:\n\t%s", response.status_code, '\n\t'.join( - '%s: %s' % (key, value) for (key, value) in response.headers.items()), response.content) - - if response.status_code == 401: - raise common.Unauthorized("Please check your AppKey and Master Secret") - elif not (200 <= response.status_code < 300): - raise common.JPushFailure.from_response(response) - return response + base64string = base64.encodestring('%s:%s' % (self.key, self.secret))[:-1] + authheader = "Basic %s" % base64string + headers['Authorization'] = authheader + conn = HTTP20Connection(host=base_url, secure=True) + + response = conn.request(method,url,headers=headers,body=body) + resp = conn.get_response(response) + #add status_code to test + resp.status_code=resp.status + logger.debug(resp.status) + logger.debug(resp.read()) + return resp def push(self, payload): """Push this payload to the specified recipients. @@ -50,11 +48,8 @@ def push(self, payload): Payload: a dictionary the contents to send, e.g.: {'aps': {'alert': 'Hello'}, 'android': {'alert': 'Hello'}} """ - warnings.warn( - "JPush.push() is deprecated. See documentation on upgrading.", - DeprecationWarning) body = json.dumps(payload) - self._request('POST', body, common.PUSH_URL, 'application/json', version=1) + self._request('POST', body, common.PUSH_URL, common.PUSH_BASEURL) def set_logging(self, level): level_list= ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"] @@ -88,4 +83,4 @@ def create_report(self): def create_schedule(self): """Create a Schedule.""" - return Schedule(self) + return Schedule(self) \ No newline at end of file diff --git a/jpush/device/core.py b/jpush/device/core.py index 6c027e8..51c1d58 100644 --- a/jpush/device/core.py +++ b/jpush/device/core.py @@ -1,7 +1,11 @@ #!/usr/bin/env python from jpush import common import json +import base64 +from hyper import HTTP20Connection +import logging +logger = logging.getLogger('jpush') class Device(object): """Device info query/update.. @@ -11,19 +15,33 @@ def __init__(self, jpush): self._jpush = jpush self.entity = None - def send(self, method, url, body, content_type=None, version=3): - """Send the request - - """ - response = self._jpush._request(method, body, url, content_type, version=3) - return DeviceResponse(response) + + def _request(self, method, body, url,base_url=None): + headers = {} + headers['user-agent'] = 'jpush-api-python-client' + headers['connection'] = 'keep-alive' + headers['content-type'] = 'application/json;charset:utf-8' + + logger.debug("Making %s request to %s. Headers:\n\t%s\nBody:\n\t%s", + method, url, '\n\t'.join('%s: %s' % (key, value) for (key, value) in headers.items()), body) + base64string = base64.encodestring('%s:%s' % (self.key, self.secret))[:-1] + authheader = "Basic %s" % base64string + headers['Authorization'] = authheader + conn = HTTP20Connection(host=base_url, secure=True) + + response = conn.request(method,url,headers=headers,body=body) + resp = conn.get_response(response) + logger.debug(resp.status) + logger.debug(resp.read()) + return resp + def get_taglist(self): """Get deviceinfo with registration id. """ url = common.TAGLIST_URL body = None - info = self.send("GET", url, body) + info = self._jpush._request('GET',body,url,base_url=common.DEVICE_BASEURL) return info def get_deviceinfo(self, registration_id): @@ -31,7 +49,7 @@ def get_deviceinfo(self, registration_id): """ url = common.DEVICE_URL + registration_id + "/" body = None - info = self.send("GET", url, body) + info = self._jpush._request('GET', body, url, base_url=common.DEVICE_BASEURL) return info def set_deviceinfo(self, registration_id, entity): @@ -39,7 +57,7 @@ def set_deviceinfo(self, registration_id, entity): """ url = common.DEVICE_URL + registration_id + "/" body = json.dumps(entity) - info = self.send("POST", url, body) + info = self._jpush._request('POST', body, url, base_url=common.DEVICE_BASEURL) return info def set_devicemobile(self, registration_id, entity): @@ -47,7 +65,7 @@ def set_devicemobile(self, registration_id, entity): """ url = common.DEVICE_URL + registration_id + "/" body = json.dumps(entity) - info = self.send("POST", url, body) + info = self._jpush._request('POST', body, url, base_url=common.DEVICE_BASEURL) return info def delete_tag(self, tag, platform=None): @@ -57,7 +75,7 @@ def delete_tag(self, tag, platform=None): body = None if platform: body = platform - info = self.send("DELETE", url, body) + info = self._jpush._request('DELETE', body, url, base_url=common.DEVICE_BASEURL) return info def update_tagusers(self, tag, entity): @@ -65,7 +83,7 @@ def update_tagusers(self, tag, entity): """ url = common.TAG_URL + tag + "/" body = json.dumps(entity) - info = self.send("POST", url, body) + info = self._jpush._request('POST', body, url, base_url=common.DEVICE_BASEURL) return info def check_taguserexist(self, tag, registration_id): @@ -73,7 +91,7 @@ def check_taguserexist(self, tag, registration_id): """ url = common.TAG_URL + tag + "/registration_ids/" + registration_id body = registration_id - info = self.send("GET", url, body) + info = self._jpush._request('GET', body, url, base_url=common.DEVICE_BASEURL) return info def delete_alias(self, alias, platform=None): @@ -83,7 +101,7 @@ def delete_alias(self, alias, platform=None): body = None if platform: body = platform - info = self.send("DELETE", url, body) + info = self._jpush._request('DELETE', body, url, base_url=common.DEVICE_BASEURL) return info def get_aliasuser(self, alias, platform=None): @@ -93,7 +111,7 @@ def get_aliasuser(self, alias, platform=None): body = None if platform: body = platform - info = self.send("GET", url, body) + info = self._jpush._request('GET', body, url, base_url=common.DEVICE_BASEURL) return info diff --git a/jpush/push/core.py b/jpush/push/core.py index 391afa3..7b7ea27 100644 --- a/jpush/push/core.py +++ b/jpush/push/core.py @@ -45,8 +45,8 @@ def send(self): """ body = json.dumps(self.payload) - response = self._jpush._request('POST', body, common.PUSH_URL, 'application/json', version=3) - return PushResponse(response) + response = self._jpush._request('POST',body,common.PUSH_URL,base_url=common.PUSH_BASEURL) + return response def send_validate(self): """Send the notification to validate. @@ -58,28 +58,8 @@ def send_validate(self): """ body = json.dumps(self.payload) - response = self._jpush._request('POST', body, common.VALIDATE_PUSH_URL, 'application/json', version=3) - return PushResponse(response) + response = self._jpush._request('POST', body, common.VALIDATE_PUSH_URL, base_url=common.PUSH_BASEURL) + return response -class PushResponse(object): - """Response to a successful push notification 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 - status_code = None - - def __init__(self, response): - self.status_code = response.status_code - data = response.json() - self.payload = data - - def get_status_code(self): - return self.status_code - - def __str__(self): - return "Response Payload: {0}".format(self.payload) diff --git a/jpush/report/core.py b/jpush/report/core.py index cf7ecaa..f80f11c 100644 --- a/jpush/report/core.py +++ b/jpush/report/core.py @@ -9,28 +9,28 @@ class Report(object): def __init__(self, jpush): self._jpush = jpush - def send(self, method, url, body, content_type=None, version=3): + def send(self, method, body,url,base_url=None): """Send the request """ - response = self._jpush._request(method, body,url,content_type,version=3) - return ReportResponse(response) + response = self._jpush._request(method, body,url,common.REPORT_BASEURL) + return response def get_received(self,msg_ids): url=common.RECEIVED_URL+msg_ids body = None - received = self.send("GET", url, body) + received = self.send("GET", body, url ,common.REPORT_BASEURL) return received def get_messages(self, msg_ids): url = common.MESSAGES_URL + msg_ids body = None - messages = self.send("GET", url, body) + messages = self.send("GET", body, url, common.REPORT_BASEURL) return messages def get_users(self, time_unit,start,duration): url = common.USERS_URL + "time_unit="+time_unit+"&start="+start+"&duration="+duration body = None - users = self.send("GET", url, body) + users = self.send("GET", body, url, common.REPORT_BASEURL) return users diff --git a/jpush/schedule/core.py b/jpush/schedule/core.py index 33ce507..6c0d201 100644 --- a/jpush/schedule/core.py +++ b/jpush/schedule/core.py @@ -10,20 +10,22 @@ class Schedule(object): def __init__(self, jpush): self._jpush = jpush - def send(self, method, url, body, content_type=None, version=3): - response = self._jpush._request(method, body, url, content_type, version=3) - return ScheduleResponse(response) + def send(self, method, body,url,base_url=None): + """Send the request + """ + response = self._jpush._request(method, body,url,common.PUSH_BASEURL) + return response def post_schedule(self, schedulepayload): url=common.BASE_SCHEDULEURL body = json.dumps(schedulepayload) - result = self.send("POST", url, body) + result = self.send("POST", body, url) return result def get_schedule_by_id(self, schedule_id): url=common.BASE_SCHEDULEURL + schedule_id body = None - result = self.send("GET", url, body) + result = self.send("GET", body, url) return result def get_schedule_list(self, page_id): @@ -32,19 +34,19 @@ def get_schedule_list(self, page_id): else: url = common.BASE_LISTURL body = None - result = self.send("GET", url, body) + result = self.send("GET",body,url) return result def put_schedule(self, schedulepayload, schedule_id): url = common.BASE_SCHEDULEURL + schedule_id body = json.dumps(schedulepayload) - result = self.send("PUT", url, body) + result = self.send("PUT", body,url) return result def delete_schedule(self,schedule_id): url = common.BASE_SCHEDULEURL + schedule_id body = None - result = self.send("DELETE", url, body) + result = self.send("DELETE",body,url) return result diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..1feaff5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +hyper==0.7.0 +requests==2.10.0 +setuptools==23.1.0 diff --git a/tests/conf.py b/tests/conf.py index 5b8764c..3fdc12c 100644 --- a/tests/conf.py +++ b/tests/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' diff --git a/tests/devices/conf.py b/tests/devices/conf.py index 5b8764c..c210c48 100644 --- a/tests/devices/conf.py +++ b/tests/devices/conf.py @@ -1,3 +1,3 @@ # please put your app_key and master_secret here app_key = u'6be9204c30b9473e87bad4dc' -master_secret = u'9349ad7c90292a603c512e92' +master_secret = u'e62664ad421b67270e5c9d5b' \ No newline at end of file diff --git a/tests/devices/test_devices.py b/tests/devices/test_devices.py index 1f72dcb..3bf77ea 100644 --- a/tests/devices/test_devices.py +++ b/tests/devices/test_devices.py @@ -7,12 +7,14 @@ _jpush = jpush.JPush(app_key, master_secret) device = _jpush.create_device() -_jpush.set_logging("DEBUG") +import logging + +logging.basicConfig(level=logging.DEBUG,filename='myapp.log',filemode='w') class TestEntity(unittest.TestCase): def test_create_device(self): - reg_id = '1507bfd3f7c466c355c' + reg_id = '160a3797c80d93ce897' entity = jpush.device_tag(jpush.add("ddd", "tageee")) result = device.set_devicemobile(reg_id, entity) self.assertEqual(result.status_code, 200) @@ -24,7 +26,7 @@ def test_aliasuser(self): self.assertEqual(result.status_code, 200) def test_clear_tag(self): - reg_id = '090c1f59f89' + reg_id = '160a3797c80d93ce897' entity = jpush.device_tag("") try: device.set_deviceinfo(reg_id, entity) @@ -34,7 +36,7 @@ def test_clear_tag(self): self.assertEqual(1, 0) def test_get_device(self): - reg_id = '090c1f59f89' + reg_id = '160a3797c80d93ce897' try: device.get_deviceinfo(reg_id) except common.JPushFailure: @@ -56,7 +58,7 @@ def test_remove_tags(self): def test_tag_exist(self): tag = "ddd" - registration_id = '090c1f59f89' + registration_id = '160a3797c80d93ce897' result = device.check_taguserexist(tag, registration_id) self.assertEqual(result.status_code, 200) @@ -66,18 +68,18 @@ def test_tag_list(self): def test_update_tagusers(self): tag = "ddd" - entity = jpush.device_regid(jpush.add("090c1f59f89")) + entity = jpush.device_regid(jpush.add("160a3797c80d93ce897")) result = device.update_tagusers(tag, entity) self.assertEqual(result.status_code, 200) def test_set_device_mobile(self): - reg_id = '1507bfd3f7c466c355c' - entity = jpush.device_tag(jpush.add("ddd", "tageee")) + reg_id = '160a3797c80d93ce897' + entity = jpush.device_tag(jpush.add("ddd", "tag")) result = device.set_devicemobile(reg_id, entity) self.assertEqual(result.status_code, 200) def test_device_mobile(self): - reg_id = '1507bfd3f7c466c355c' - entity = jpush.device_mobile("18588232140") + reg_id = '160a3797c80d93ce897' + entity = jpush.device_mobile("18588888888") result = device.set_devicemobile(reg_id, entity) self.assertEqual(result.status_code, 200) \ No newline at end of file diff --git a/tests/myap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Bw%s*p9-6*1NOsHO6S2@}TzeKyE3SFmP(cUHYY35dJ{bwiE4P>Lr9dQI*Eu&ABbc|;naQ60w4STGpME9DoZu+JDDykIcK z&T6+k=61_k_~0{e_H(|}YmA*;yDv;QmvZmPz}9EWH{p!2bD$b>n2o+v-Y AF#rGn literal 0 HcmV?d00001 diff --git a/tests/schedule/test_schedule.py b/tests/schedule/test_schedule.py index 848e459..de62505 100644 --- a/tests/schedule/test_schedule.py +++ b/tests/schedule/test_schedule.py @@ -17,25 +17,25 @@ class TestEntity(unittest.TestCase): def test_post_schedule(self): - trigger = jpush.schedulepayload.trigger("2016-07-17 12:00:00") + trigger = jpush.schedulepayload.trigger("2016-12-17 12:00:00") schedulepayload = jpush.schedulepayload.schedulepayload("name", True, trigger, push) result = schedule.post_schedule(schedulepayload) self.assertEqual(result.status_code, 200) def test_post_schedule_periodical(self): - trigger = jpush.schedulepayload.trigger("12:00:00",start="2016-07-17 12:00:00", end="2016-09-17 12:00:00", + trigger = jpush.schedulepayload.trigger("12:00:00",start="2016-07-17 12:00:00", end="2016-12-17 12:00:00", time_unit = "WEEK", frequency = 1, point = ["WED","FRI"]) schedulepayload = jpush.schedulepayload.schedulepayload("periodical", True, trigger, push) result = schedule.post_schedule(schedulepayload) self.assertEqual(result.status_code, 200) def test_get_schedule_by_id(self): - result = schedule.get_schedule_by_id("3fc6e2fa-15a6-11e6-83d4-0021f653c902") + result = schedule.get_schedule_by_id("a7d5ceea-8e98-11e6-8ea0-0021f652c102") self.assertEqual(result.status_code, 200) def test_get_schedule_by_invalid_id(self): try: - result = schedule.get_schedule_by_id("3fc6e2fa-15a6-11e6-83d4-0021f653c222") + result = schedule.get_schedule_by_id("3fc6e2fa-15a6-11e6-03d4-0021f653c222") self.assertNotEqual(result.status_code, 200) except common.JPushFailure as e: self.assertIsInstance(e, common.JPushFailure) @@ -48,7 +48,7 @@ def test_get_schedule_list(self): pass def test_put_invalid_schedule(self): - trigger = jpush.schedulepayload.trigger("2016-07-17 12:00:00") + trigger = jpush.schedulepayload.trigger("2016-12-17 12:00:00") schedulepayload = jpush.schedulepayload.schedulepayload("update a new name", True, trigger, push) try: result = schedule.put_schedule(schedulepayload, "3fc6e2fa-15a6-11e6-83d4-0021f653c902") @@ -57,7 +57,7 @@ def test_put_invalid_schedule(self): pass def test_put_schedule(self): - trigger = jpush.schedulepayload.trigger("2016-07-17 12:00:00") + trigger = jpush.schedulepayload.trigger("2016-12-17 12:00:00") schedulepayload = jpush.schedulepayload.schedulepayload("update_a_new_name", True, trigger, push) try: result = schedule.put_schedule(schedulepayload, "3fc6e2fa-15a6-11e6-83d4-0021f653c902") @@ -67,7 +67,7 @@ def test_put_schedule(self): def test_delete_schedule(self): try: - result = schedule.delete_schedule("e9c553d0-0850-11e6-b6d4-0021f652c102") + result = schedule.delete_schedule("59272e6a-8e98-11e6-85a9-0021f653c902") self.assertNotEqual(result.status_code, 200) except common.JPushFailure as e: self.assertIsInstance(e, jpush.common.JPushFailure) From 6baf587c04e0caf51514fabc2fbf09d47c9419e9 Mon Sep 17 00:00:00 2001 From: fendouai Date: Thu, 13 Oct 2016 14:59:24 +0800 Subject: [PATCH 2/3] fix the readme --- README.md | 61 ------------------------------------------------------- 1 file changed, 61 deletions(-) diff --git a/README.md b/README.md index 62ef8e7..4ab9125 100644 --- a/README.md +++ b/README.md @@ -10,67 +10,6 @@ Hyper: https://github.com/Lukasa/hyper ## 兼容版本 + Python 2.7 -## 环境配置 - -pip 方式: -``` -sudo pip install jpush -``` -easy_install 方式: -``` -sudo easy_install jpush -``` -使用源码方式: -``` -sudo python setup.py install -``` - - -## 代码样例 - -> 代码样例在 jpush-api-python-client 中的 examples 文件夹中,[点击查看所有 examples ](https://github.com/jpush/jpush-api-python-client/tree/master/examples) 。 - -> 以下片断来自项目代码里的文件:jpush-api-python-client 中的 examples/push_examples 目录下的 example_all.py - -> 这个样例演示了消息推送,日志设置,异常处理。 - -``` -_jpush = jpush.JPush(app_key, master_secret) -push = _jpush.create_push() -# if you set the logging level to "DEBUG",it will show the debug logging. -_jpush.set_logging("DEBUG") -push.audience = jpush.all_ -push.notification = jpush.notification(alert="hello python jpush api") -push.platform = jpush.all_ -try: - response=push.send() -except common.Unauthorized: - raise common.Unauthorized("Unauthorized") -except common.APIConnectionException: - raise common.APIConnectionException("conn error") -except common.JPushFailure: - print ("JPushFailure") -except: - print ("Exception") -``` -## 日志说明 -logging level 默认的是 WARNING ,为了方便调试建议设置为 DEBUG -设置方法为: -``` -_jpush.set_logging("DEBUG") -``` - -## 异常说明 - -+ Unauthorized - + AppKey,Master Secret 错误,验证失败必须改正。 - -+ APIConnectionException - + 包含错误的信息:比如超时,无网络等情况。 - -+ JPushFailure - + 请求出错,参考业务返回码。 - ## HTTP 状态码 参考文档: From b2f523f2108863e212d70b9c95a8dad78373fa2c Mon Sep 17 00:00:00 2001 From: fendouai Date: Wed, 28 Dec 2016 15:03:30 +0800 Subject: [PATCH 3/3] fix the description --- jpush/push/payload.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/jpush/push/payload.py b/jpush/push/payload.py index 3fcdf2f..87e3fb7 100644 --- a/jpush/push/payload.py +++ b/jpush/push/payload.py @@ -70,9 +70,9 @@ def ios(alert=None, badge=None, sound=None, content_available=False, def android(alert, title=None, builder_id=None, extras=None): """Android specific platform override payload. - All keyword arguments are optional. - - :keyword alert: String alert text. + :keyword alert: String alert text.If you set alert to a empty string,then the payload + will not display on notification bar. + more info:https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification :keyword title: String :keyword builder_id: Integer :keyword extras: A set of key/value pairs to include in the push payload