Skip to content

Commit e8bafc1

Browse files
authored
Merge pull request #5 from chilispa/log-refactor
Log refactor
2 parents 111159f + ae1cf63 commit e8bafc1

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

src/RequestsLibrary/RequestsKeywords.py

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def create_custom_session(
231231
232232
``cookies`` Dictionary of cookies
233233
234-
``auth`` A Custom Authentication object to be passed on to the reqests library.
234+
``auth`` A Custom Authentication object to be passed on to the requests library.
235235
http://docs.python-requests.org/en/master/user/advanced/#custom-authentication
236236
237237
``timeout`` Connection timeout
@@ -504,6 +504,7 @@ def get_request(
504504
alias,
505505
uri,
506506
headers=None,
507+
data=None,
507508
json=None,
508509
params=None,
509510
allow_redirects=None,
@@ -519,7 +520,12 @@ def get_request(
519520
520521
``headers`` a dictionary of headers to use with the request
521522
522-
``json`` json data to send in the body of the :class:`Request`.
523+
``data`` a dictionary of key-value pairs that will be urlencoded
524+
and sent as GET data
525+
or binary data that is sent as the raw body content
526+
527+
``json`` a value that will be json encoded
528+
and sent as GET data if data is not specified
523529
524530
``allow_redirects`` Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.
525531
@@ -533,14 +539,10 @@ def get_request(
533539
uri,
534540
params=params,
535541
headers=headers,
542+
data=data,
536543
json=json,
537544
allow_redirects=redir,
538545
timeout=timeout)
539-
540-
logger.info(
541-
'Get Request using : alias=%s, uri=%s, headers=%s json=%s' %
542-
(alias, uri, headers, json))
543-
544546
return response
545547

546548
def post_request(
@@ -596,10 +598,6 @@ def post_request(
596598
headers=headers,
597599
allow_redirects=redir,
598600
timeout=timeout)
599-
data_str = self._format_data_to_log_string_according_to_header(session, data, headers)
600-
logger.info('Post Request using : alias=%s, uri=%s, data=%s, headers=%s, files=%s, allow_redirects=%s '
601-
% (alias, uri, data_str, headers, files, redir))
602-
603601
return response
604602

605603
def patch_request(
@@ -653,12 +651,6 @@ def patch_request(
653651
allow_redirects=redir,
654652
timeout=timeout)
655653

656-
if isinstance(data, bytes):
657-
data = data.decode('utf-8')
658-
logger.info('Patch Request using : alias=%s, uri=%s, data=%s, \
659-
headers=%s, files=%s, allow_redirects=%s '
660-
% (alias, uri, data, headers, files, redir))
661-
662654
return response
663655

664656
def put_request(
@@ -710,11 +702,6 @@ def put_request(
710702
allow_redirects=redir,
711703
timeout=timeout)
712704

713-
if isinstance(data, bytes):
714-
data = data.decode('utf-8')
715-
logger.info('Put Request using : alias=%s, uri=%s, data=%s, \
716-
headers=%s, allow_redirects=%s ' % (alias, uri, data, headers, redir))
717-
718705
return response
719706

720707
def delete_request(
@@ -758,11 +745,6 @@ def delete_request(
758745
allow_redirects=redir,
759746
timeout=timeout)
760747

761-
if isinstance(data, bytes):
762-
data = data.decode('utf-8')
763-
logger.info('Delete Request using : alias=%s, uri=%s, data=%s, \
764-
headers=%s, allow_redirects=%s ' % (alias, uri, data, headers, redir))
765-
766748
return response
767749

768750
def head_request(
@@ -792,8 +774,6 @@ def head_request(
792774
headers=headers,
793775
allow_redirects=redir,
794776
timeout=timeout)
795-
logger.info('Head Request using : alias=%s, uri=%s, headers=%s, \
796-
allow_redirects=%s ' % (alias, uri, headers, redir))
797777

798778
return response
799779

@@ -824,9 +804,6 @@ def options_request(
824804
headers=headers,
825805
allow_redirects=redir,
826806
timeout=timeout)
827-
logger.info(
828-
'Options Request using : alias=%s, uri=%s, headers=%s, allow_redirects=%s ' %
829-
(alias, uri, headers, redir))
830807

831808
return response
832809

@@ -837,22 +814,21 @@ def _common_request(
837814
uri,
838815
**kwargs):
839816

840-
self._capture_output()
841-
817+
self._log_request(method, session, uri, **kwargs)
842818
method_function = getattr(session, method)
819+
820+
self._capture_output()
843821
resp = method_function(
844822
self._get_url(session, uri),
845823
params=self._utf8_urlencode(kwargs.pop('params', None)),
846824
timeout=self._get_timeout(kwargs.pop('timeout', None)),
847825
cookies=self.cookies,
848826
verify=self.verify,
849827
**kwargs)
850-
851828
self._print_debug()
852-
session.last_resp = resp
853829

854-
# TODO centralize also requests log
855-
logger.debug('%s response: %s' % (method, resp.text))
830+
session.last_resp = resp
831+
self._log_response(method, resp)
856832

857833
return resp
858834

@@ -941,8 +917,8 @@ def _format_data_according_to_header(self, session, data, headers):
941917

942918
return data
943919

944-
def _format_data_to_log_string_according_to_header(self, session, data, headers):
945-
data_str = "<empty>"
920+
def _format_data_to_log_string_according_to_headers(self, session, data, headers):
921+
data_str = None
946922
# Merged headers are already case insensitive
947923
headers = self._merge_headers(session, headers)
948924

@@ -958,6 +934,44 @@ def _format_data_to_log_string_according_to_header(self, session, data, headers)
958934

959935
return data_str
960936

937+
def _log_request(
938+
self,
939+
method,
940+
session,
941+
uri,
942+
**kwargs):
943+
944+
# TODO would be nice to add also the alias
945+
# TODO would be nice to pretty format the headers / json / data
946+
# TODO move in common the data formatting to have this as @staticmethod
947+
948+
# kwargs might include: method, session, uri, params, files, headers,
949+
# data, json, allow_redirects, timeout
950+
args = kwargs.copy()
951+
args.pop('session', None)
952+
# This will log specific headers merged with session defined headers
953+
merged_headers = self._merge_headers(session, args.pop('headers', None))
954+
formatted_data = self._format_data_to_log_string_according_to_headers(session,
955+
args.pop('data', None),
956+
merged_headers)
957+
formatted_json = args.pop('json', None)
958+
method_log = '%s Request using : ' % method.upper()
959+
uri_log = 'uri=%s' % uri
960+
composed_log = method_log + uri_log
961+
for arg in args:
962+
composed_log += ', %s=%s' % (arg, kwargs.get(arg, None))
963+
logger.info(composed_log + '\n' +
964+
'headers=%s \n' % merged_headers +
965+
'data=%s \n' % formatted_data +
966+
'json=%s' % formatted_json)
967+
968+
@staticmethod
969+
def _log_response(method, response):
970+
logger.debug('%s Response : status=%s, reason=%s\n' % (method.upper(),
971+
response.status_code,
972+
response.reason) +
973+
response.text)
974+
961975
@staticmethod
962976
def _merge_headers(session, headers):
963977
if headers is None:

src/RequestsLibrary/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = '0.6.0'
1+
VERSION = '0.6.1'

0 commit comments

Comments
 (0)