mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
3.2.0
This commit is contained in:
21
sop-sdk/sdk-python/common/JsonUtil.py
Normal file
21
sop-sdk/sdk-python/common/JsonUtil.py
Normal file
@@ -0,0 +1,21 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import json
|
||||
|
||||
|
||||
def to_json_string(obj):
|
||||
"""将对象转换成json字符串
|
||||
|
||||
:param obj: 对象
|
||||
:type obj: object
|
||||
|
||||
:return: 返回json
|
||||
:rtype: str
|
||||
"""
|
||||
if isinstance(obj, dict):
|
||||
param = obj
|
||||
else:
|
||||
param = obj.__dict__
|
||||
return json.dumps(param, ensure_ascii=False)
|
||||
|
122
sop-sdk/sdk-python/common/OpenClient.py
Normal file
122
sop-sdk/sdk-python/common/OpenClient.py
Normal file
@@ -0,0 +1,122 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import json
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from common import SignUtil, RequestTypes
|
||||
from common.RequestType import RequestType
|
||||
|
||||
_headers = {'Accept-Encoding': 'identity'}
|
||||
|
||||
|
||||
class OpenClient:
|
||||
"""调用客户端"""
|
||||
__app_id = ''
|
||||
__private_key = ''
|
||||
__url = ''
|
||||
|
||||
def __init__(self, app_id, private_key, url):
|
||||
"""客户端
|
||||
|
||||
:param app_id: 应用ID
|
||||
:type app_id: str
|
||||
|
||||
:param private_key: 应用私钥
|
||||
:type private_key: str
|
||||
|
||||
:param url: 请求URL
|
||||
:type url: str
|
||||
"""
|
||||
self.__app_id = app_id
|
||||
self.__private_key = private_key
|
||||
self.__url = url
|
||||
|
||||
def execute(self, request, token=None):
|
||||
"""
|
||||
|
||||
:param request: 请求对象,BaseRequest的子类
|
||||
|
||||
:param token: (Optional) token
|
||||
:type token: str
|
||||
|
||||
:return: 返回请求结果
|
||||
:rtype: BaseResponse
|
||||
"""
|
||||
biz_model = request.biz_model
|
||||
request_type = request.get_request_type()
|
||||
if not isinstance(request_type, RequestType):
|
||||
raise Exception('get_request_type返回错误类型,正确方式:RequestTypes.XX')
|
||||
|
||||
params = biz_model.__dict__
|
||||
if request.files is not None:
|
||||
response = self._post_file(request, params, token)
|
||||
elif request_type == RequestTypes.GET:
|
||||
response = self._get(request, params, token)
|
||||
elif request_type == RequestTypes.POST_FORM:
|
||||
response = self._post_form(request, params, token)
|
||||
elif request_type == RequestTypes.POST_JSON:
|
||||
response = self._post_json(request, params, token)
|
||||
elif request_type == RequestTypes.POST_UPLOAD:
|
||||
response = self._post_file(request, params, token)
|
||||
else:
|
||||
raise Exception('get_request_type设置错误')
|
||||
|
||||
return self._parse_response(response, request)
|
||||
|
||||
def _get(self, request, params, token):
|
||||
all_params = self._build_params(request, params, token)
|
||||
return requests.get(self.__url, all_params, headers=_headers).text
|
||||
|
||||
def _post_form(self, request, params, token):
|
||||
all_params = self._build_params(request, params, token)
|
||||
return requests.post(self.__url, data=all_params, headers=_headers).text
|
||||
|
||||
def _post_json(self, request, params, token):
|
||||
all_params = self._build_params(request, params, token)
|
||||
return requests.post(self.__url, json=all_params, headers=_headers).text
|
||||
|
||||
def _post_file(self, request, params, token):
|
||||
all_params = self._build_params(request, params, token)
|
||||
return requests.request('POST', self.__url, data=all_params, files=request.files, headers=_headers).text
|
||||
|
||||
def _build_params(self, request, params, token):
|
||||
"""构建所有的请求参数
|
||||
|
||||
:param request: 请求对象
|
||||
:type request: request.BaseRequest
|
||||
|
||||
:param params: 业务请求参数
|
||||
:type params: dict
|
||||
|
||||
:param token: token
|
||||
:type token: str
|
||||
|
||||
:return: 返回请求参数
|
||||
:rtype: str
|
||||
"""
|
||||
all_params = {
|
||||
'app_id': self.__app_id,
|
||||
'method': request.get_method(),
|
||||
'charset': 'UTF-8',
|
||||
'sign_type': 'RSA2',
|
||||
'timestamp': time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
|
||||
'version': request.get_version()
|
||||
}
|
||||
|
||||
if token is not None:
|
||||
all_params['access_token'] = token
|
||||
|
||||
# 添加业务参数
|
||||
all_params.update(params)
|
||||
|
||||
# 构建sign
|
||||
sign = SignUtil.create_sign(all_params, self.__private_key, 'RSA2')
|
||||
all_params['sign'] = sign
|
||||
return all_params
|
||||
|
||||
def _parse_response(self, resp, request):
|
||||
response_dict = json.loads(resp)
|
||||
return request.parse_response(response_dict)
|
8
sop-sdk/sdk-python/common/RequestType.py
Normal file
8
sop-sdk/sdk-python/common/RequestType.py
Normal file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
|
||||
class RequestType:
|
||||
|
||||
def __init__(self):
|
||||
pass
|
6
sop-sdk/sdk-python/common/RequestTypes.py
Normal file
6
sop-sdk/sdk-python/common/RequestTypes.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from common.RequestType import RequestType
|
||||
|
||||
GET = RequestType()
|
||||
POST_JSON = RequestType()
|
||||
POST_FORM = RequestType()
|
||||
POST_UPLOAD = RequestType()
|
102
sop-sdk/sdk-python/common/SignUtil.py
Normal file
102
sop-sdk/sdk-python/common/SignUtil.py
Normal file
@@ -0,0 +1,102 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
import rsa
|
||||
import base64
|
||||
|
||||
__pem_begin = '-----BEGIN RSA PRIVATE KEY-----\n'
|
||||
__pem_end = '\n-----END RSA PRIVATE KEY-----'
|
||||
|
||||
|
||||
def create_sign(all_params, private_key, sign_type):
|
||||
"""创建签名
|
||||
|
||||
:param all_params: 参数
|
||||
:type all_params: dict
|
||||
|
||||
:param private_key: 私钥字符串
|
||||
:type private_key: str
|
||||
|
||||
:param sign_type: 签名类型,'RSA', 'RSA2'二选一
|
||||
:type sign_type: str
|
||||
|
||||
:return: 返回签名内容
|
||||
:rtype: str
|
||||
"""
|
||||
sign_content = get_sign_content(all_params)
|
||||
private_key = _format_private_key(private_key)
|
||||
return sign(sign_content, private_key, sign_type)
|
||||
|
||||
|
||||
def _format_private_key(private_key):
|
||||
if not private_key.startswith(__pem_begin):
|
||||
private_key = __pem_begin + private_key
|
||||
if not private_key.endswith(__pem_end):
|
||||
private_key = private_key + __pem_end
|
||||
return private_key
|
||||
|
||||
|
||||
def get_sign_content(params):
|
||||
"""构建签名内容
|
||||
|
||||
1.筛选并排序
|
||||
获取所有请求参数,不包括字节类型参数,如文件、字节流,剔除sign字段,剔除值为空的参数,并按照参数名ASCII码递增排序(字母升序排序),
|
||||
如果遇到相同字符则按照第二个字符的键值ASCII码递增排序,以此类推。
|
||||
|
||||
2.拼接
|
||||
将排序后的参数与其对应值,组合成“参数=参数值”的格式,并且把这些参数用&字符连接起来,此时生成的字符串为待签名字符串。
|
||||
|
||||
:param params: 参数
|
||||
:type params: dict
|
||||
|
||||
:return: 返回签名内容
|
||||
:rtype: str
|
||||
"""
|
||||
keys = params.keys()
|
||||
keys.sort()
|
||||
result = []
|
||||
for key in keys:
|
||||
value = str(params.get(key))
|
||||
if len(value) > 0:
|
||||
result.append(key + '=' + value)
|
||||
|
||||
return '&'.join(result)
|
||||
|
||||
|
||||
def sign(content, private_key, sign_type):
|
||||
"""签名
|
||||
|
||||
:param content: 签名内容
|
||||
:type content: str
|
||||
|
||||
:param private_key: 私钥字符串
|
||||
:type private_key: str
|
||||
|
||||
:param sign_type: 签名类型,'RSA', 'RSA2'二选一
|
||||
:type sign_type: str
|
||||
|
||||
:return: 返回签名内容
|
||||
:rtype: str
|
||||
"""
|
||||
if sign_type.upper() == 'RSA':
|
||||
return rsa_sign(content, private_key, 'SHA-1')
|
||||
elif sign_type.upper() == 'RSA2':
|
||||
return rsa_sign(content, private_key, 'SHA-256')
|
||||
else:
|
||||
raise Exception('sign_type错误')
|
||||
|
||||
|
||||
def rsa_sign(content, private_key, hash):
|
||||
"""SHAWithRSA
|
||||
|
||||
:param content: 签名内容
|
||||
:type content: str
|
||||
|
||||
:param private_key: 私钥
|
||||
:type private_key: str
|
||||
|
||||
:return: 签名内容
|
||||
:rtype: str
|
||||
"""
|
||||
pri_key = rsa.PrivateKey.load_pkcs1(private_key.encode('utf-8'))
|
||||
sign_result = rsa.sign(content, pri_key, hash)
|
||||
return base64.b64encode(sign_result)
|
0
sop-sdk/sdk-python/common/__init__.py
Normal file
0
sop-sdk/sdk-python/common/__init__.py
Normal file
Reference in New Issue
Block a user