# 使用Python-SDK ## 一、概述 易宝开放平台官方SDK包含了请求的封装,签名加密,响应解释,性能优化等等功能,使用SDK可以轻松、快速完成API的调用和结果处理。 **SDK 通常可以分为系统 SDK、应用 SDK 和业务 SDK。** * 系统 SDK 是为特定的软件包、软件框架、硬件平台、操作系统等的应用时所使用的开发工具集合。 * 应用 SDK 是基于系统 SDK 开发的独立于具体业务而具有特定功能的开发工具集合。 * 业务 SDK 是基于应用 SDK 开发的与具体业务相关且具有特定功能的开发工具集合。 ## 二、环境要求 * Python 3.5+ * Python 2.7+ (不能保证停止维护的Python版本有效,请参考[https://www.python.org/doc/sunset-python-2](javascript:goToUrl('https://www.python.org/doc/sunset-python-2/');) ## 三、SDK使用 #### 下载【基础SDK】 [PyPI](javascript:goToUrl('https://pypi.org/project/yop-python-sdk/');)、[源码](javascript:goToUrl('https://github.com/yop-platform/yop-python-sdk');) #### 使用【基础SDK】 下载基础SDK后,查看README.md了解具体使用方法。 ## 四、SDK配置 SDK 默认从路径(相对根目录) config/yop_sdk_config_rsa_prod.json 读取配置文件。 下载SDK后,打开yop_sdk_config_rsa_prod.json文件,并修改 isv_private_key 中的 app_key、value,SDK会自动加载您的配置信息。 ### 配置文件内容 * 通常情况下只需要修改配置文件中 isv_private_key 中的 app_key、value 即可,参数整体说明如下 ``` { "server_root": 普通接口请求路径,默认https://openapi.yeepay.com/yop-center "yos_server_root": 文件类接口请求路径,默认https://yos.yeepay.com/yop-center "yop_public_key": 易宝公钥,对响应结果验签时需要提供 [ { "store_type": 密钥存储类型 "cert_type": "RSA2048" "serial_no": 密钥证书序列号 "value": 密钥值 } ], "isv_private_key": 商户私钥列表,当认证鉴权机制为非对称时需要提供 [ { "app_key": 应用标识(设置ISV密钥的默认应用app_key) "store_type": "string" 密钥文本 "cert_type": "RSA2048" "value": 商户公钥文本 } ], "http_client": { "connect_timeout": 连接超时时间,默认10000ms "read_timeout": 读取超时时间,默认30000ms "max_conn_total": 最大连接总数,默认200 "max_conn_per_route": 单域名最大连接数,默认100 } } ``` ## 五、对接API ### 【基础SDK】 [示例代码](javascript:goToUrl('https://github.com/yop-platform/yop-python-sdk/tree/develop/tests');),请参考 tests 目录下面的单元测试,包含 GET、POST、文件上传、文件下载等接口 #### 普通GET请求调用示例 ``` def test_get_v3(self, client): api = '/rest/v1.0/trade/order/query' params = {'merchantNo': '100800xxxxx', 'parentMerchantNo': '100800xxxx', 'orderId': 'order_xxxx'} res = client.get(api, params) if 'prod' == client.env: if 'sm' == client.cert_type: assertion.failure(res, '40029') else: assertion.success(res) assert 'OPR00000' == res['result']['code'] else: assert 'OPR00000' == res['result']['code'] ``` #### 上传接口调用示例 ``` def test_upload_remote_file(self, client): file_url = 'https://www.yeepay.com/static/images/logo.png' file = urllib.request.urlopen(file_url) api = '/yos/v1.0/mer/merchant/qual/upload' params = { 'merQual': ('file_name', file.read(), 'multipart/form-data'), 'remark': '演示普通参数传递,该api没有remark参数' } res = client.upload(api, params) assertion.success(res) ``` #### 下载接口调用示例 ``` def test_download(self, client): api = '/yos/v1.0/balance/yop-simple-remit/download-electronic-receipt' params = { 'batchNo': 'xxx', 'orderId': 'xxx', } file_path = os.environ['HOME'] res = client.download(api, params, credentials, file_path=file_path) assertion.failure(res, '40047') ```