Commit 689d0d08 authored by Helperhaps's avatar Helperhaps

add zone

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