更新时间:2023-11-02 GMT+08:00
创建servicelayer连接器
- 新建SAPLogin函数用于自定义认证
函数配置如下:
函数代码如下:
# -*- coding:utf-8 -*- import json import base64 import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning requests.packages.urllib3.disable_warnings(InsecureRequestWarning) """" mssiAuthData参数样例 { "header":{}, // 连接器认证header参数 "path": {}, // 连接器认证path参数 "query":{}, // 连接器认证query参数 "body":{}, // 连接器认证body参数 "host":"https://demo.com // API主机地址 } """ def login(body, mssiAuthData): url = mssiAuthData.get("host") + "/b1s/v1/Login" headers = {'Content-Type': 'application/json'} if body.get("Language"): data = { "CompanyDB": body["CompanyDB"], "UserName": body["UserName"], "Password": body["Password"], "Language": int(body["Language"]) } else: data = { "CompanyDB": body["CompanyDB"], "UserName": body["UserName"], "Password": body["Password"], } session = requests.Session() session.post(url, json=data, headers=headers, verify=False) _cookies = [] for k, v in session.cookies.get_dict().items(): _cookies.append(k + "=" + v) return {'Cookie': ';'.join(_cookies)} def extractRequestParam(rawValue, encoded, defaultValue): if encoded and rawValue: rawValue = str(base64.b64decode(rawValue), "utf-8") return json.loads(rawValue) if rawValue else defaultValue ## 请勿对下面的函数做修改 def handler(event, context): """ 函数是方法的入口 :param event: 执行事件(event), 包含用户定义的函数参数以及所选择的的连接器认证相关参数 :param context: Runtime提供的函数执行上下文 :return: """ isBase64Encoded = event.get('isBase64Encoded', False) inputData = extractRequestParam(event.get('body'), isBase64Encoded, {}) # 用户定义的函数参数数据 mssiAuthData = extractRequestParam(event.get('mssiAuthData'), isBase64Encoded, {}) # 连接器认证数据 mssiAuthData["securityToken"] = context.getToken() dataExtendConfig = extractRequestParam(event.get('dataExtendConfig'), isBase64Encoded, {}) # 流步骤扩展参数 result = login(inputData, mssiAuthData) return json.dumps(result)
- 新建servicelayer连接器,连接器配置如下,认证函数选择刚才新建都SAPLogin函数:
父主题: 集成实施最佳实践