-
Notifications
You must be signed in to change notification settings - Fork 1
/
Channel.py
661 lines (517 loc) · 17.4 KB
/
Channel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
import time
import urllib
import httplib
import hashlib
import json
#from lib.ChannelException import ChannelException
from lib.RequestCore import *
from json.tests.test_pass1 import JSON
class Channel(object):
'''Channel类提供百度云PUSH服务的Python版本SDK,
用户首先实例化这个类,
设置自己的apiKey,和 secretKey,即可使用PUSH服务接口
'''
#发起请求时的时间戳
TIMESTAMP = 'timestamp'
#请求过期的时间, 默认为10分钟
EXPIRES = 'expires'
#API版本号
VERSION = 'v'
#消息通道ID号
CHANNEL_ID = 'channel_id'
#用户类型
USER_ID = 'user_id'
#用户ID的类型
USER_TYPE = 'user_type'
#设备类型
DEVICE_TYPE = 'device_type'
#第几页,批量查询是,需要指定start, 默认为0
START = 'start'
#每页多少条记录,默认为100
LIMIT = 'limit'
#消息
MESSAGES = 'messages'
#消息id
MSG_IDS = 'msg_ids'
#消息key,可以按key去除重复消息
MSG_KEYS = 'msg_keys'
#消息类型,0:消息(透传), 1:通知, 默认为0
MESSAGE_TYPE = 'message_type'
#消息过期时间,默认86400秒
MESSAGE_EXPIRES = 'message_expires'
#消息标签名,可按标签分组
TAG_NAME = 'tag'
#消息标签扩展信息
TAG_INFO = 'info'
#消息标签id
TAG_ID = 'tid'
#应用id,从百度开发者中心获得
APPID = 'appid'
#应用key,从百度开发者中心获得,是创建Channel的必须参数
API_KEY = 'apikey'
#从百度开发者中心获得,是创建Channel的必须参数
SECRET_KEY = 'secret_key'
#Channel常量,用户不必关注
SIGN = 'sign'
METHOD = 'method'
HOST = 'host'
PRODUCT = 'channel'
DEFAULT_HOST = 'channel.api.duapp.com'
#证书相关常量
NAME = 'name'
DESCRIPTION = 'description'
CERT = 'cert'
RELEASE_CERT = 'release_cert'
DEV_CERT = 'dev_cert'
#推送类型
PUSH_TYPE = 'push_type'
#可选推送类型
PUSH_TO_USER = 1
PUSH_TO_TAG = 2
PUSH_TO_ALL = 3
#Channel 错误常量
CHANNEL_SDK_SYS = 1
CHANNEL_SDK_INIT_FAIL = 2
CHANNEL_SDK_PARAM = 3
CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR = 4
CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR = 5
###
# 对外接口
###
def __init__(self, apiKey, secretKey, arr_curlOpts = None):
self._apiKey = apiKey
self._secretKey = secretKey
self._requestId = 0
if arr_curlOpts is None:
self._curlOpts = dict(IMEOUT = 30, CONNECTTIMEOUT = 5)
else:
self._curlOpts = arr_curlOpts
self._arrayErrorMap = {
'0' : 'python sdk error',
Channel.CHANNEL_SDK_SYS : 'php sdk error',
Channel.CHANNEL_SDK_INIT_FAIL : 'php sdk init error',
Channel.CHANNEL_SDK_PARAM : 'lack param',
Channel.CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR : 'http status is error, and the body returned is not a json string',
Channel.CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR :'http status is ok, but the body returned is not a json string'}
self._method_channel_in_body = ['push_msg', 'set_tag', 'fetch_tag', 'delete_tag', 'query_user_tags']
if(not isinstance(self._curlOpts, dict)):
raise ChannelExcepthion('invalid param -arr_curlopt is not an dict',Channel.CHANNEL_SDK_INIT_FAIL)
def setApiKey(self, apiKey):
self._apiKey = apiKey
def setSecretKey(self, secretKey):
self._secretKey = secretKey
def getRequestId(self):
return self._requestId
# 服务器端根据userId, 查询绑定信息
# 用户关注:是
def queryBindList(self, optional = None):
"""
服务器端根据userId, 查询绑定信息
参数:
str userId: 用户ID号
dict optional: 可选参数
返回值:
成功:python字典; 失败:False
"""
try:
arrArgs = dict()
arrArgs[Channel.METHOD] = 'query_bindlist'
arrArgs[Channel.API_KEY] = self._apiKey
if(not arrArgs.has_key(Channel.TIMESTAMP)):
arrArgs[Channel.TIMESTAMP] = int(time.time())
if(arrArgs.has_key(Channel.SECRET_KEY)):
del arrArgs[Channel.SECRET_KEY]
resource = 'channel'
if(arrArgs.has_key(Channel.CHANNEL_ID) ):
if(arrArgs[Channel.CHANNEL_ID] is not None and arrArgs[Channel.METHOD] not in self._method_channel_in_body):
resource = arrArgs[Channel.CHANNEL_ID]
del arrArgs[Channel.CHANNEL_ID]
url = 'http://' + Channel.DEFAULT_HOST + '/rest/2.0/' + Channel.PRODUCT + '/' + resource
arrArgs[Channel.SIGN] = self._genSign('POST', url, arrArgs)
print arrArgs
self.send_request1(arrArgs)
self.send_request(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
# 服务器端根据userId, 查询绑定信息
# 用户关注:是
def queryBindList_byId(self, userId, optional = None):
"""
服务器端根据userId, 查询绑定信息
参数:
str userId: 用户ID号
dict optional: 可选参数
返回值:
成功:python字典; 失败:False
"""
try:
#tmpArgs = [userId, optional]
#arrArgs = self._mergeArgs([Channel.USER_ID], tmpArgs)
arrArgs = dict()
arrArgs[Channel.METHOD] = 'query_bindlist'
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
# 推送消息
# 用户关注:是
def pushMessage(self, push_type, messages,
message_keys, optional = None):
"""
推送消息
参数:
push_type: 推送消息的类型
messages:消息内容
message_keys: 消息key
optional: 可选参数
返回值 成功:python字典; 失败:False
"""
try:
tmpArgs = [push_type, messages, message_keys, optional]
arrArgs = self._mergeArgs([Channel.PUSH_TYPE, Channel.MESSAGES,
Channel.MSG_KEYS], tmpArgs)
arrArgs[Channel.METHOD] = 'push_msg'
if(push_type == Channel.PUSH_TO_USER):
if(not arrArgs.has_key(Channel.USER_ID) or
arrArgs[Channel.USER_ID] is None):
raise ChannelException("userId should be specified in optional when pushType is PUSH_TO_USER", self.CHANNEL_SDK_PARAM)
elif(push_type == Channel.PUSH_TO_TAG):
if(not arrArgs.has_key(Channel.TAG_NAME) or
arrArgs[Channel.TAG_NAME] is None):
raise ChannelException("tagName should be specified in optional when pushType is PUSH_TO_TAG", self.CHANNEL_SDK_PARAM)
elif(push_type == Channel.PUSH_TO_ALL):
pass
else:
raise ChannelException("pushType value is invalid", self.CHANNEL_SDK_PARAM)
arrArgs[Channel.PUSH_TYPE] = push_type
if(isinstance(arrArgs[Channel.MESSAGES], list)):
arrArgs[Channel.MESSAGES] = json.dumps(arrArgs[Channel.MESSAGES])
if(isinstance(arrArgs[Channel.MSG_KEYS], list)):
arrArgs[Channel.MSG_KEYS] = json.dumps(arrArgs[Channel.MSG_KEYS])
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#校验userId是否已经绑定
#用户关注:是
def verifyBind(self, userId, optional = None):
"""
校验userId是否已经绑定
参数:
userId:用户id
optional:可选参数
返回值:
成功:python数组;失败False
"""
try:
tmpArgs = [userId, optional]
arrArgs = self._mergeArgs([Channel.USER_ID], tmpArgs)
arrArgs[Channel.METHOD] = 'verify_bind';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#根据userId查询消息
#用户关注:是
def fetchMessage(self, userId, optional = None):
"""
根据userId查询消息
参数:
userId:用户id
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [userId, optional]
arrArgs = self._mergeArgs([Channel.USER_ID], tmpArgs)
arrArgs[Channel.METHOD] = 'fetch_msg';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#根据userId查询消息个数
#用户关注:是
def fetchMessageCount(self, userId, optional = None):
"""
根据userId查询消息个数
参数:
userId:用户id
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [userId, optional]
arrArgs = self._mergeArgs([Channel.USER_ID], tmpArgs)
arrArgs[Channel.METHOD] = 'fetch_msgcount';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#根据userId, msgIds删除消息
#用户关注:是
def deleteMessage(self, userId, msgId, optional = None):
"""
根据userId, msgIds删除消息
参数:
userId:用户id
msgIds:消息id
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [userId, msgId, optional]
arrArgs = self._mergeArgs([Channel.USER_ID, Channel.MSG_IDS], tmpArgs)
arrArgs[Channel.METHOD] = 'delete_msg';
if(isinstance(arrArgs[Channel.MSG_IDS], list)):
arrArgs[Channel.MSG_IDS] = json.dumps(arrArgs[Channel.MSG_IDS])
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
#设置消息标签
#用户关注:是
def setTag(self, tagName, optional = None):
"""
设置消息标签
参数:
tagName:标签
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [tagName, optional]
arrArgs = self._mergeArgs([Channel.TAG_NAME], tmpArgs)
arrArgs[Channel.METHOD] = 'set_tag';
return self._commonProcess(arrArgs)
except ChannelException, e:
self._channelExceptionHandler(e)
return False
#查询消息标签信息
#用户关注:是
def fetchTag(self, optional = None):
"""
查询消息标签信息
参数:
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [optional]
arrArgs = self._mergeArgs([], tmpArgs)
arrArgs[Channel.METHOD] = 'fetch_tag';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#删除消息标签
#用户关注:是
def deleteTag(self, tagName, optional = None):
"""
删除消息标签
参数:
tagName:标签
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [tagName, optional]
arrArgs = self._mergeArgs([Channel.TAG_NAME], tmpArgs)
arrArgs[Channel.METHOD] = 'delete_tag';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#查询用户相关的标签
#用户关注:是
def queryUserTag(self, userId, optional = None):
"""
查询用户相关的标签
参数:
userId:用户id
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [userId, optional]
arrArgs = self._mergeArgs([Channel.USER_ID], tmpArgs)
arrArgs[Channel.METHOD] = 'query_user_tags';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#根据channelId查询设备类型
#用户关注:是
def queryDeviceType(self, channelId, optional = None):
"""
根据channelId查询设备类型
参数:
ChannelId:用户Channel的ID号
optional:可选参数
返回值:
成功:python字典; 失败:False
"""
try:
tmpArgs = [channelId, optional]
arrArgs = self._mergeArgs([Channel.CHANNEL_ID], tmpArgs)
arrArgs[Channel.METHOD] = 'query_device_type';
return self._commonProcess(arrArgs)
except ChannelException, e:
#self._channelExceptionHandler(e)
return False
#
# 内部函数
#
def _checkString(self, string, minLen, maxLen):
if( isinstance(string, str) and len(string) >= minLen
and len(string) <= maxLen ):
return True
else:
return False
def _adjustOpt(self, opt):
if(not ((opt is not None) and isinstance(opt, dict))):
raise ChannelException('no params are set', Channel.CHANNEL_SDK_PARAM)
if(not opt.has_key(Channel.TIMESTAMP)):
opt[Channel.TIMESTAMP] = int(time.time())
opt[Channel.HOST] = Channel.DEFAULT_HOST
opt[Channel.API_KEY] = self._apiKey
if(opt.has_key(Channel.SECRET_KEY)):
del opt[Channel.SECRET_KEY]
def _genSign(self, method, url, arrContent):
gather = method + url
keys = arrContent.keys()
keys.sort()
for key in keys:
gather += key + '=' + str(arrContent[key])
gather += self._secretKey
sign = hashlib.md5(urllib.quote_plus(gather))
return sign.hexdigest()
def _baseControl(self, opt):
resource = 'channel'
if(opt.has_key(Channel.CHANNEL_ID) ):
if(opt[Channel.CHANNEL_ID] is not None and opt[Channel.METHOD] not in self._method_channel_in_body):
resource = opt[Channel.CHANNEL_ID]
del opt[Channel.CHANNEL_ID]
host = opt[Channel.HOST]
del opt[Channel.HOST]
url = 'http://' + host + '/rest/2.0/' + Channel.PRODUCT + '/'
url += resource
http_method = 'POST'
opt[Channel.SIGN] = self._genSign(http_method, url, opt)
request = RequestCore(url)
headers = dict()
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client'
for (headerKey , headerValue) in headers.items():
headerValue = headerValue.replace('\r', '')
headerValue = headerValue.replace('\n', '')
if (headerValue is not None):
request.add_header(headerKey, headerValue)
request.set_method(http_method)
request.set_body(urllib.urlencode(opt))
if(isinstance(self._curlOpts, dict)):
request.set_curlopts(self._curlOpts)
request.handle_request()
return ResponseCore(request.get_response_header(),request.get_response_body(),request.get_response_code())
def _commonProcess(self, paramOpt):
self._adjustOpt(paramOpt)
ret = self._baseControl(paramOpt)
if( ret is None):
raise ChannelException('base control returned None object',
Channel.CHANNEL_SDK_SYS)
if(ret.isOK()):
result = json.loads(ret.body)
if (result is None):
raise ChannelException(ret.body,
Channel.CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR)
self._requestId = result['request_id']
return result
result = json.loads(ret.body)
if(result is None):
raise ChannelException('ret body:' + ret.body,
Channel.CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR)
self._requestId = result['request_id']
raise ChannelException(result['error_msg'], result['error_code'])
def _mergeArgs(self, arrNeed, tmpArgs):
arrArgs = dict()
if( len(arrNeed) == 0 and len(tmpArgs) == 0):
return arrArgs
if(len(tmpArgs)-1 != len(arrNeed) and len(tmpArgs) != len(arrNeed)):
keys = '('
for key in arrNeed:
keys += key + ','
if(key[-1] == '' and key[-2] == ','):
keys = keys[0:-2]
keys += ')'
raise ChannelException('invalid sdk, params, params' + keys + 'are need', Channel.CHANNEL_SDK_PARAM)
if(len(tmpArgs)-1 == len(arrNeed) and tmpArgs[-1] is not None
and (not isinstance(tmpArgs[-1], dict)) ):
raise ChannelException ('invalid sdk params, optional param must bean dict', Channel.CHANNEL_SDK_PARAM)
idx = 0
if(isinstance(arrNeed, list)):
for key in arrNeed:
if(tmpArgs[idx] is None):
raise ChannelException ('lack param ' + key, Channel.CHANNEL_SDK_PARAM)
arrArgs[key] = tmpArgs[idx]
idx = idx + 1
if(len(tmpArgs) == idx + 1 and tmpArgs[idx] is not None):
for (key, value) in tmpArgs[idx].items():
if(not arrArgs.has_key(key) and value is not None):
arrArgs[key] = value
return arrArgs
# send Http Request
def send_request(self, arrArgs, echo = False):
conn = httplib.HTTPConnection("channel.api.duapp.com") #hostname, port
#headers = dict()
#headers['Content-Type'] = 'application/x-www-form-urlencoded'
#headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client'
headers = {"Content-type": "application/x-www-form-urlencoded", "User-Agent": "Baidu Channel Service Pythonsdk Client"}
conn.request("POST", "rest/2.0/channel/channel", urllib.urlencode(arrArgs), headers)
response = conn.getresponse()
if response.status == 200 and response.reason == "OK":
return response.read() if echo else "@Reviced Data OK."
else:
raise RuntimeError("Http request exception:", response.status)
def send_request1(self, opt):
request = RequestCore("http://channel.api.duapp.com/rest/2.0/channel/channel")
headers = dict()
headers['Content-Type'] = 'application/x-www-form-urlencoded'
headers['User-Agent'] = 'Baidu Channel Service Pythonsdk Client'
for (headerKey , headerValue) in headers.items():
headerValue = headerValue.replace('\r', '')
headerValue = headerValue.replace('\n', '')
if (headerValue is not None):
request.add_header(headerKey, headerValue)
request.set_method('POST')
request.set_body(urllib.urlencode(opt))
if(isinstance(self._curlOpts, dict)):
request.set_curlopts(self._curlOpts)
request.handle_request()
ret = ResponseCore(request.get_response_header(),request.get_response_body(),request.get_response_code())
if( ret is None):
raise ChannelException('base control returned None object',
Channel.CHANNEL_SDK_SYS)
if(ret.isOK()):
result = ret.body
print result
if (result is None):
raise ChannelException(ret.body,
Channel.CHANNEL_SDK_HTTP_STATUS_OK_BUT_RESULT_ERROR)
#self._requestId = result['request_id']
return result
result = json.loads(ret.body)
if(result is None):
raise ChannelException('ret body:' + ret.body,
Channel.CHANNEL_SDK_HTTP_STATUS_ERROR_AND_RESULT_ERROR)
self._requestId = result['request_id']
raise ChannelException(result['error_msg'], result['error_code'])
def _channelExceptionHandler(self, ex):
print ex.error_msg, self._arrayErrorMap[ex.error_code]