Skip to content

Commit 87bf172

Browse files
mose-zmmose-x.zm
andauthored
fix aio params with bool value (#48)
fix aio params with bool value --------- Co-authored-by: mose-x.zm <[email protected]>
1 parent c8d964a commit 87bf172

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

dashscope/api_entities/http_request.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (c) Alibaba, Inc. and its affiliates.
2-
2+
import datetime
33
import json
44
import ssl
55
from http import HTTPStatus
@@ -148,6 +148,8 @@ async def _handle_aio_request(self):
148148
params = {}
149149
if hasattr(self, 'data') and self.data is not None:
150150
params = getattr(self.data, 'parameters', {})
151+
if params:
152+
params = self.__handle_parameters(params)
151153
response = await session.get(url=self.url,
152154
params=params,
153155
headers=self.headers)
@@ -165,6 +167,32 @@ async def _handle_aio_request(self):
165167
logger.error(e)
166168
raise e
167169

170+
@staticmethod
171+
def __handle_parameters(params: dict) -> dict:
172+
def __format(value):
173+
if isinstance(value, bool):
174+
return str(value).lower()
175+
elif isinstance(value, (str, int, float)):
176+
return value
177+
elif value is None:
178+
return ''
179+
elif isinstance(value, (datetime.datetime, datetime.date)):
180+
return value.isoformat()
181+
elif isinstance(value, (list, tuple)):
182+
return ','.join(str(__format(x)) for x in value)
183+
elif isinstance(value, dict):
184+
return json.dumps(value)
185+
else:
186+
try:
187+
return str(value)
188+
except Exception as e:
189+
raise ValueError(f"Unsupported type {type(value)} for param formatting: {e}")
190+
191+
formatted = {}
192+
for k, v in params.items():
193+
formatted[k] = __format(v)
194+
return formatted
195+
168196
async def _handle_aio_response(self, response: aiohttp.ClientResponse):
169197
request_id = ''
170198
if (response.status == HTTPStatus.OK and self.stream

0 commit comments

Comments
 (0)