Skip to content

Commit

Permalink
Merge pull request #206 from sberdevices/feature/DPNLPF-1536
Browse files Browse the repository at this point in the history
DPNLPF-1536 added additional params for logging and improve headers checks
  • Loading branch information
dangerink authored Mar 1, 2022
2 parents 5c80567 + fe55539 commit 7dd57ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
31 changes: 21 additions & 10 deletions smart_kit/action/base_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ def __init__(self, items, id=None):
self.error = None

@staticmethod
def _check_headers_validity(headers: Dict[str, Any]) -> Dict[str, str]:
for header_name, header_value in headers.items():
def _check_headers_validity(headers: Dict[str, Any], user) -> Dict[str, str]:
for header_name, header_value in list(headers.items()):
if not isinstance(header_value, (str, bytes)):
headers[header_name] = str(header_value)
if isinstance(header_value, (int, float, bool)):
headers[header_name] = str(header_value)
else:
log(f"{__class__.__name__}._check_headers_validity remove header {header_name} because "
f"({type(header_value)}) is not in [int, float, bool, str, bytes]", user=user, params={
log_const.KEY_NAME: "sent_http_remove_header",
})
del headers[header_name]
return headers

def _make_response(self, request_parameters, user):
Expand All @@ -64,8 +71,8 @@ def _make_response(self, request_parameters, user):
except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError):
self.error = self.CONNECTION

def _get_requst_params(self, user: BaseUser, text_preprocessing_result: BaseTextPreprocessingResult,
params: Optional[Dict[str, Union[str, float, int]]] = None):
def _get_request_params(self, user: BaseUser, text_preprocessing_result: BaseTextPreprocessingResult,
params: Optional[Dict[str, Union[str, float, int]]] = None):
collected = user.parametrizer.collect(text_preprocessing_result)
params.update(collected)

Expand All @@ -75,28 +82,32 @@ def _get_requst_params(self, user: BaseUser, text_preprocessing_result: BaseText
if req_headers:
# Заголовки в запросах должны иметь тип str или bytes. Поэтому добавлена проверка и приведение к типу str,
# на тот случай если в сценарии заголовок указали как int, float и тд
request_parameters["headers"] = self._check_headers_validity(req_headers)
request_parameters["headers"] = self._check_headers_validity(req_headers, user)
return request_parameters

def _log_request(self, user, request_parameters):
def _log_request(self, user, request_parameters, additional_params=None):
additional_params = additional_params or {}
log(f"{self.__class__.__name__}.run sent https request ", user=user, params={
**request_parameters,
log_const.KEY_NAME: "sent_http_request"
log_const.KEY_NAME: "sent_http_request",
**additional_params,
})

def _log_response(self, user, response, data):
def _log_response(self, user, response, data, additional_params=None):
additional_params = additional_params or {}
log(f"{self.__class__.__name__}.run get https response ", user=user, params={
'headers': dict(response.headers),
'time': response.elapsed.microseconds,
'cookie': {i.name: i.value for i in response.cookies},
'status': response.status_code,
'data': data,
log_const.KEY_NAME: "got_http_response",
**additional_params,
})

def run(self, user: BaseUser, text_preprocessing_result: BaseTextPreprocessingResult,
params: Optional[Dict[str, Union[str, float, int]]] = None) -> Optional[List[Command]]:
params = params or {}
request_parameters = self._get_requst_params(user, text_preprocessing_result, params)
request_parameters = self._get_request_params(user, text_preprocessing_result, params)
self._log_request(user, request_parameters)
return self._make_response(request_parameters, user)
4 changes: 3 additions & 1 deletion tests/smart_kit_tests/action/test_base_http_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def test_headers_fix(self, request_mock):
"headers": {
"header_1": 32,
"header_2": 32.03,
"header_3": b"d32"
"header_3": b"d32",
"header_4": None,
"header_5": {"data": "value"},
},
}
result = BaseHttpRequestAction(items).run(self.user, None, {})
Expand Down

0 comments on commit 7dd57ee

Please sign in to comment.