Skip to content

Commit

Permalink
Merge pull request #168 from salute-developers/feature/DPNLPF-2184_fi…
Browse files Browse the repository at this point in the history
…x_http_request

issue#50: DPNLPF-2184: fix http_request
  • Loading branch information
SyrexMinus authored Nov 16, 2023
2 parents 5da1227 + 302a5f9 commit d8812c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
16 changes: 8 additions & 8 deletions smart_kit/action/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ async def _make_response(self, request_parameters: dict, user: BaseUser):
async with aiohttp.request(**request_parameters) as response:
response.raise_for_status()
self._log_response(user, response)
return response
try:
return await response.json()
except aiohttp.client_exceptions.ContentTypeError:
return await response.content.read()
return None
except (aiohttp.ServerTimeoutError, asyncio.TimeoutError):
self.error = self.TIMEOUT
except aiohttp.ClientError:
Expand Down Expand Up @@ -118,11 +122,7 @@ async def process_result(self, response, user, text_preprocessing_result, params
behavior_description = user.descriptions["behaviors"][self.behavior] if self.behavior else None
action = None
if self.error is None:
try:
data = await response.json()
except aiohttp.client_exceptions.ContentTypeError:
data = None
user.variables.set(self.store, data)
user.variables.set(self.store, response)
action = behavior_description.success_action if behavior_description else None
elif behavior_description is not None:
if self.error == self.TIMEOUT:
Expand All @@ -139,6 +139,6 @@ async def run(self, user: BaseUser, text_preprocessing_result: BaseTextPreproces
request_parameters = self._get_request_params(user, text_preprocessing_result, params)
self._log_request(user, request_parameters)
response = await self._make_response(request_parameters, user)
if response:
log("response data: %(body)s", params={"body": response.json()}, level="INFO")
if response is not None:
log("response data: %(body)s", params={"body": response}, level="INFO")
return await self.process_result(response, user, text_preprocessing_result, params)
9 changes: 9 additions & 0 deletions smart_kit/template/static/references/actions/actions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"exception_action": {
"type": "string",
"command": "ANSWER_TO_USER",
"nodes": {
"answer": [
Expand All @@ -21,5 +22,13 @@
"type": "template",
"template": "{{ payload.get('new_session', False) and settings['template_settings'].get('reset_context_on_new_session', False)}}"
}
},
"exception_reset_scenario": {
"type": "external",
"action": "exception_action"
},
"timeout_reset_scenario": {
"type": "external",
"action": "exception_action"
}
}

0 comments on commit d8812c8

Please sign in to comment.