Commit 94c3a9d9 authored by fendouai's avatar fendouai

Schedule and Report Api with Example

parent b90b648b
......@@ -6,4 +6,5 @@ push = _jpush.create_push()
push.audience = jpush.all_
push.notification = jpush.notification(alert="Hello, world!")
push.platform = jpush.all_
push.send()
app_key = u'6be9204c30b9473e87bad4dc'
master_secret = u'9349ad7c90292a603c512e92'
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
schedule = _jpush.create_schedule()
schedule.delete_schedule("e9c553d0-0850-11e6-b6d4-0021f652c102")
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
schedule = _jpush.create_schedule()
schedule.get_schedule_by_id("e9c553d0-0850-11e6-b6d4-0021f652c102")
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
schedule = _jpush.create_schedule()
schedule.get_schedule_list("1")
\ No newline at end of file
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
schedule = _jpush.create_schedule()
push = _jpush.create_push()
push.audience = jpush.all_
push.notification = jpush.notification(alert="Hello, world!")
push.platform = jpush.all_
push=push.payload
trigger=jpush.schedulepayload.trigger("2016-05-17 12:00:00")
schedulepayload=jpush.schedulepayload.schedulepayload("name",True,trigger,push)
schedule.post_schedule(schedulepayload)
import jpush as jpush
from conf import app_key, master_secret
_jpush = jpush.JPush(app_key, master_secret)
schedule = _jpush.create_schedule()
push = _jpush.create_push()
push.audience = jpush.all_
push.notification = jpush.notification(alert="Hello, world!")
push.platform = jpush.all_
push=push.payload
trigger=jpush.schedulepayload.trigger("2016-05-17 12:00:00")
schedulepayload=jpush.schedulepayload.schedulepayload("putanewname",True,trigger,push)
schedule.put_schedule(schedulepayload,"17349f00-0852-11e6-91b1-0021f653c902")
\ No newline at end of file
......@@ -37,6 +37,11 @@ from .report import (
Report,
)
from .schedule import (
Schedule,
schedulepayload,
)
__all__ = [
JPush,
JPushFailure,
......@@ -63,6 +68,8 @@ __all__ = [
device_alias,
device_regid,
Report,
Schedule,
schedulepayload,
]
# Silence urllib3 INFO logging by default
......
......@@ -17,6 +17,8 @@ RECEIVED_URL=REPORT_BASEURL+"v3/received?msg_ids="
MESSAGES_URL=REPORT_BASEURL+"v3/messages?msg_ids="
USERS_URL=REPORT_BASEURL+"v3/users?"
BASE_SCHEDULEURL="https://api.jpush.cn/v3/schedules/"
BASE_LISTURL="https://api.jpush.cn/v3/schedules?page="
logger = logging.getLogger('jpush')
class Unauthorized(Exception):
......
......@@ -8,6 +8,7 @@ from . import common
from .push import Push
from .device import Device
from .report import Report
from .schedule import Schedule
logger = logging.getLogger('jpush')
class JPush(object):
......@@ -72,3 +73,7 @@ class JPush(object):
def create_report(self):
"""Create a Push notification."""
return Report(self)
def create_schedule(self):
"""Create a Push notification."""
return Schedule(self)
from .core import Schedule
__all__ = [
Schedule,
]
\ No newline at end of file
import json
import logging
from jpush import common
from schedulepayload import *
logger = logging.getLogger('jpush')
class Schedule(object):
"""JPush Report API V3"""
def __init__(self, jpush):
self._jpush = jpush
def send(self, method, url, body, content_type=None, version=3):
response = self._jpush._request(method, body, url, content_type, version=3)
return ScheduleResponse(response)
def post_schedule(self,schedulepayload):
url=common.BASE_SCHEDULEURL
body = json.dumps(schedulepayload)
result = self.send("POST", url, body)
print (result)
return result
def get_schedule_by_id(self,schedule_id):
url=common.BASE_SCHEDULEURL + schedule_id
print url
body = None
result = self.send("GET", url, body)
print (result)
return result
def get_schedule_list(self,page_id):
if page_id is not None:
url=common.BASE_LISTURL + page_id
else:
url = common.BASE_LISTURL
print url
body = None
result = self.send("GET", url, body)
print (result)
return result
def put_schedule(self, schedulepayload,schedule_id):
url = common.BASE_SCHEDULEURL + schedule_id
body = json.dumps(schedulepayload)
result = self.send("PUT", url, body)
print (result)
return result
def delete_schedule(self,schedule_id):
url = common.BASE_SCHEDULEURL + schedule_id
print url
body = None
result = self.send("DELETE", url, body)
print (result)
return result
class ScheduleResponse(object):
"""Response to a successful device request send.
Right now this is a fairly simple wrapper around the json payload response,
but making it an object gives us some flexibility to add functionality
later.
"""
payload = None
def __init__(self, response):
if 0 != len(response.content):
data = response.json()
self.payload = data
elif 200 == response.status_code:
self.payload = "success"
def __str__(self):
return "Schedule response Payload: {0}".format(self.payload)
\ No newline at end of file
import re
from jpush import push
def schedulepayload(name=None, enabled=None, trigger=None, push=None):
schedulepayload = {}
if name is not None:
schedulepayload['name'] = name
if enabled is not None:
schedulepayload['enabled'] = enabled
if trigger is not None:
schedulepayload['trigger'] = trigger
if push is not None:
schedulepayload['push'] = push
if not schedulepayload:
raise ValueError("schedulepayload may not be empty")
return schedulepayload
def trigger(time,start=None, end=None,time_unit=None,frequency=None,point=None):
if(start==None and end==None and time_unit==None and frequency==None):
trigger={}
single={}
single["time"]=time
trigger["single"]=single
return trigger
else:
trigger={}
periodical={}
if time is not None:
periodical['time'] = time
if start is not None:
periodical['start'] = start
if end is not None:
periodical['end'] = end
if time_unit is not None:
periodical['time_unit'] = time_unit
if frequency is not None:
periodical['frequency'] = frequency
if point is not None:
periodical['point'] = point
trigger["periodical"]=periodical
return trigger
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