Skip to content

Commit 4795ed4

Browse files
author
kevin
committed
refactor(api): remove redundant exception handling in request modules
1 parent 6536dff commit 4795ed4

2 files changed

Lines changed: 130 additions & 148 deletions

File tree

dashscope/api_entities/aiohttp_request.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -245,45 +245,38 @@ async def _handle_response( # pylint: disable=too-many-branches
245245
)
246246

247247
async def _handle_request(self):
248-
try:
249-
async with aiohttp.ClientSession(
250-
timeout=aiohttp.ClientTimeout(total=self.timeout),
251-
headers=self.headers,
252-
) as session:
253-
logger.debug("Starting request: %s", self.url)
254-
if self.method == HTTPMethod.POST:
255-
is_form, obj = self.data.get_aiohttp_payload()
256-
if is_form:
257-
headers = {**self.headers, **obj.headers}
258-
response = await session.post(
259-
url=self.url,
260-
data=obj,
261-
headers=headers,
262-
)
263-
else:
264-
response = await session.request(
265-
"POST",
266-
url=self.url,
267-
json=obj,
268-
headers=self.headers,
269-
)
270-
elif self.method == HTTPMethod.GET:
271-
response = await session.get(
248+
async with aiohttp.ClientSession(
249+
timeout=aiohttp.ClientTimeout(total=self.timeout),
250+
headers=self.headers,
251+
) as session:
252+
logger.debug("Starting request: %s", self.url)
253+
if self.method == HTTPMethod.POST:
254+
is_form, obj = self.data.get_aiohttp_payload()
255+
if is_form:
256+
headers = {**self.headers, **obj.headers}
257+
response = await session.post(
272258
url=self.url,
273-
params=self.data.parameters,
274-
headers=self.headers,
259+
data=obj,
260+
headers=headers,
275261
)
276262
else:
277-
raise UnsupportedHTTPMethod(
278-
f"Unsupported http method: {self.method}",
263+
response = await session.request(
264+
"POST",
265+
url=self.url,
266+
json=obj,
267+
headers=self.headers,
279268
)
280-
logger.debug("Response returned: %s", self.url)
281-
async with response:
282-
async for rsp in self._handle_response(response):
283-
yield rsp
284-
except aiohttp.ClientConnectorError as e:
285-
logger.error(e)
286-
raise e
287-
except Exception as e:
288-
logger.error(e)
289-
raise e
269+
elif self.method == HTTPMethod.GET:
270+
response = await session.get(
271+
url=self.url,
272+
params=self.data.parameters,
273+
headers=self.headers,
274+
)
275+
else:
276+
raise UnsupportedHTTPMethod(
277+
f"Unsupported http method: {self.method}",
278+
)
279+
logger.debug("Response returned: %s", self.url)
280+
async with response:
281+
async for rsp in self._handle_response(response):
282+
yield rsp

dashscope/api_entities/http_request.py

Lines changed: 99 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -160,75 +160,68 @@ async def aio_call(self):
160160
return result
161161

162162
async def _handle_aio_request(self): # pylint: disable=too-many-branches
163-
try:
164-
# Use external aio_session if provided,
165-
# otherwise create temporary session
166-
if self._external_aio_session is not None:
167-
session = self._external_aio_session
168-
should_close = False
169-
else:
170-
connector = aiohttp.TCPConnector(
171-
ssl=ssl.create_default_context(
172-
cafile=certifi.where(),
173-
),
174-
)
175-
session = aiohttp.ClientSession(
176-
connector=connector,
177-
timeout=aiohttp.ClientTimeout(total=self.timeout),
178-
headers=self.headers,
179-
)
180-
should_close = True
163+
# Use external aio_session if provided,
164+
# otherwise create temporary session
165+
if self._external_aio_session is not None:
166+
session = self._external_aio_session
167+
should_close = False
168+
else:
169+
connector = aiohttp.TCPConnector(
170+
ssl=ssl.create_default_context(
171+
cafile=certifi.where(),
172+
),
173+
)
174+
session = aiohttp.ClientSession(
175+
connector=connector,
176+
timeout=aiohttp.ClientTimeout(total=self.timeout),
177+
headers=self.headers,
178+
)
179+
should_close = True
181180

182-
try:
183-
logger.debug("Starting request: %s", self.url)
184-
if self.method == HTTPMethod.POST:
185-
is_form, obj = False, {}
186-
if hasattr(self, "data") and self.data is not None:
187-
is_form, obj = self.data.get_aiohttp_payload()
188-
if is_form:
189-
headers = {**self.headers, **obj.headers}
190-
response = await session.post(
191-
url=self.url,
192-
data=obj,
193-
headers=headers,
194-
)
195-
else:
196-
response = await session.request(
197-
"POST",
198-
url=self.url,
199-
json=obj,
200-
headers=self.headers,
201-
)
202-
elif self.method == HTTPMethod.GET:
203-
# 添加条件判断
204-
params = {}
205-
if hasattr(self, "data") and self.data is not None:
206-
params = getattr(self.data, "parameters", {})
207-
if params:
208-
params = self.__handle_parameters(params)
209-
response = await session.get(
181+
try:
182+
logger.debug("Starting request: %s", self.url)
183+
if self.method == HTTPMethod.POST:
184+
is_form, obj = False, {}
185+
if hasattr(self, "data") and self.data is not None:
186+
is_form, obj = self.data.get_aiohttp_payload()
187+
if is_form:
188+
headers = {**self.headers, **obj.headers}
189+
response = await session.post(
210190
url=self.url,
211-
params=params,
212-
headers=self.headers,
191+
data=obj,
192+
headers=headers,
213193
)
214194
else:
215-
raise UnsupportedHTTPMethod(
216-
f"Unsupported http method: {self.method}",
195+
response = await session.request(
196+
"POST",
197+
url=self.url,
198+
json=obj,
199+
headers=self.headers,
217200
)
218-
logger.debug("Response returned: %s", self.url)
219-
async with response:
220-
async for rsp in self._handle_aio_response(response):
221-
yield rsp
222-
finally:
223-
# Only close if we created the session
224-
if should_close:
225-
await session.close()
226-
except aiohttp.ClientConnectorError as e:
227-
logger.error(e)
228-
raise e
229-
except BaseException as e:
230-
logger.error(e)
231-
raise e
201+
elif self.method == HTTPMethod.GET:
202+
# 添加条件判断
203+
params = {}
204+
if hasattr(self, "data") and self.data is not None:
205+
params = getattr(self.data, "parameters", {})
206+
if params:
207+
params = self.__handle_parameters(params)
208+
response = await session.get(
209+
url=self.url,
210+
params=params,
211+
headers=self.headers,
212+
)
213+
else:
214+
raise UnsupportedHTTPMethod(
215+
f"Unsupported http method: {self.method}",
216+
)
217+
logger.debug("Response returned: %s", self.url)
218+
async with response:
219+
async for rsp in self._handle_aio_response(response):
220+
yield rsp
221+
finally:
222+
# Only close if we created the session
223+
if should_close:
224+
await session.close()
232225

233226
@staticmethod
234227
def __handle_parameters(params: dict) -> dict:
@@ -458,55 +451,51 @@ def _handle_response( # pylint: disable=too-many-branches
458451
yield _handle_http_failed_response(response)
459452

460453
def _handle_request(self):
461-
try:
462-
# Use external session if provided,
463-
# otherwise create temporary session
464-
if self._external_session is not None:
465-
session = self._external_session
466-
should_close = False
467-
else:
468-
session = requests.Session()
469-
should_close = True
454+
# Use external session if provided,
455+
# otherwise create temporary session
456+
if self._external_session is not None:
457+
session = self._external_session
458+
should_close = False
459+
else:
460+
session = requests.Session()
461+
should_close = True
470462

471-
try:
472-
if self.method == HTTPMethod.POST:
473-
is_form, form, obj = self.data.get_http_payload()
474-
if is_form:
475-
headers = {**self.headers}
476-
headers.pop("Content-Type")
477-
response = session.post(
478-
url=self.url,
479-
data=obj,
480-
files=form,
481-
headers=headers,
482-
timeout=self.timeout,
483-
)
484-
else:
485-
logger.debug("Request body: %s", obj)
486-
response = session.post(
487-
url=self.url,
488-
stream=self.stream,
489-
json=obj,
490-
headers={**self.headers},
491-
timeout=self.timeout,
492-
)
493-
elif self.method == HTTPMethod.GET:
494-
response = session.get(
463+
try:
464+
if self.method == HTTPMethod.POST:
465+
is_form, form, obj = self.data.get_http_payload()
466+
if is_form:
467+
headers = {**self.headers}
468+
headers.pop("Content-Type")
469+
response = session.post(
495470
url=self.url,
496-
params=self.data.parameters,
497-
headers=self.headers,
471+
data=obj,
472+
files=form,
473+
headers=headers,
498474
timeout=self.timeout,
499475
)
500476
else:
501-
raise UnsupportedHTTPMethod(
502-
f"Unsupported http method: {self.method}",
477+
logger.debug("Request body: %s", obj)
478+
response = session.post(
479+
url=self.url,
480+
stream=self.stream,
481+
json=obj,
482+
headers={**self.headers},
483+
timeout=self.timeout,
503484
)
504-
for rsp in self._handle_response(response):
505-
yield rsp
506-
finally:
507-
# Only close if we created the session
508-
if should_close:
509-
session.close()
510-
except BaseException as e:
511-
logger.error(e)
512-
raise e
485+
elif self.method == HTTPMethod.GET:
486+
response = session.get(
487+
url=self.url,
488+
params=self.data.parameters,
489+
headers=self.headers,
490+
timeout=self.timeout,
491+
)
492+
else:
493+
raise UnsupportedHTTPMethod(
494+
f"Unsupported http method: {self.method}",
495+
)
496+
for rsp in self._handle_response(response):
497+
yield rsp
498+
finally:
499+
# Only close if we created the session
500+
if should_close:
501+
session.close()

0 commit comments

Comments
 (0)