Skip to content

Commit 664a99a

Browse files
author
mose-x.zm
committed
Add headers to the response.
1 parent 40fd113 commit 664a99a

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

dashscope/api_entities/dashscope_response.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class DashScopeAPIResponse(DictMixin):
9191
message: str
9292
output: Any
9393
usage: Any
94+
headers: dict
9495

9596
def __init__(
9697
self,
@@ -100,6 +101,7 @@ def __init__(
100101
message: str = "",
101102
output: Any = None,
102103
usage: Any = None,
104+
headers: dict = None,
103105
**kwargs,
104106
):
105107
super().__init__(
@@ -109,9 +111,32 @@ def __init__(
109111
message=message,
110112
output=output,
111113
usage=usage,
114+
headers=headers,
112115
**kwargs,
113116
)
114117

118+
def __repr__(self):
119+
data = {
120+
"status_code": self.status_code,
121+
"request_id": self.request_id,
122+
"code": self.code,
123+
"message": self.message,
124+
"output": self.output,
125+
"usage": self.usage,
126+
}
127+
return f"{type(self).__name__}({data})"
128+
129+
def __str__(self):
130+
data = {
131+
"status_code": self.status_code,
132+
"request_id": self.request_id,
133+
"code": self.code,
134+
"message": self.message,
135+
"output": self.output,
136+
"usage": self.usage,
137+
}
138+
return json.dumps(data, ensure_ascii=False)
139+
115140

116141
class Role:
117142
USER = "user"
@@ -235,6 +260,7 @@ def __init__(
235260
class GenerationResponse(DashScopeAPIResponse):
236261
output: GenerationOutput
237262
usage: GenerationUsage
263+
headers: dict
238264

239265
@staticmethod
240266
def from_api_response(api_response: DashScopeAPIResponse):
@@ -250,13 +276,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
250276
message=api_response.message,
251277
output=GenerationOutput(**api_response.output),
252278
usage=GenerationUsage(**usage),
279+
headers=api_response.headers,
253280
)
254281
else:
255282
return GenerationResponse(
256283
status_code=api_response.status_code,
257284
request_id=api_response.request_id,
258285
code=api_response.code,
259286
message=api_response.message,
287+
headers=api_response.headers,
260288
)
261289

262290

@@ -316,6 +344,7 @@ def __init__(
316344
class MultiModalConversationResponse(DashScopeAPIResponse):
317345
output: MultiModalConversationOutput
318346
usage: MultiModalConversationUsage
347+
headers: dict
319348

320349
@staticmethod
321350
def from_api_response(api_response: DashScopeAPIResponse):
@@ -331,13 +360,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
331360
message=api_response.message,
332361
output=MultiModalConversationOutput(**api_response.output),
333362
usage=MultiModalConversationUsage(**usage),
363+
headers=api_response.headers,
334364
)
335365
else:
336366
return MultiModalConversationResponse(
337367
status_code=api_response.status_code,
338368
request_id=api_response.request_id,
339369
code=api_response.code,
340370
message=api_response.message,
371+
headers=api_response.headers,
341372
)
342373

343374

@@ -365,6 +396,7 @@ def __init__(self, **kwargs):
365396
class TranscriptionResponse(DashScopeAPIResponse):
366397
output: TranscriptionOutput
367398
usage: TranscriptionUsage
399+
headers: dict
368400

369401
@staticmethod
370402
def from_api_response(api_response: DashScopeAPIResponse):
@@ -383,6 +415,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
383415
message=api_response.message,
384416
output=output,
385417
usage=usage,
418+
headers=api_response.headers,
386419
)
387420

388421
else:
@@ -391,6 +424,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
391424
request_id=api_response.request_id,
392425
code=api_response.code,
393426
message=api_response.message,
427+
headers=api_response.headers,
394428
)
395429

396430

@@ -414,6 +448,7 @@ def __init__(self, duration: int = 0, **kwargs):
414448
class RecognitionResponse(DashScopeAPIResponse):
415449
output: RecognitionOutput
416450
usage: RecognitionUsage
451+
headers: dict
417452

418453
@staticmethod
419454
def from_api_response(api_response: DashScopeAPIResponse):
@@ -433,6 +468,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
433468
message=api_response.message,
434469
output=output,
435470
usage=usage,
471+
headers=api_response.headers,
436472
)
437473

438474
else:
@@ -441,6 +477,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
441477
request_id=api_response.request_id,
442478
code=api_response.code,
443479
message=api_response.message,
480+
headers=api_response.headers,
444481
)
445482

446483
@staticmethod
@@ -478,6 +515,7 @@ def __init__(self, characters: int = 0, **kwargs):
478515
class SpeechSynthesisResponse(DashScopeAPIResponse):
479516
output: SpeechSynthesisOutput
480517
usage: SpeechSynthesisUsage
518+
headers: dict
481519

482520
@staticmethod
483521
def from_api_response(api_response: DashScopeAPIResponse):
@@ -496,6 +534,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
496534
message=api_response.message,
497535
output=output,
498536
usage=usage,
537+
headers=api_response.headers,
499538
)
500539

501540
else:
@@ -504,6 +543,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
504543
request_id=api_response.request_id,
505544
code=api_response.code,
506545
message=api_response.message,
546+
headers=api_response.headers,
507547
)
508548

509549

@@ -597,6 +637,7 @@ def __init__(
597637
class ImageSynthesisResponse(DashScopeAPIResponse):
598638
output: ImageSynthesisOutput
599639
usage: ImageSynthesisUsage
640+
headers: dict
600641

601642
@staticmethod
602643
def from_api_response(api_response: DashScopeAPIResponse):
@@ -615,6 +656,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
615656
message=api_response.message,
616657
output=output,
617658
usage=usage,
659+
headers=api_response.headers,
618660
)
619661

620662
else:
@@ -623,13 +665,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
623665
request_id=api_response.request_id,
624666
code=api_response.code,
625667
message=api_response.message,
668+
headers=api_response.headers,
626669
)
627670

628671

629672
@dataclass(init=False)
630673
class VideoSynthesisResponse(DashScopeAPIResponse):
631674
output: VideoSynthesisOutput
632675
usage: VideoSynthesisUsage
676+
headers: dict
633677

634678
@staticmethod
635679
def from_api_response(api_response: DashScopeAPIResponse):
@@ -648,6 +692,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
648692
message=api_response.message,
649693
output=output,
650694
usage=usage,
695+
headers=api_response.headers,
651696
)
652697

653698
else:
@@ -656,6 +701,7 @@ def from_api_response(api_response: DashScopeAPIResponse):
656701
request_id=api_response.request_id,
657702
code=api_response.code,
658703
message=api_response.message,
704+
headers=api_response.headers,
659705
)
660706

661707

@@ -705,6 +751,7 @@ def __init__(self, total_tokens=None, **kwargs):
705751
class ReRankResponse(DashScopeAPIResponse):
706752
output: ReRankOutput
707753
usage: GenerationUsage
754+
headers: dict
708755

709756
@staticmethod
710757
def from_api_response(api_response: DashScopeAPIResponse):
@@ -720,13 +767,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
720767
message=api_response.message,
721768
output=ReRankOutput(**api_response.output),
722769
usage=ReRankUsage(**usage),
770+
headers=api_response.headers,
723771
)
724772
else:
725773
return ReRankResponse(
726774
status_code=api_response.status_code,
727775
request_id=api_response.request_id,
728776
code=api_response.code,
729777
message=api_response.message,
778+
headers=api_response.headers,
730779
)
731780

732781

@@ -777,6 +826,7 @@ def __init__(
777826
class TextToSpeechResponse(DashScopeAPIResponse):
778827
output: TextToSpeechOutput
779828
usage: MultiModalConversationUsage
829+
headers: dict
780830

781831
@staticmethod
782832
def from_api_response(api_response: DashScopeAPIResponse):
@@ -792,13 +842,15 @@ def from_api_response(api_response: DashScopeAPIResponse):
792842
message=api_response.message,
793843
output=TextToSpeechOutput(**api_response.output),
794844
usage=MultiModalConversationUsage(**usage),
845+
headers=api_response.headers,
795846
)
796847
else:
797848
return TextToSpeechResponse(
798849
status_code=api_response.status_code,
799850
request_id=api_response.request_id,
800851
code=api_response.code,
801852
message=api_response.message,
853+
headers=api_response.headers,
802854
)
803855

804856

@@ -858,6 +910,7 @@ def __init__(
858910
class ImageGenerationResponse(DashScopeAPIResponse):
859911
output: ImageGenerationOutput
860912
usage: ImageGenerationUsage
913+
headers: dict
861914

862915
@staticmethod
863916
def from_api_response(api_response: DashScopeAPIResponse):
@@ -873,11 +926,13 @@ def from_api_response(api_response: DashScopeAPIResponse):
873926
message=api_response.message,
874927
output=ImageGenerationOutput(**api_response.output),
875928
usage=ImageGenerationUsage(**usage),
929+
headers=api_response.headers,
876930
)
877931
else:
878932
return ImageGenerationResponse(
879933
status_code=api_response.status_code,
880934
request_id=api_response.request_id,
881935
code=api_response.code,
882936
message=api_response.message,
937+
headers=api_response.headers,
883938
)

dashscope/api_entities/http_request.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
266266
response: aiohttp.ClientResponse,
267267
):
268268
request_id = ""
269+
headers = dict(response.headers)
269270
if (
270271
response.status == HTTPStatus.OK
271272
and self.stream
@@ -291,6 +292,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
291292
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
292293
code="Unknown",
293294
message=data,
295+
headers=headers,
294296
)
295297
continue
296298
if is_error:
@@ -299,6 +301,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
299301
status_code=status_code,
300302
code=msg["code"],
301303
message=msg["message"],
304+
headers=headers,
302305
)
303306
else:
304307
if self.encryption and self.encryption.is_valid():
@@ -308,6 +311,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
308311
status_code=HTTPStatus.OK,
309312
output=output,
310313
usage=usage,
314+
headers=headers,
311315
)
312316
elif (
313317
response.status == HTTPStatus.OK
@@ -329,6 +333,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
329333
request_id=request_id,
330334
status_code=HTTPStatus.OK,
331335
output=output,
336+
headers=headers,
332337
)
333338
elif response.status == HTTPStatus.OK:
334339
json_content = await response.json()
@@ -356,6 +361,7 @@ async def _handle_aio_response( # pylint: disable=too-many-branches, too-many-s
356361
status_code=HTTPStatus.OK,
357362
output=output,
358363
usage=usage,
364+
headers=headers,
359365
)
360366
else:
361367
yield await _handle_aiohttp_failed_response(response)
@@ -365,6 +371,7 @@ def _handle_response( # pylint: disable=too-many-branches
365371
response: requests.Response,
366372
):
367373
request_id = ""
374+
headers = dict(response.headers)
368375
if (
369376
response.status_code == HTTPStatus.OK
370377
and self.stream
@@ -395,6 +402,7 @@ def _handle_response( # pylint: disable=too-many-branches
395402
output=None,
396403
code="Unknown",
397404
message=data,
405+
headers=headers,
398406
)
399407
continue
400408
if is_error:
@@ -406,6 +414,7 @@ def _handle_response( # pylint: disable=too-many-branches
406414
if "code" in msg
407415
else None, # noqa E501
408416
message=msg["message"] if "message" in msg else None,
417+
headers=headers,
409418
) # noqa E501
410419
else:
411420
if self.flattened_output:
@@ -418,6 +427,7 @@ def _handle_response( # pylint: disable=too-many-branches
418427
status_code=HTTPStatus.OK,
419428
output=output,
420429
usage=usage,
430+
headers=headers,
421431
)
422432
elif response.status_code == HTTPStatus.OK:
423433
json_content = response.json()
@@ -442,6 +452,7 @@ def _handle_response( # pylint: disable=too-many-branches
442452
status_code=HTTPStatus.OK,
443453
output=output,
444454
usage=usage,
455+
headers=headers,
445456
)
446457
else:
447458
yield _handle_http_failed_response(response)

0 commit comments

Comments
 (0)