Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jpush-api-python-client
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
jpush-api-python-client
Commits
6612ae14
Commit
6612ae14
authored
May 04, 2016
by
fendouai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add APIConnectionException,fix Unauthorized
parent
80476c5e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
21 additions
and
15 deletions
+21
-15
conf.py
examples/push/conf.py
+2
-1
example_all.py
examples/push/example_all.py
+7
-1
common.py
jpush/common.py
+2
-3
core.py
jpush/core.py
+8
-8
core.py
jpush/push/core.py
+1
-2
payload.py
jpush/push/payload.py
+1
-0
No files found.
examples/push/conf.py
View file @
6612ae14
app_key
=
u'6be9204c30b9473e87bad4dc'
master_secret
=
u'9349ad7c90292a603c512e92'
master_secret
=
u'9349ad7c90292a603c512e92'
\ No newline at end of file
examples/push/example_all.py
View file @
6612ae14
...
...
@@ -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"
)
jpush/common.py
View file @
6612ae14
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'
)
...
...
jpush/core.py
View file @
6612ae14
...
...
@@ -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
\n
Body:
\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
):
...
...
jpush/push/core.py
View file @
6612ae14
...
...
@@ -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
)
...
...
jpush/push/payload.py
View file @
6612ae14
...
...
@@ -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.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment