Commit 6612ae14 authored by fendouai's avatar fendouai

add APIConnectionException,fix Unauthorized

parent 80476c5e
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_
push.notification = jpush.notification(alert="hello python jpush api")
push.platform = jpush.all_
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 logging
import requests
BASE_URL = "https://api.jpush.cn/"
PUSH_URL = BASE_URL + 'v3/push'
......@@ -47,12 +47,11 @@ class JPushFailure(Exception):
self.error_code = error_code
self.details = details
self.response = response
super(JPushFailure, self).__init__(*args)
super(self, JPushFailure).__init__(*args)
@classmethod
def from_response(cls, response):
"""Instantiate a ValidationFailure from a Response object"""
try:
payload = response.json()
error = payload.get('error')
......
......@@ -3,7 +3,6 @@ import logging
import warnings
import requests
from . import common
from .push import Push
from .device import Device
......@@ -27,8 +26,7 @@ class JPush(object):
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, content_type=None, version=None, params=None):
headers = {}
headers['user-agent'] = 'jpush-api-python-client'
......@@ -39,9 +37,12 @@ class JPush(object):
method, url, '\n\t'.join(
'%s: %s' % (key, value) for (key, value) in headers.items()),
body)
response = self.session.request(
method, url, data=body, params=params, headers=headers)
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(
......@@ -50,10 +51,9 @@ class JPush(object):
response.content)
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):
raise common.JPushFailure.from_response(response)
return response
def push(self, payload):
......
......@@ -61,8 +61,7 @@ class Push(object):
"""
body = json.dumps(self.payload)
response = self._jpush._request('POST', body,
common.VALIDATE_PUSH_URL, 'application/json', version=3)
response = self._jpush._request('POST', body, common.VALIDATE_PUSH_URL, 'application/json', version=3)
print (response.content)
return PushResponse(response)
......
......@@ -3,6 +3,7 @@ import re
# Valid autobadge values: auto, +N, -N
VALID_AUTOBADGE = re.compile(r'^(auto|[+-][\d]+)$')
def notification(alert=None, ios=None, android=None, winphone=None):
"""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