### Initialize Dada Client Source: https://github.com/wandoubleming/dada_openapi_python/blob/master/README.md Create the client instance using the appropriate configuration object. ```python dada_client = DadaApiClient(QAConfig) # 根据环境选择合适的配置对象 ``` -------------------------------- ### Initialize Configuration Source: https://github.com/wandoubleming/dada_openapi_python/blob/master/README.md Define the required credentials and host information for the Dada API. ```python APP_KEY = "" APP_SECRET = "" HOST = "" SOURCE_ID = "" ``` -------------------------------- ### Initialize API Class Source: https://github.com/wandoubleming/dada_openapi_python/blob/master/README.md Instantiate the API class with the populated model. ```python order_add_api = OrderAddClass(model=order_model) ``` -------------------------------- ### Initialize DadaApiClient Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt The DadaApiClient handles communication, signing, and parameter assembly. Use is_user_source_id=False when registering a new merchant. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import QAConfig, OnlineConfig # 测试环境初始化 dada_client = DadaApiClient(QAConfig) # 生产环境初始化 dada_client = DadaApiClient(OnlineConfig) # 注册商户时不需要 source_id dada_client = DadaApiClient(QAConfig, is_user_source_id=False) ``` -------------------------------- ### Initialize Order Model Source: https://github.com/wandoubleming/dada_openapi_python/blob/master/README.md Create and populate an order model instance. Ensure field_check() is implemented for mandatory parameter validation. ```python # 新增订单模型 order_model = OrderModel() order_model.shop_no = "11664071" order_model.origin_id = "test0000000001" order_model.cargo_price = 11 order_model.city_code = "021" order_model.is_prepay = 0 order_model.receiver_name = "测试达达" order_model.receiver_address = "虹口足球场" order_model.receiver_lat = 31.228623 order_model.receiver_lng = 121.587172 order_model.receiver_phone = "13798061234" order_model.callback = "" ``` -------------------------------- ### Manage API Configuration Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Extend BaseConfig to define custom API keys and host addresses. Pre-configured QA and Online settings are available for quick environment switching. ```python from open_api.dada_config import BaseConfig, QAConfig, OnlineConfig # 自定义配置类 class CustomConfig(BaseConfig): APP_KEY = "your_app_key" APP_SECRET = "your_app_secret" HOST = "http://newopen.imdada.cn" SOURCE_ID = "your_source_id" # 预置测试环境配置 # QAConfig.HOST = "http://newopen.qa.imdada.cn" # 预置生产环境配置 # OnlineConfig.HOST = "http://newopen.imdada.cn" ``` -------------------------------- ### Create Order API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Use OrderAddClass to create delivery orders. Provide necessary order details including shop_no, origin_id, and receiver information. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import QAConfig from open_api.api.order.order import OrderAddClass from open_api.model.order.order import OrderModel # 初始化客户端 dada_client = DadaApiClient(QAConfig) # 构建订单数据模型 order_model = OrderModel() order_model.shop_no = "11664071" # 门店编号 order_model.origin_id = "order20231201001" # 第三方订单编号 order_model.cargo_price = 99.5 # 货物价格 order_model.city_code = "021" # 城市编码(上海) order_model.is_prepay = 0 # 是否预付(0-否,1-是) order_model.receiver_name = "王五" # 收货人姓名 order_model.receiver_address = "虹口足球场1号门" # 收货地址 order_model.receiver_lat = 31.228623 # 收货地址纬度 order_model.receiver_lng = 121.587172 # 收货地址经度 order_model.receiver_phone = "13798061234" # 收货人电话 order_model.callback = "https://your-domain.com/callback" # 订单状态回调地址 # 创建 API 实例并发起请求 order_api = OrderAddClass(model=order_model) result = dada_client.do_rpc(api=order_api) print(result.to_string()) # 响应示例: {"status": "success", "code": 0, "msg": "成功", "result": {"distance": 1200, "fee": 8.5, "deliverFee": 8.5}} ``` -------------------------------- ### Execute RPC Request Source: https://github.com/wandoubleming/dada_openapi_python/blob/master/README.md Perform the RPC call using the client and API instance, then output the result. ```python result = dada_client.do_rpc(api=order_add_api) print result.to_string() ``` -------------------------------- ### List Available API Endpoints Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Provides a reference list of URI constants for various order, merchant, and utility API endpoints. ```python # 订单相关 API ORDER_ADD_URI = "/api/order/addOrder" # 新增订单 ORDER_RE_ADD_URI = "/api/order/reAddOrder" # 重新发布订单 ORDER_QUERY_DELIVER_FEE_URI = "/api/order/queryDeliverFee" # 查询运费 ORDER_ADD_AFTER_QUERY_FEE_URI = "/api/order/addAfterQuery" # 查询后下单 ORDER_ADD_TIP_URI = "/api/order/addTip" # 增加小费 ORDER_DETAIL_QUERY_URI = "/api/order/status/query" # 查询订单状态 ORDER_CANCEL_URI = "/api/order/formalCancel" # 取消订单 ORDER_CANCEL_REASON_URI = "/api/order/cancel/reasons" # 取消原因列表 ORDER_APPOINT_URI = "/api/order/appoint/exist" # 查询是否有指定配送员 ORDER_CANCEL_APPOINT_URI = "/api/order/appoint/cancel" # 取消指定配送员 ORDER_CAN_APPOINT_TRANSPORTER_URI = "/api/order/appoint/list/transporter" # 可指定配送员列表 # 商户相关 API MERCHANT_REGISTER_URI = "/merchantApi/merchant/add" # 注册商户 MERCHANT_ADD_SHOP_URI = "/api/shop/add" # 新增门店 MERCHANT_UPDATE_SHOP_URI = "/api/shop/update" # 更新门店 MERCHANT_SHOP_DETAIL_URI = "/api/shop/detail" # 门店详情 MERCHANT_RECHARGE_URI = "/api/recharge" # 充值 MERCHANT_BALANCE_QUERY_URI = "/api/balance/query" # 查询余额 # 其他 API ORDER_CITY_CODE_QUERY_URI = "/api/cityCode/list" # 城市编码列表 ORDER_COMPLAINT_DADA_URL = "/api/complaint/dada" # 投诉达达 ORDER_COMPLAINT_REASON_URL = "/api/complaint/reasons" # 投诉原因列表 ``` -------------------------------- ### Create Order API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Creates a new delivery order on the Dada platform. ```APIDOC ## POST /api/order/add ### Description Creates a delivery order. ### Method POST ### Request Body - **shop_no** (string) - Required - Shop number - **origin_id** (string) - Required - Third-party order ID - **cargo_price** (float) - Required - Cargo price - **city_code** (string) - Required - City code - **is_prepay** (int) - Required - Prepay flag - **receiver_name** (string) - Required - Receiver name - **receiver_address** (string) - Required - Receiver address - **receiver_lat** (float) - Required - Receiver latitude - **receiver_lng** (float) - Required - Receiver longitude - **receiver_phone** (string) - Required - Receiver phone - **callback** (string) - Optional - Callback URL ### Response #### Success Response (200) - **result** (object) - Contains distance, fee, and deliverFee ### Response Example { "status": "success", "code": 0, "msg": "成功", "result": { "distance": 1200, "fee": 8.5, "deliverFee": 8.5 } } ``` -------------------------------- ### Query City Codes with DadaApiClient Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Initializes the client and executes a city code query API call. This operation does not require a specific data model. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import QAConfig from open_api.api.city_code import CityQueryApiClass # 初始化客户端 dada_client = DadaApiClient(QAConfig) # 城市查询不需要参数模型 city_api = CityQueryApiClass() result = dada_client.do_rpc(api=city_api) print(result.to_string()) # 响应示例: {"status": "success", "code": 0, "result": [{"cityName": "上海", "cityCode": "021"}, ...]} ``` -------------------------------- ### Add Shop API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Use ShopAddApiClass to register new shop locations under a merchant. Ensure all required location and contact fields are populated in the model. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import QAConfig from open_api.api.merchant.shop_add import ShopAddApiClass from open_api.model.merchant.shop_add import ShopAddModel # 初始化客户端 dada_client = DadaApiClient(QAConfig) # 构建门店数据模型 shop_model = ShopAddModel() shop_model.station_name = "测试门店" shop_model.business = 1 # 业务类型 shop_model.city_name = "上海" shop_model.area_name = "浦东新区" shop_model.station_address = "上海市浦东新区张江路100号" shop_model.lng = 121.587172 # 经度 shop_model.lat = 31.228623 # 纬度 shop_model.contact_name = "李四" shop_model.phone = "13900139000" # 创建 API 实例并发起请求 shop_api = ShopAddApiClass(model=shop_model) result = dada_client.do_rpc(api=shop_api) print(result.to_string()) # 响应示例: {"status": "success", "code": 0, "msg": "成功", "result": {"success": [{"originShopId": "shop001"}]}} ``` -------------------------------- ### Handle API Responses and Errors Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Demonstrates how to process DadaRpcResponse objects and implement error handling logic for API calls. ```python from open_api.dada_response import DadaRpcResponse # 响应对象属性 # result.status - 请求状态: "success" 或 "fail" # result.code - 状态码: 0 表示成功 # result.msg - 状态描述信息 # result.result - 业务返回数据 # 完整的错误处理示例 def create_order_with_error_handling(): dada_client = DadaApiClient(QAConfig) order_api = OrderAddClass(model=order_model) result = dada_client.do_rpc(api=order_api) if result.status == "success" and result.code == 0: print("订单创建成功:", result.result) elif result.code == -2: print("网络异常:", result.msg) else: print("业务错误 [%s]: %s" % (result.code, result.msg)) return result ``` -------------------------------- ### Create Custom Data Models Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Extends BaseModel to define custom request structures, requiring implementation of the field_check method for validation. ```python from open_api.model.base import BaseModel class CustomOrderModel(BaseModel): def __init__(self): self.order_id = None self.amount = None self.description = None def field_check(self): """校验必填参数""" if self.order_id is None: raise TypeError("order_id value can not be null") if self.amount is None: raise TypeError("amount value can not be null") return True # 使用示例 model = CustomOrderModel() model.order_id = "12345" model.amount = 100.0 model.description = "测试订单" # 转换为 JSON 字符串 json_str = model.to_json() # 输出: '{"order_id":"12345","amount":100.0,"description":"测试订单"}' # 转换为字典 dict_data = model.to_dict() # 输出: {"order_id": "12345", "amount": 100.0, "description": "测试订单"} ``` -------------------------------- ### Register Merchant API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Use MerchantAddApiClass to register a new merchant. This operation does not require a source_id during initialization. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import QAConfig from open_api.api.merchant.merchant_add import MerchantAddApiClass from open_api.model.merchant.merchant_add import MerchantAddModel # 初始化客户端(注册商户不需要 source_id) dada_client = DadaApiClient(QAConfig, is_user_source_id=False) # 构建商户数据模型 merchant_model = MerchantAddModel() merchant_model.mobile = "13800138000" merchant_model.city_name = "上海" merchant_model.enterprise_name = "测试企业名称" merchant_model.enterprise_address = "上海市浦东新区张江高科技园区" merchant_model.contact_name = "张三" merchant_model.contact_phone = "13800138000" merchant_model.email = "test@example.com" # 创建 API 实例并发起请求 merchant_api = MerchantAddApiClass(model=merchant_model) result = dada_client.do_rpc(api=merchant_api) # 处理响应结果 print(result.to_string()) # 响应示例: {"status": "success", "code": 0, "msg": "成功", "result": {"source_id": "73758"}} ``` -------------------------------- ### Add Shop API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Adds a new shop location for a registered merchant. ```APIDOC ## POST /api/shop/add ### Description Adds a new shop for the merchant. ### Method POST ### Request Body - **station_name** (string) - Required - Shop name - **business** (int) - Required - Business type - **city_name** (string) - Required - City name - **area_name** (string) - Required - Area name - **station_address** (string) - Required - Shop address - **lng** (float) - Required - Longitude - **lat** (float) - Required - Latitude - **contact_name** (string) - Required - Contact name - **phone** (string) - Required - Contact phone ### Response #### Success Response (200) - **result** (object) - Contains success list with originShopId ### Response Example { "status": "success", "code": 0, "msg": "成功", "result": { "success": [ { "originShopId": "shop001" } ] } } ``` -------------------------------- ### Update Shop API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Updates information for an existing shop. ```APIDOC ## POST /api/shop/update ### Description Updates existing shop details. ### Method POST ### Request Body - **origin_shop_id** (string) - Required - Original shop ID - **city_name** (string) - Optional - City name - **area_name** (string) - Optional - Area name ### Response #### Success Response (200) - **status** (string) - Status message ### Response Example { "status": "success", "code": 0, "msg": "成功", "result": null } ``` -------------------------------- ### Merchant Registration API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Registers a new merchant on the Dada platform to obtain a source_id for subsequent operations. ```APIDOC ## POST /api/merchant/add ### Description Registers a new merchant on the Dada platform. ### Method POST ### Request Body - **mobile** (string) - Required - Merchant mobile number - **city_name** (string) - Required - City name - **enterprise_name** (string) - Required - Enterprise name - **enterprise_address** (string) - Required - Enterprise address - **contact_name** (string) - Required - Contact person name - **contact_phone** (string) - Required - Contact phone number - **email** (string) - Required - Contact email ### Response #### Success Response (200) - **status** (string) - Status message - **code** (int) - Response code - **msg** (string) - Message - **result** (object) - Contains source_id ### Response Example { "status": "success", "code": 0, "msg": "成功", "result": { "source_id": "73758" } } ``` -------------------------------- ### Update Shop API Source: https://context7.com/wandoubleming/dada_openapi_python/llms.txt Use ShopUpdateApiClass to modify existing shop details. The origin_shop_id is mandatory to identify the target shop. ```python from open_api.dada_client import DadaApiClient from open_api.dada_config import OnlineConfig from open_api.api.merchant.shop_update import ShopUpdateApiClass from open_api.model.merchant.shop_update import ShopUpdateModel # 初始化客户端(生产环境) dada_client = DadaApiClient(OnlineConfig) # 构建门店更新数据模型 shop_update_model = ShopUpdateModel() shop_update_model.origin_shop_id = "shop001" # 必填:原门店编号 shop_update_model.city_name = "上海" shop_update_model.area_name = "黄浦区" # 创建 API 实例并发起请求 shop_update_api = ShopUpdateApiClass(model=shop_update_model) result = dada_client.do_rpc(api=shop_update_api) print(result.to_string()) # 响应示例: {"status": "success", "code": 0, "msg": "成功", "result": null} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.