forked from rongcloud/server-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrong.py
More file actions
549 lines (444 loc) · 17.7 KB
/
rong.py
File metadata and controls
549 lines (444 loc) · 17.7 KB
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
#! /usr/bin/env python
# coding=utf-8
import os
import re
import json
import time
import logging
import random
import datetime
import hashlib
import requests
class ConnectionError(Exception):
def __init__(self, response, content=None, message=None):
self.response = response
self.content = content
self.message = message
def __str__(self):
message = "Failed."
if hasattr(self.response, 'status_code'):
message += " Response status: %s." % (self.response.status_code)
if hasattr(self.response, 'reason'):
message += " Response message: %s." % (self.response.reason)
if self.content is not None:
message += " Error message: " + str(self.content)
return message
class Redirection(ConnectionError):
"""3xx Redirection
"""
def __str__(self):
message = super(Redirection, self).__str__()
if self.response.get('Location'):
message = "%s => %s" % (message, self.response.get('Location'))
return message
class MissingParam(TypeError):
pass
class MissingConfig(Exception):
pass
class ClientError(ConnectionError):
"""4xx Client Error
"""
pass
class BadRequest(ClientError):
"""400 Bad Request
"""
pass
class UnauthorizedAccess(ClientError):
"""401 Unauthorized
"""
pass
class ForbiddenAccess(ClientError):
"""403 Forbidden
"""
pass
class ResourceNotFound(ClientError):
"""404 Not Found
"""
pass
class ResourceConflict(ClientError):
"""409 Conflict
"""
pass
class ResourceGone(ClientError):
"""410 Gone
"""
pass
class ResourceInvalid(ClientError):
"""422 Invalid
"""
pass
class ServerError(ConnectionError):
"""5xx Server Error
"""
pass
class MethodNotAllowed(ClientError):
"""405 Method Not Allowed
"""
def allowed_methods(self):
return self.response['Allow']
class ApiClient(object):
api_host = "https://api.cn.rong.io"
response_type = "json"
ACTION_USER_TOKEN = "/user/getToken"
ACTION_USER_REFRESH = "/user/refresh"
ACTION_USER_CHECKONLINE = "/user/checkOnline"
ACTION_USER_BLOCK = '/user/block'
ACTION_USER_UNBLOCK = '/user/unblock'
ACTION_USER_BLOCK_QUERY = '/user/block/query'
ACTION_USER_BLACKLIST_ADD = "/user/blacklist/add"
ACTION_USER_BLACKLIST_REMOVE = "/user/blacklist/remove"
ACTION_USER_BLACKLIST_QUERY = "/user/blacklist/query"
ACTION_MESSAGE_PUBLISH = "/message/private/publish"
ACTION_MESSAGE_SYSTEM_PUBLISH = "/message/system/publish"
ACTION_MESSAGE_GROUP_PUBLISH = "/message/group/publish"
ACTION_MESSAGE_CHATROOM_PUBLISH = "/message/chatroom/publish"
ACTION_MESSAGE_HISTORY = '/message/history'
ACTION_GROUP_SYNC = "/group/sync"
ACTION_GROUP_CREATE = "/group/create"
ACTION_GROUP_JOIN = "/group/join"
ACTION_GROUP_QUIT = "/group/quit"
ACTION_GROUP_DISMISS = "/group/dismiss"
ACTION_GROUP_REFRESH = "/group/refresh"
ACTION_CHATROOM_CREATE = "/chatroom/create"
ACTION_CHATROOM_DESTROY = "/chatroom/destroy"
ACTION_CHATROOM_QUERY = "/chatroom/query"
def __init__(self, key=None, secret=None):
self._app_key = key
self._app_secret = secret
if self._app_key is None:
self._app_key = os.environ.get('rongcloud_app_key')
if self._app_secret is None:
self._app_secret = os.environ.get('rongcloud_app_secret')
@staticmethod
def _merge_dict(data, *override):
result = {}
for current_dict in (data,) + override:
result.update(current_dict)
return result
@staticmethod
def _join_url(url, *paths):
for path in paths:
url = re.sub(r'/?$', re.sub(r'^/?', '/', path), url)
return url
@staticmethod
def _handle_response(response, content):
"""Validate HTTP response
"""
status = response.status_code
if status in (301, 302, 303, 307):
raise Redirection(response, content)
elif 200 <= status <= 299:
return json.loads(content) if content else {}
elif status == 400:
raise BadRequest(response, content)
elif status == 401:
raise UnauthorizedAccess(response, content)
elif status == 403:
raise ForbiddenAccess(response, content)
elif status == 404:
raise ResourceNotFound(response, content)
elif status == 405:
raise MethodNotAllowed(response, content)
elif status == 409:
raise ResourceConflict(response, content)
elif status == 410:
raise ResourceGone(response, content)
elif status == 422:
raise ResourceInvalid(response, content)
elif 401 <= status <= 499:
raise ClientError(response, content)
elif 500 <= status <= 599:
raise ServerError(response, content)
else:
raise ConnectionError(response, content, "Unknown response code: #{response.code}")
def _make_common_signature(self):
"""生成通用签名
一般情况下,您不需要调用该方法
文档详见 http://docs.rongcloud.cn/server.html#_API_调用签名规则
:return: {'app-key':'xxx','nonce':'xxx','timestamp':'xxx','signature':'xxx'}
"""
nonce = str(random.random())
timestamp = str(
int(time.time()) * 1000
)
signature = hashlib.sha1(
self._app_secret + nonce + timestamp
).hexdigest()
return {
"rc-app-key": self._app_key,
"rc-nonce": nonce,
"rc-timestamp": timestamp,
"rc-signature": signature
}
def _headers(self):
"""Default HTTP headers
"""
return self._merge_dict(
self._make_common_signature(),
{
"content-type": "application/x-www-form-urlencoded",
}
)
def _http_call(self, url, method, **kwargs):
"""Makes a http call. Logs response information.
"""
logging.info("Request[%s]: %s" % (method, url))
start_time = datetime.datetime.now()
response = requests.request(method,
url,
verify=False,
**kwargs)
duration = datetime.datetime.now() - start_time
logging.info("Response[%d]: %s, Duration: %s.%ss." %
(response.status_code, response.reason,
duration.seconds, duration.microseconds))
return self._handle_response(response,
response.content.decode("utf-8"))
def call_api(self, action, params=None, **kwargs):
"""
调用API的通用方法,有关SSL证书验证问题请参阅
http://www.python-requests.org/en/latest/user/advanced/#ssl-cert-verification
:param action: Method Name,
:param params: Dictionary,form params for api.
:param timeout: (optional) Float describing the timeout of the request.
:return:
"""
return self._http_call(
url=self._join_url(self.api_host, "%s.%s" % (action, self.response_type)),
method="POST",
data=params,
headers=self._headers(),
**kwargs
)
def user_get_token(self, user_id, name, portrait_uri):
""" 获取token
http://docs.rongcloud.cn/server.html#_获取_Token_方法
:param user_id:
:param name:
:param portrait_uri:
:return: {"code":200, "userId":"jlk456j5", "token":"sfd9823ihufi"}
"""
return self.call_api(
action=self.ACTION_USER_TOKEN,
params={
"userId": user_id,
"name": name,
"portraitUri": portrait_uri
}
)
def user_refresh(self, user_id, name, portrait_uri):
return self.call_api(
action=self.ACTION_USER_REFRESH,
params={
"userId": user_id,
"name": name,
"portraitUri": portrait_uri
}
)
def user_check_online(self, user_id):
return self.call_api(
action=self.ACTION_USER_CHECKONLINE,
params={
"userId": user_id
}
)
def user_block(self, user_id, minute):
return self.call_api(
action=self.ACTION_USER_BLOCK,
params={
"userId": user_id,
"minute": minute
}
)
def user_unblock(self, user_id):
return self.call_api(
action=self.ACTION_USER_UNBLOCK,
params={
"userId": user_id
}
)
def user_block_query(self):
return self.call_api(
action=self.ACTION_USER_BLOCK_QUERY
)
def user_blocklist_add(self, user_id, black_user_id):
return self.call_api(
action=self.ACTION_USER_BLACKLIST_ADD,
params={
'userId':user_id,
'blackUserId':black_user_id
}
)
def user_blocklist_remove(self, user_id, black_user_id):
return self.call_api(
action=self.ACTION_USER_BLACKLIST_REMOVE,
params={
'userId':user_id,
'blackUserId':black_user_id
}
)
def user_blocklist_query(self, user_id):
return self.call_api(
action=self.ACTION_USER_BLACKLIST_QUERY,
params={
'userId':user_id,
}
)
def message_publish(self, from_user_id, to_user_id,
object_name, content,
push_content=None, push_data=None):
""" 发送会话消息
http://docs.rongcloud.cn/server.html#_融云内置消息类型表
http://docs.rongcloud.cn/server.html#_发送会话消息_方法
:param from_user_id:发送人用户 Id
:param to_user_id:接收用户 Id,提供多个本参数可以实现向多人发送消息。
:param object_name:消息类型,目前包括如下类型 ["RC:TxtMsg","RC:ImgMsg","RC:VcMsg","RC:LocMsg"]
:param content:发送消息内容,参考融云消息类型表.示例说明;如果 objectName 为自定义消息类型,该参数可自定义格式。(必传)
:param push_content:如果为自定义消息,定义显示的 Push 内容(可选)
:param push_data:针对 iOS 平台,Push 通知附加的 payload 字段,字段名为 appData。(可选)
:return:{"code":200}
"""
return self.call_api(
action=self.ACTION_MESSAGE_PUBLISH,
params={
"fromUserId": from_user_id,
"toUserId": to_user_id,
"objectName": object_name,
"content": content,
"pushContent": push_content if push_content is not None else "",
"pushData": push_data if push_data is not None else ""
}
)
def message_system_publish(self, from_user_id, to_user_id,
object_name, content,
push_content=None, push_data=None):
"""发送系统消息
http://docs.rongcloud.cn/server.html#_发送系统消息_方法
:param from_user_id:发送人用户 Id
:param to_user_id:接收用户 Id,提供多个本参数可以实现向多人发送消息。
:param object_name:消息类型,目前包括如下类型 ["RC:TxtMsg","RC:ImgMsg","RC:VcMsg","RC:LocMsg"]
:param content:发送消息内容,参考融云消息类型表.示例说明;如果 objectName 为自定义消息类型,该参数可自定义格式。(必传)
:param push_content:如果为自定义消息,定义显示的 Push 内容(可选)
:param push_data:针对 iOS 平台,Push 通知附加的 payload 字段,字段名为 appData。(可选)
:return:{"code":200}
"""
return self.call_api(
action=self.ACTION_MESSAGE_SYSTEM_PUBLISH,
params={
"fromUserId": from_user_id,
"toUserId": to_user_id,
"objectName": object_name,
"content": content,
"pushContent": push_content if push_content is not None else '',
"pushData": push_data if push_data is not None else ''
}
)
def message_group_publish(self, from_user_id, to_group_id, object_name,
content, push_content=None, push_data=None):
"""以一个用户身份向群组发送消息
http://docs.rongcloud.cn/server.html#_发送群组消息_方法
:param from_user_id:发送人用户 Id
:param to_group_id:接收群Id,提供多个本参数可以实现向多群发送消息。(必传)
:param object_name:消息类型,目前包括如下类型 ["RC:TxtMsg","RC:ImgMsg","RC:VcMsg","RC:LocMsg"]
:param content:发送消息内容,参考融云消息类型表.示例说明;如果 objectName 为自定义消息类型,该参数可自定义格式。(必传)
:param push_content:如果为自定义消息,定义显示的 Push 内容(可选)
:param push_data:针对 iOS 平台,Push 通知附加的 payload 字段,字段名为 appData。(可选)
:return:{"code":200}
"""
return self.call_api(
action=self.ACTION_MESSAGE_GROUP_PUBLISH,
params={
"fromUserId": from_user_id,
"toGroupId": to_group_id,
"objectName": object_name,
"content": content,
"pushContent": push_content if push_content is not None else '',
"pushData": push_data if push_data is not None else ''
}
)
def message_chatroom_publish(self, from_user_id,
to_chatroom_id,
object_name,
content):
return self.call_api(
action=self.ACTION_MESSAGE_CHATROOM_PUBLISH,
params={
"fromUserId": from_user_id,
"toChatroomId": to_chatroom_id,
"objectName": object_name,
"content": content
}
)
def message_history(self, date):
return self.call_api(
action=self.ACTION_MESSAGE_HISTORY,
params={
"date": date,
}
)
def group_sync(self, user_id, groups):
group_mapping = {"group[%s]" % k:v for k, v in groups.items()}
group_mapping.setdefault("userId", user_id)
return self.call_api(action=self.ACTION_GROUP_SYNC, params=group_mapping)
def group_create(self, user_id_list, group_id, group_name):
return self.call_api(action=self.ACTION_GROUP_CREATE, params={
"userId":user_id_list,
"groupId":group_id,
"groupName":group_name
})
def group_join(self, user_id_list, group_id, group_name):
return self.call_api(action=self.ACTION_GROUP_JOIN, params={
"userId":user_id_list,
"groupId":group_id,
"groupName":group_name
})
def group_quit(self, user_id_list, group_id):
return self.call_api(action=self.ACTION_GROUP_QUIT, params={
"userId": user_id_list,
"groupId": group_id
})
def group_dismiss(self, user_id, group_id):
"""将该群解散,所有用户都无法再接收该群的消息。
http://docs.rongcloud.cn/server.html#_解散群组_方法
:param user_id: 操作解散群的用户 Id。
:param group_id:要解散的群 Id。
:return:{"code":200}
"""
return self.call_api(action=self.ACTION_GROUP_DISMISS, params={
"userId":user_id,
"groupId":group_id,
})
def group_refresh(self, group_id, group_name):
return self.call_api(action=self.ACTION_GROUP_REFRESH, params={
"groupId": group_id,
"groupName": group_name
})
def chatroom_create(self, chatrooms):
"""创建聊天室 方法
http://docs.rongcloud.cn/server.html#_创建聊天室_方法
:param chatrooms: {'r001':'room1'} id:要创建的聊天室的id;name:要创建的聊天室的name
:return:{"code":200}
"""
chatroom_mapping = {'chatroom[%s]' % k:v for k, v in chatrooms.items()}
return self.call_api(action=self.ACTION_CHATROOM_CREATE, params=chatroom_mapping)
def chatroom_destroy(self, chatroom_id_list=None):
"""销毁聊天室 方法
当提交参数chatroomId多个时表示销毁多个聊天室
http://docs.rongcloud.cn/server.html#_销毁聊天室_方法
:param chatroom_id_list:要销毁的聊天室 Id。
:return:{"code":200}
"""
params={
"chatroomId":chatroom_id_list
} if chatroom_id_list is not None else {}
return self.call_api(action=self.ACTION_CHATROOM_DESTROY, params=params)
def chatroom_query(self, chatroom_id_list=None):
"""查询聊天室信息 方法
http://docs.rongcloud.cn/server.html#_查询聊天室信息_方法
:param chatroom_id_list:当提交多个时表示查询多个聊天室, 如果为None ,则查询所有聊天室
:return:{"code":200,"chatRooms":[{"chatroomId":"id1001","name":"name1","time":"2014-01-01 1:1:1"},{"chatroomId":"id1002","name":"name2","time":"2014-01-01 1:1:2"}]}
"""
params={
"chatroomId":chatroom_id_list
} if chatroom_id_list is not None else {}
return self.call_api(action=self.ACTION_CHATROOM_QUERY, params=params)