2021-03-24 11:01:35 +00:00
|
|
|
import hashlib
|
|
|
|
import inspect
|
|
|
|
from inspect import Parameter
|
|
|
|
|
|
|
|
from common.utils.common import get_logger
|
2021-10-21 08:50:11 +00:00
|
|
|
from common.sdk.im import exceptions as exce
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
2021-10-18 03:25:39 +00:00
|
|
|
def digest(corp_id, corp_secret):
|
2021-03-24 11:01:35 +00:00
|
|
|
md5 = hashlib.md5()
|
2021-10-18 03:25:39 +00:00
|
|
|
md5.update(corp_id.encode())
|
|
|
|
md5.update(corp_secret.encode())
|
|
|
|
dist = md5.hexdigest()
|
|
|
|
return dist
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def update_values(default: dict, others: dict):
|
|
|
|
for key in default.keys():
|
|
|
|
if key in others:
|
|
|
|
default[key] = others[key]
|
|
|
|
|
|
|
|
|
|
|
|
def set_default(data: dict, default: dict):
|
|
|
|
for key in default.keys():
|
|
|
|
if key not in data:
|
|
|
|
data[key] = default[key]
|
|
|
|
|
|
|
|
|
|
|
|
class DictWrapper:
|
2021-12-13 07:52:54 +00:00
|
|
|
def __init__(self, data: dict):
|
2021-03-24 11:01:35 +00:00
|
|
|
self.raw_data = data
|
|
|
|
|
|
|
|
def __getitem__(self, item):
|
|
|
|
# 网络请求返回的数据,不能完全信任,所以字典操作包在异常里
|
|
|
|
try:
|
|
|
|
return self.raw_data[item]
|
|
|
|
except KeyError as e:
|
|
|
|
msg = f'Response 200 but get field from json error: error={e} data={self.raw_data}'
|
|
|
|
logger.error(msg)
|
2021-05-17 06:20:51 +00:00
|
|
|
raise exce.ResponseDataKeyError(detail=self.raw_data)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
def __getattr__(self, item):
|
|
|
|
return getattr(self.raw_data, item)
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.raw_data
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return str(self.raw_data)
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-12-13 07:52:54 +00:00
|
|
|
return repr(self.raw_data)
|
2021-03-24 11:01:35 +00:00
|
|
|
|
|
|
|
|
2021-08-12 08:44:06 +00:00
|
|
|
def as_request(func):
|
2021-03-24 11:01:35 +00:00
|
|
|
def inner(*args, **kwargs):
|
|
|
|
signature = inspect.signature(func)
|
|
|
|
bound_args = signature.bind(*args, **kwargs)
|
|
|
|
bound_args.apply_defaults()
|
|
|
|
|
|
|
|
arguments = bound_args.arguments
|
|
|
|
self = arguments['self']
|
|
|
|
request_method = func.__name__
|
|
|
|
|
|
|
|
parameters = {}
|
|
|
|
for k, v in signature.parameters.items():
|
|
|
|
if k == 'self':
|
|
|
|
continue
|
|
|
|
if v.kind is Parameter.VAR_KEYWORD:
|
|
|
|
parameters.update(arguments[k])
|
|
|
|
continue
|
|
|
|
parameters[k] = arguments[k]
|
|
|
|
|
|
|
|
response = self.request(request_method, **parameters)
|
|
|
|
return response
|
|
|
|
return inner
|