Skip to content

Commit

Permalink
Merge 98ce3f8 into 121d885
Browse files Browse the repository at this point in the history
  • Loading branch information
mokaddem committed May 22, 2019
2 parents 121d885 + 98ce3f8 commit 274a8ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 6 additions & 3 deletions pymisp/api.py
Expand Up @@ -236,15 +236,18 @@ def flatten_error_messages(self, response):

return messages

def _check_response(self, response):
def _check_response(self, response, lenient_response_type=False):
"""Check if the response from the server is not an unexpected error"""
try:
json_response = response.json()
except ValueError:
# If the server didn't return a JSON blob, we've a problem.
if not len(response.text):
raise PyMISPEmptyResponse('The server returned an empty response. \n{}\n{}\n'.format(response.request.headers, response.request.body))
raise PyMISPError(everything_broken.format(response.request.headers, response.request.body, response.text))
if lenient_response_type and not response.headers.get('content-type').startswith('application/json;'):
return response.text
else:
raise PyMISPError(everything_broken.format(response.request.headers, response.request.body, response.text))

errors = []

Expand Down Expand Up @@ -445,7 +448,7 @@ def direct_call(self, url, data=None):
if isinstance(data, dict):
data = json.dumps(data)
response = self._prepare_request('POST', url, data)
return self._check_response(response)
return self._check_response(response, lenient_response_type=True)

# ##############################################
# ############### Event handling ###############
Expand Down
4 changes: 3 additions & 1 deletion pymisp/aping.py
Expand Up @@ -62,7 +62,7 @@ def make_timestamp(self, value: DateTypes):
else:
return value

def _check_response(self, response):
def _check_response(self, response, lenient_response_type=False):
"""Check if the response from the server is not an unexpected error"""
if response.status_code >= 500:
logger.critical(everything_broken.format(response.request.headers, response.request.body, response.text))
Expand All @@ -84,6 +84,8 @@ def _check_response(self, response):
response = response['response']
return response
except Exception:
if lenient_response_type and not response.headers.get('content-type').startswith('application/json;'):
return response.text
if logger.isEnabledFor(logging.DEBUG):
logger.debug(response.text)
if not len(response.content):
Expand Down

0 comments on commit 274a8ca

Please sign in to comment.