|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # Copyright (c) Alibaba, Inc. and its affiliates. |
| 3 | +from typing import Any, Dict, Union |
3 | 4 | from urllib.parse import urlencode |
4 | 5 |
|
| 6 | +import aiohttp |
| 7 | +import requests |
| 8 | + |
5 | 9 | import dashscope |
6 | 10 | from dashscope.api_entities.api_request_data import ApiRequestData |
| 11 | +from dashscope.api_entities.encryption import Encryption |
7 | 12 | from dashscope.api_entities.http_request import HttpRequest |
8 | 13 | from dashscope.api_entities.websocket_request import WebSocketRequest |
9 | 14 | from dashscope.common.constants import ( |
|
15 | 20 | from dashscope.common.error import InputDataRequired, UnsupportedApiProtocol |
16 | 21 | from dashscope.common.logging import logger |
17 | 22 | from dashscope.protocol.websocket import WebsocketStreamingMode |
18 | | -from dashscope.api_entities.encryption import Encryption |
19 | 23 |
|
20 | 24 |
|
21 | 25 | def _get_protocol_params(kwargs): |
@@ -70,35 +74,111 @@ def _get_protocol_params(kwargs): |
70 | 74 |
|
71 | 75 |
|
72 | 76 | def _build_api_request( # pylint: disable=too-many-branches |
| 77 | + # pylint: disable=too-many-arguments,too-many-locals |
73 | 78 | model: str, |
74 | 79 | input: object, # pylint: disable=redefined-builtin |
75 | 80 | task_group: str, |
76 | 81 | task: str, |
77 | 82 | function: str, |
78 | 83 | api_key: str, |
79 | | - is_service=True, |
| 84 | + is_service: bool = True, |
| 85 | + # Protocol and connection configuration |
| 86 | + api_protocol: ApiProtocol = ApiProtocol.HTTPS, |
| 87 | + http_method: HTTPMethod = HTTPMethod.POST, |
| 88 | + stream: bool = False, |
| 89 | + async_request: bool = False, |
| 90 | + request_timeout: int = None, |
| 91 | + # WebSocket specific |
| 92 | + ws_stream_mode: WebsocketStreamingMode = WebsocketStreamingMode.OUT, |
| 93 | + is_binary_input: bool = False, |
| 94 | + # HTTP specific |
| 95 | + query: bool = False, |
| 96 | + headers: Dict[str, str] = None, |
| 97 | + form: Dict = None, |
| 98 | + resources: Dict = None, |
| 99 | + base_address: str = None, |
| 100 | + flattened_output: bool = False, |
| 101 | + extra_url_parameters: Dict[str, Any] = None, |
| 102 | + user_agent: str = "", |
| 103 | + session: Union[requests.Session, aiohttp.ClientSession] = None, |
| 104 | + task_id: str = None, |
| 105 | + enable_encryption: bool = False, |
| 106 | + pre_task_id: str = None, |
| 107 | + # Additional parameters for API request data |
80 | 108 | **kwargs, |
81 | 109 | ): |
82 | | - ( |
83 | | - api_protocol, |
84 | | - ws_stream_mode, |
85 | | - is_binary_input, |
86 | | - http_method, |
87 | | - stream, |
88 | | - async_request, |
89 | | - query, |
90 | | - headers, |
91 | | - request_timeout, |
92 | | - form, |
93 | | - resources, |
94 | | - base_address, |
95 | | - flattened_output, |
96 | | - extra_url_parameters, |
97 | | - user_agent, |
98 | | - session, |
99 | | - ) = _get_protocol_params(kwargs) |
100 | | - task_id = kwargs.pop("task_id", None) |
101 | | - enable_encryption = kwargs.pop("enable_encryption", False) |
| 110 | + # pylint: disable=too-many-statements |
| 111 | + """Build API request object. |
| 112 | +
|
| 113 | + Args: |
| 114 | + model (str): The model name. |
| 115 | + input (object): The input data for the request. |
| 116 | + task_group (str): The task group for the API path. |
| 117 | + task (str): The task name for the API path. |
| 118 | + function (str): The function name for the API path. |
| 119 | + api_key (str): The API key for authentication. |
| 120 | + is_service (bool, optional): Whether this is a service call. |
| 121 | + Defaults to True. |
| 122 | + api_protocol (ApiProtocol, optional): The protocol to use |
| 123 | + (HTTP, HTTPS, WEBSOCKET). Defaults to ApiProtocol.HTTPS. |
| 124 | + http_method (HTTPMethod, optional): The HTTP method (GET, POST). |
| 125 | + Defaults to HTTPMethod.POST. |
| 126 | + stream (bool, optional): Enable streaming output. |
| 127 | + Defaults to False. |
| 128 | + async_request (bool, optional): Enable async request. |
| 129 | + Defaults to False. |
| 130 | + request_timeout (int, optional): Request timeout in seconds. |
| 131 | + Defaults to None. |
| 132 | + ws_stream_mode (WebsocketStreamingMode, optional): WebSocket |
| 133 | + streaming mode. Defaults to WebsocketStreamingMode.OUT. |
| 134 | + is_binary_input (bool, optional): Whether input is binary data. |
| 135 | + Defaults to False. |
| 136 | + query (bool, optional): Whether this is a query request. |
| 137 | + Defaults to False. |
| 138 | + headers (Dict[str, str], optional): Additional HTTP headers. |
| 139 | + Defaults to None. |
| 140 | + form (Dict, optional): Form data for multipart requests. |
| 141 | + Defaults to None. |
| 142 | + resources (Dict, optional): Resource data. Defaults to None. |
| 143 | + base_address (str, optional): Custom base URL for the API. |
| 144 | + Defaults to None. |
| 145 | + flattened_output (bool, optional): Whether to flatten output. |
| 146 | + Defaults to False. |
| 147 | + extra_url_parameters (Dict[str, Any], optional): Extra URL query |
| 148 | + parameters. Defaults to None. |
| 149 | + user_agent (str, optional): Custom user agent string. |
| 150 | + Defaults to "". |
| 151 | + session (Union[requests.Session, aiohttp.ClientSession], optional): |
| 152 | + Custom session for connection reuse. Defaults to None. |
| 153 | + task_id (str, optional): Task ID for the request. |
| 154 | + Defaults to None. |
| 155 | + enable_encryption (bool, optional): Enable request encryption. |
| 156 | + Defaults to False. |
| 157 | + pre_task_id (str, optional): Previous task ID for WebSocket. |
| 158 | + Defaults to None. |
| 159 | + **kwargs: Additional parameters passed to the API request data. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + HttpRequest or WebSocketRequest: The constructed request object. |
| 163 | +
|
| 164 | + Raises: |
| 165 | + InputDataRequired: If input data is missing or invalid. |
| 166 | + UnsupportedApiProtocol: If the API protocol is not supported. |
| 167 | + """ |
| 168 | + # Handle stream mode for WebSocket |
| 169 | + if not stream and ws_stream_mode == WebsocketStreamingMode.OUT: |
| 170 | + ws_stream_mode = WebsocketStreamingMode.NONE |
| 171 | + |
| 172 | + # Handle user_agent from headers |
| 173 | + if headers and "user-agent" in headers: |
| 174 | + header_ua = headers.pop("user-agent") |
| 175 | + if user_agent: |
| 176 | + user_agent = ( |
| 177 | + f"{header_ua}; {user_agent}" if header_ua else user_agent |
| 178 | + ) |
| 179 | + else: |
| 180 | + user_agent = header_ua |
| 181 | + |
102 | 182 | encryption = None |
103 | 183 |
|
104 | 184 | if api_protocol in [ApiProtocol.HTTP, ApiProtocol.HTTPS]: |
@@ -146,7 +226,6 @@ def _build_api_request( # pylint: disable=too-many-branches |
146 | 226 | websocket_url = base_address |
147 | 227 | else: |
148 | 228 | websocket_url = dashscope.base_websocket_api_url |
149 | | - pre_task_id = kwargs.pop("pre_task_id", None) |
150 | 229 | request = WebSocketRequest( |
151 | 230 | url=websocket_url, |
152 | 231 | api_key=api_key, |
|
0 commit comments