Skip to content

Commit 2fc79c6

Browse files
Merge pull request #185 from dashscope/dev/issues-7
Merge SDK headers into single x-dashscope-sdk-client field
2 parents 41d4eec + 13a16db commit 2fc79c6

30 files changed

Lines changed: 1583 additions & 40 deletions

‎dashscope/acli/providers/tongyi.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,14 @@ def __init__(
4848
request_timeout: int = 60,
4949
protocol: str = "openai",
5050
base_url: str | None = None,
51+
module: str = "app",
5152
):
5253
self.model = model
5354
self.api_key = api_key
5455
self.request_timeout = request_timeout
5556
self.protocol = protocol
5657
self.base_url = (base_url or DASHSCOPE_BASE_URL).rstrip("/")
58+
self.module = module
5759

5860
def _convert_tools(self, tools: list[dict] | None) -> list[dict] | None:
5961
if not tools:
@@ -126,8 +128,10 @@ def _get_headers(self) -> dict:
126128
if self.api_key:
127129
headers["Authorization"] = f"Bearer {self.api_key}"
128130
if not os.environ.get("DASHSCOPE_DISABLE_SDK_HEADERS"):
129-
headers["x-dashscope-sdk-client"] = "acli"
130-
headers["x-dashscope-sdk-version"] = __version__
131+
parts = ["acli", __version__]
132+
if self.module:
133+
parts.append(self.module)
134+
headers["x-dashscope-sdk-client"] = "/".join(parts)
131135
headers["x-dashscope-sdk-session-id"] = SDK_SESSION_ID
132136
return headers
133137

‎dashscope/agentstudio/transport.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def build_headers(
230230
HEADER_AUTHORIZATION: f"Bearer {api_key}",
231231
HEADER_USER_AGENT: user_agent,
232232
}
233-
headers.update(get_sdk_headers())
233+
headers.update(get_sdk_headers(module="agentstudio"))
234234
if json_body:
235235
headers["Content-Type"] = "application/json"
236236
headers["Accept"] = "application/json"

‎dashscope/api_entities/api_request_factory.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def _build_api_request( # pylint: disable=too-many-branches
5353
task_id: Optional[str] = None,
5454
enable_encryption: bool = False,
5555
pre_task_id: Optional[str] = None,
56+
# SDK module tag for the x-dashscope-sdk-client header
57+
sdk_module: str = "",
5658
# Additional parameters for API request data
5759
**kwargs,
5860
):
@@ -192,7 +194,7 @@ def _build_api_request( # pylint: disable=too-many-branches
192194
"websocket]",
193195
)
194196

195-
merged_headers = dict(get_sdk_headers())
197+
merged_headers = dict(get_sdk_headers(module=sdk_module))
196198
if headers is not None:
197199
merged_headers.update(headers)
198200
if merged_headers:

‎dashscope/api_entities/encryption.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
DEFAULT_REQUEST_TIMEOUT_SECONDS,
1919
)
2020
from dashscope.common.logging import logger
21+
from dashscope.common.utils import get_sdk_headers
2122

2223

2324
class Encryption:
@@ -90,6 +91,7 @@ def _get_public_keys():
9091
url = dashscope.base_http_api_url + "/public-keys/latest"
9192
headers = {
9293
"Authorization": f"Bearer {dashscope.api_key}",
94+
**get_sdk_headers(module="utils"),
9395
}
9496

9597
response = requests.get(

‎dashscope/app/application.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ def call( # type: ignore[override]
240240
workspace=workspace,
241241
api_key=api_key,
242242
is_service=False,
243+
sdk_module="app",
243244
**parameters,
244245
)
245246
# call request service.

‎dashscope/audio/http_tts/http_speech_synthesizer.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _http_call(
203203

204204
# Get base URL
205205
import dashscope
206-
from dashscope.common.utils import join_url
206+
from dashscope.common.utils import get_sdk_headers, join_url
207207

208208
if url:
209209
base_url = url
@@ -233,6 +233,9 @@ def _http_call(
233233
stream=stream,
234234
)
235235

236+
# Tag sdk module (custom headers below may still override)
237+
request.add_headers(get_sdk_headers(module="audio"))
238+
236239
# Add custom headers
237240
if headers:
238241
request.add_headers(headers)

‎dashscope/audio/qwen_omni/omni_realtime.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import dashscope
1515
from dashscope.common.error import ModelRequired
1616
from dashscope.common.logging import logger
17-
from dashscope.common.utils import get_user_agent
17+
from dashscope.common.utils import get_sdk_headers, get_user_agent
1818

1919

2020
class OmniRealtimeCallback:
@@ -259,6 +259,7 @@ def _get_websocket_header(self):
259259
headers = {
260260
"user-agent": ua,
261261
"Authorization": "Bearer " + self.apikey,
262+
**get_sdk_headers(module="audio"),
262263
}
263264
if self.user_headers:
264265
headers = {**self.user_headers, **headers}

‎dashscope/audio/qwen_tts_realtime/qwen_tts_realtime.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import dashscope
1313
from dashscope.common.error import ModelRequired
1414
from dashscope.common.logging import logger
15-
from dashscope.common.utils import get_user_agent
15+
from dashscope.common.utils import get_sdk_headers, get_user_agent
1616

1717

1818
class QwenTtsRealtimeCallback:
@@ -117,6 +117,7 @@ def _get_websocket_header(self):
117117
headers = {
118118
"user-agent": ua,
119119
"Authorization": "Bearer " + self.apikey,
120+
**get_sdk_headers(module="audio"),
120121
}
121122
if self.user_headers:
122123
headers = {**self.user_headers, **headers}

‎dashscope/audio/tts_v2/speech_synthesizer.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
RequestFailure,
2323
)
2424
from dashscope.common.logging import logger
25-
from dashscope.common.utils import get_user_agent
25+
from dashscope.common.utils import get_sdk_headers, get_user_agent
2626
from dashscope.protocol.websocket import (
2727
ACTION_KEY,
2828
ERROR_MESSAGE,
@@ -183,6 +183,7 @@ def get_websocket_headers(self, headers, workspace):
183183
self.headers = {
184184
"user-agent": ua,
185185
"Authorization": "Bearer " + self.apikey,
186+
**get_sdk_headers(module="audio"),
186187
}
187188
if headers:
188189
self.headers = {**self.headers, **headers}

‎dashscope/cli/__init__.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import dashscope # noqa: E402
2323
from dashscope.cli.common import err_console # noqa: E402
2424
from dashscope.common.error import AuthenticationError # noqa: E402
25+
from dashscope.common.utils import set_sdk_client # noqa: E402
2526
from dashscope.cli import ( # noqa: E402
2627
application,
2728
auth,
@@ -43,6 +44,8 @@
4344
video_synthesis,
4445
)
4546

47+
set_sdk_client("python-cli")
48+
4649

4750
# ---------------------------------------------------------------------------
4851
# Legacy command compatibility layer

0 commit comments

Comments
 (0)