Commit 6612ae14 authored by fendouai's avatar fendouai

add APIConnectionException,fix Unauthorized

parent 80476c5e
app_key = u'6be9204c30b9473e87bad4dc' app_key = u'6be9204c30b9473e87bad4dc'
master_secret = u'9349ad7c90292a603c512e92' master_secret = u'9349ad7c90292a603c512e92'
\ No newline at end of file
...@@ -10,5 +10,11 @@ push.audience = jpush.all_ ...@@ -10,5 +10,11 @@ push.audience = jpush.all_
push.notification = jpush.notification(alert="hello python jpush api") push.notification = jpush.notification(alert="hello python jpush api")
push.platform = jpush.all_ push.platform = jpush.all_
core.logger.debug('logger debug message test') core.logger.debug('logger debug message test')
push.send() try:
response=push.send()
except common.Unauthorized:
raise common.Unauthorized("Unauthorized")
except common.APIConnectionException:
raise common.APIConnectionException("conn")
import json import json
import logging import logging
import requests
BASE_URL = "https://api.jpush.cn/" BASE_URL = "https://api.jpush.cn/"
PUSH_URL = BASE_URL + 'v3/push' PUSH_URL = BASE_URL + 'v3/push'
...@@ -47,12 +47,11 @@ class JPushFailure(Exception): ...@@ -47,12 +47,11 @@ class JPushFailure(Exception):
self.error_code = error_code self.error_code = error_code
self.details = details self.details = details
self.response = response self.response = response
super(JPushFailure, self).__init__(*args) super(self, JPushFailure).__init__(*args)
@classmethod @classmethod
def from_response(cls, response): def from_response(cls, response):
"""Instantiate a ValidationFailure from a Response object""" """Instantiate a ValidationFailure from a Response object"""
try: try:
payload = response.json() payload = response.json()
error = payload.get('error') error = payload.get('error')
......
...@@ -3,7 +3,6 @@ import logging ...@@ -3,7 +3,6 @@ import logging
import warnings import warnings
import requests import requests
from . import common from . import common
from .push import Push from .push import Push
from .device import Device from .device import Device
...@@ -27,8 +26,7 @@ class JPush(object): ...@@ -27,8 +26,7 @@ class JPush(object):
self.session = requests.Session() self.session = requests.Session()
self.session.auth = (key, secret) self.session.auth = (key, secret)
def _request(self, method, body, url, content_type=None, def _request(self, method, body, url, content_type=None, version=None, params=None):
version=None, params=None):
headers = {} headers = {}
headers['user-agent'] = 'jpush-api-python-client' headers['user-agent'] = 'jpush-api-python-client'
...@@ -39,9 +37,12 @@ class JPush(object): ...@@ -39,9 +37,12 @@ class JPush(object):
method, url, '\n\t'.join( method, url, '\n\t'.join(
'%s: %s' % (key, value) for (key, value) in headers.items()), '%s: %s' % (key, value) for (key, value) in headers.items()),
body) body)
try:
response = self.session.request( response = self.session.request(method, url, data=body, params=params, headers=headers, timeout=30)
method, url, data=body, params=params, headers=headers) 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", logger.debug("Received %s response. Headers:\n\t%s\nBody:\n\t%s",
response.status_code, '\n\t'.join( response.status_code, '\n\t'.join(
...@@ -50,10 +51,9 @@ class JPush(object): ...@@ -50,10 +51,9 @@ class JPush(object):
response.content) response.content)
if response.status_code == 401: if response.status_code == 401:
raise common.Unauthorized raise common.Unauthorized("Please check your AppKey and Master Secret")
elif not (200 <= response.status_code < 300): elif not (200 <= response.status_code < 300):
raise common.JPushFailure.from_response(response) raise common.JPushFailure.from_response(response)
return response return response
def push(self, payload): def push(self, payload):
......
...@@ -61,8 +61,7 @@ class Push(object): ...@@ -61,8 +61,7 @@ class Push(object):
""" """
body = json.dumps(self.payload) body = json.dumps(self.payload)
response = self._jpush._request('POST', body, response = self._jpush._request('POST', body, common.VALIDATE_PUSH_URL, 'application/json', version=3)
common.VALIDATE_PUSH_URL, 'application/json', version=3)
print (response.content) print (response.content)
return PushResponse(response) return PushResponse(response)
......
...@@ -3,6 +3,7 @@ import re ...@@ -3,6 +3,7 @@ import re
# Valid autobadge values: auto, +N, -N # Valid autobadge values: auto, +N, -N
VALID_AUTOBADGE = re.compile(r'^(auto|[+-][\d]+)$') VALID_AUTOBADGE = re.compile(r'^(auto|[+-][\d]+)$')
def notification(alert=None, ios=None, android=None, winphone=None): def notification(alert=None, ios=None, android=None, winphone=None):
"""Create a notification payload. """Create a notification payload.
......
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