From 399fac9f25827d8cdd3fd7062d76dfdd15377f0d Mon Sep 17 00:00:00 2001 From: JBarti Date: Mon, 24 Jun 2024 16:09:44 +0200 Subject: [PATCH] fix: check if response is valid json Check if response code is valid JSON before labeling the response as successful to prevent false positives. --- facebook_business/api.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/facebook_business/api.py b/facebook_business/api.py index 095a9a3e..14350671 100644 --- a/facebook_business/api.py +++ b/facebook_business/api.py @@ -61,13 +61,6 @@ def body(self): """Returns the response body.""" return self._body - def json(self): - """Returns the response body -- in json if possible.""" - try: - return json.loads(self._body) - except (TypeError, ValueError): - return self._body - def headers(self): """Return the response headers.""" return self._headers @@ -83,9 +76,12 @@ def status(self): def is_success(self): """Returns boolean indicating if the call was successful.""" - json_body = self.json() + try: + json_body = json.loads(self._body) + except (TypeError, ValueError): + return False - if isinstance(json_body, collections_abc.Mapping) and 'error' in json_body: + elif isinstance(json_body, collections_abc.Mapping) and 'error' in json_body: # Is a dictionary, has error in it return False elif bool(json_body):