def request_wrapper(self, *args, **kwargs): log_level = logging.INFO request_error = '' response_status = None response_body = '' try: response = func(self, *args, **kwargs) response_status = response.status_code response_body = response.content except Exception as err: log_level = logging.ERROR request_error = str(err) if getattr(err, 'response', None) is not None: response_status = err.response.status_code response_body = pformat_json(err.response.text) raise else: return response finally: # args will be Requests method, url, and optionally params, data or json. # kwargs may include Requests method kwargs and raise_for_status. kwargs.pop('raise_for_status', None) RequestLog.log(log_level, self.domain_name, self.payload_id, request_error, response_status, response_body, *args, **kwargs)
def request_wrapper(self, *args, **kwargs): log_level = logging.INFO request_error = '' response_status = None response_body = '' try: response = func(self, *args, **kwargs) response_status = response.status_code response_body = response.content except Exception as err: log_level = logging.ERROR request_error = str(err) if getattr(err, 'response', None) is not None: response_status = err.response.status_code response_body = pformat_json(err.response.content) raise else: return response finally: # args will be Requests method, url, and optionally params, data or json. # kwargs may include Requests method kwargs and raise_for_status. kwargs.pop('raise_for_status', None) RequestLog.log(log_level, self.domain_name, request_error, response_status, response_body, *args, **kwargs)
def log_request(self, log_level, log_entry): log = RequestLog.log(log_level, log_entry) self.status = IncrementalExportStatus.from_log_entry(log_entry) self.request_log = log self.save()
def test_request_body_none(self): entry = self.get_entry(request_body=None) request_log = RequestLog.log(level=logging.DEBUG, log_entry=entry) template_value = pp_json(request_log.request_body) self.assertEqual(template_value, '')
def test_request_body_unserializable(self): entry = self.get_entry(request_body={'hello': Decimal('1.0')}) request_log = RequestLog.log(level=logging.DEBUG, log_entry=entry) template_value = pp_json(request_log.request_body) self.assertEqual(template_value, "{'hello': Decimal('1.0')}")
def test_request_body_str(self): entry = self.get_entry(request_body='hello, world!') request_log = RequestLog.log(level=logging.DEBUG, log_entry=entry) template_value = pp_json(request_log.request_body) self.assertEqual(template_value, 'hello, world!')
def test_request_body_dict(self): entry = self.get_entry(request_body={'hello': 'world!'}) request_log = RequestLog.log(level=logging.DEBUG, log_entry=entry) template_value = pp_json(request_log.request_body) self.assertEqual(template_value, '{\n "hello": "world!"\n}')