Commit 689d0d08 authored by Helperhaps's avatar Helperhaps

add zone

parent bd5b8772
......@@ -2,16 +2,31 @@ import json
import logging
import requests
PUSH_URL = 'https://api.jpush.cn/v3/'
REPORT_URL = 'https://report.jpush.cn/v3/'
DEVICE_URL = 'https://device.jpush.cn/v3/devices/'
ALIAS_URL = 'https://device.jpush.cn/v3/aliases/'
TAG_URL ='https://device.jpush.cn/v3/tags/'
SCHEDULE_URL = 'https://api.jpush.cn/v3/schedules/'
ADMIN_URL ='https://admin.jpush.cn/v1/'
ZONES = {
'DEFAULT': {
'PUSH': 'https://api.jpush.cn/v3/',
'REPORT': 'https://report.jpush.cn/v3/',
'DEVICE': 'https://device.jpush.cn/v3/devices/',
'ALIAS': 'https://device.jpush.cn/v3/aliases/',
'TAG': 'https://device.jpush.cn/v3/tags/',
'SCHEDULE': 'https://api.jpush.cn/v3/schedules/',
'ADMIN': 'https://admin.jpush.cn/v1/'
},
'BJ':{
'PUSH': 'https://bjapi.push.jiguang.cn/v3/',
'REPORT': 'https://bjapi.push.jiguang.cn/v3/report/',
'DEVICE': 'https://bjapi.push.jiguang.cn/v3/device/',
'ALIAS': 'https://bjapi.push.jiguang.cn/v3/device/aliases/',
'TAG': 'https://bjapi.push.jiguang.cn/v3/device/tags/',
'SCHEDULE': 'https://bjapi.push.jiguang.cn/v3/push/schedules/',
'ADMIN': 'https://admin.jpush.cn/v1/'
}
}
logger = logging.getLogger('jpush')
def get_url(key, zone='default'):
return ZONES[zone.upper()][key.upper()]
class Unauthorized(Exception):
"""Raised when we get a 401 from the server"""
......
......@@ -14,10 +14,11 @@ logger = logging.getLogger('jpush')
class JPush(object):
def __init__(self, key, secret, timeout=30):
def __init__(self, key, secret, timeout=30, zone = 'default'):
self.key = key
self.secret = secret
self.timeout = timeout
self.zone = zone
self.session = requests.Session()
self.session.auth = (key, secret)
......@@ -56,7 +57,8 @@ class JPush(object):
"JPush.push() is deprecated. See documentation on upgrading.",
DeprecationWarning)
body = json.dumps(payload)
self._request('POST', body, common.PUSH_URL + 'push', 'application/json', version=1)
url = common.get_url('push', self.zone) + 'push'
self._request('POST', body, url, 'application/json', version=1)
def set_logging(self, level):
level_list= ["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"]
......
......@@ -7,9 +7,10 @@ class Device(object):
"""Device info query/update..
"""
def __init__(self, jpush):
def __init__(self, jpush, zone = None):
self._jpush = jpush
self.entity = None
self.zone = zone or jpush.zone
def send(self, method, url, body, content_type=None, version=3):
"""Send the request
......@@ -21,7 +22,7 @@ class Device(object):
def get_taglist(self):
"""Get deviceinfo with registration id.
"""
url = common.TAG_URL
url = common.get_url('tag', self.zone)
body = None
info = self.send("GET", url, body)
return info
......@@ -29,7 +30,7 @@ class Device(object):
def get_deviceinfo(self, registration_id):
"""Get deviceinfo with registration id.
"""
url = common.DEVICE_URL + registration_id + "/"
url = common.get_url('device', self.zone) + registration_id
body = None
info = self.send("GET", url, body)
return info
......@@ -37,7 +38,7 @@ class Device(object):
def set_deviceinfo(self, registration_id, entity):
"""Update deviceinfo with registration id.
"""
url = common.DEVICE_URL + registration_id + "/"
url = common.get_url('device', self.zone) + registration_id
body = json.dumps(entity)
info = self.send("POST", url, body)
return info
......@@ -45,7 +46,7 @@ class Device(object):
def set_devicemobile(self, registration_id, entity):
"""Update deviceinfo with registration id.
"""
url = common.DEVICE_URL + registration_id + "/"
url = common.get_url('device', self.zone) + registration_id
body = json.dumps(entity)
info = self.send("POST", url, body)
return info
......@@ -53,7 +54,7 @@ class Device(object):
def delete_tag(self, tag, platform=None):
"""Delete registration id tag.
"""
url = common.TAG_URL + tag + "/"
url = common.get_url('tag', self.zone) + tag
body = None
if platform:
body = platform
......@@ -63,7 +64,7 @@ class Device(object):
def update_tagusers(self, tag, entity):
"""Add/Remove specified tag users.
"""
url = common.TAG_URL + tag + "/"
url = common.get_url('tag', self.zone) + tag
body = json.dumps(entity)
info = self.send("POST", url, body)
return info
......@@ -71,7 +72,7 @@ class Device(object):
def check_taguserexist(self, tag, registration_id):
"""Check registration id whether in tag.
"""
url = common.TAG_URL + tag + "/registration_ids/" + registration_id
url = common.get_url('tag', self.zone) + tag + "/registration_ids/" + registration_id
body = registration_id
info = self.send("GET", url, body)
return info
......@@ -79,7 +80,7 @@ class Device(object):
def delete_alias(self, alias, platform=None):
"""Delete appkey alias.
"""
url = common.ALIAS_URL + alias + "/"
url = common.get_url('alias', self.zone) + alias
body = None
if platform:
body = platform
......@@ -89,7 +90,7 @@ class Device(object):
def get_aliasuser(self, alias, platform=None):
"""Get appkey alias users.
"""
url = common.ALIAS_URL + alias + "/"
url = common.get_url('alias', self.zone) + alias
body = None
if platform:
body = platform
......
......@@ -8,7 +8,7 @@ logger = logging.getLogger('jpush')
class Push(object):
"""A push notification. Set audience, message, etc, and send."""
def __init__(self, jpush, end_point = 'push'):
def __init__(self, jpush, end_point = 'push', zone = None):
self._jpush = jpush
self.audience = None
self.notification = None
......@@ -18,6 +18,7 @@ class Push(object):
self.message = None
self.smsmessage=None
self.end_point = end_point
self.zone = zone or jpush.zone
@property
def payload(self):
......@@ -49,7 +50,7 @@ class Push(object):
"""
body = json.dumps(self.payload)
url = common.PUSH_URL + self.end_point
url = common.get_url('push', self.zone) + self.end_point
response = self._jpush._request('POST', body, url, 'application/json', version=3)
return PushResponse(response)
......@@ -63,13 +64,15 @@ class Push(object):
"""
body = json.dumps(self.payload)
url = common.PUSH_URL + 'push/validate'
url = common.get_url('push', self.zone) + 'push/validate'
response = self._jpush._request('POST', body, url, 'application/json', version=3)
return PushResponse(response)
def get_cid(self, count, type = None):
body = None
url = common.PUSH_URL + 'push/cid'
url = common.get_url('push', self.zone) + 'push/cid'
params = {
'count': count,
'type': type
......
......@@ -6,8 +6,9 @@ logger = logging.getLogger('jpush')
class Report(object):
"""JPush Report API V3"""
def __init__(self, jpush):
def __init__(self, jpush, zone = None):
self._jpush = jpush
self.zone = zone or jpush.zone
def send(self, method, url, body, content_type=None, version=3, params = None):
"""Send the request
......@@ -16,21 +17,21 @@ class Report(object):
return ReportResponse(response)
def get_received(self,msg_ids):
url = common.REPORT_URL + 'received'
url = common.get_url('report', self.zone) + 'received'
params = { 'msg_ids': msg_ids }
body = None
received = self.send("GET", url, body, params = params)
return received
def get_messages(self, msg_ids):
url = common.REPORT_URL + 'messages'
url = common.get_url('report', self.zone) + 'messages'
params = { 'msg_ids': msg_ids }
body = None
messages = self.send("GET", url, body, params = params)
return messages
def get_users(self, time_unit,start,duration):
url = common.REPORT_URL + 'users'
url = common.get_url('report', self.zone) + 'users'
params = {
'time_unit': time_unit,
'start': start,
......
......@@ -7,40 +7,41 @@ logger = logging.getLogger('jpush')
class Schedule(object):
"""JPush Report API V3"""
def __init__(self, jpush):
def __init__(self, jpush, zone = None):
self._jpush = jpush
self.zone = zone or jpush.zone
def send(self, method, url, body, content_type=None, version=3, params = None):
response = self._jpush._request(method, body, url, content_type, version=3, params = params)
return ScheduleResponse(response)
def post_schedule(self, schedulepayload):
url=common.SCHEDULE_URL
url = common.get_url('schedule', self.zone)
body = json.dumps(schedulepayload)
result = self.send("POST", url, body)
return result
def get_schedule_by_id(self, schedule_id):
url=common.SCHEDULE_URL + schedule_id
url = common.get_url('schedule', self.zone) + schedule_id
body = None
result = self.send("GET", url, body)
return result
def get_schedule_list(self, page = 1):
url = common.SCHEDULE_URL
url = common.get_url('schedule', self.zone)
params = { 'page': page }
body = None
result = self.send("GET", url, body, params = params)
return result
def put_schedule(self, schedulepayload, schedule_id):
url = common.SCHEDULE_URL + schedule_id
url = common.get_url('schedule', self.zone) + schedule_id
body = json.dumps(schedulepayload)
result = self.send("PUT", url, body)
return result
def delete_schedule(self,schedule_id):
url = common.SCHEDULE_URL + schedule_id
url = common.get_url('schedule', self.zone) + schedule_id
body = None
result = self.send("DELETE", url, body)
return result
......
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