From ab18a18a0d445a178e402a6fea9146d0f4c5d1f2 Mon Sep 17 00:00:00 2001 From: Gal Birka Date: Fri, 17 May 2024 09:07:39 +0300 Subject: [PATCH 1/4] optional status_code in APIError --- shodan/exception.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shodan/exception.py b/shodan/exception.py index 75b158e..0121da8 100644 --- a/shodan/exception.py +++ b/shodan/exception.py @@ -1,7 +1,8 @@ class APIError(Exception): """This exception gets raised whenever a non-200 status code was returned by the Shodan API.""" - def __init__(self, value): + def __init__(self, value, status_code=None): self.value = value + self.status_code = status_code def __str__(self): return self.value From 4cc72e7f4814ca72ddb470bbedb70f89464f10ab Mon Sep 17 00:00:00 2001 From: Gal Birka Date: Fri, 17 May 2024 09:07:58 +0300 Subject: [PATCH 2/4] add status_code to APIError when appropiate --- shodan/client.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/shodan/client.py b/shodan/client.py index 21c70af..e7bac69 100644 --- a/shodan/client.py +++ b/shodan/client.py @@ -384,24 +384,24 @@ def _request(self, function, params, service='shodan', method='get', json_data=N # Otherwise lets raise the error message error = u'{}'.format(e) - raise APIError(error) + raise APIError(error, data.status_code) elif data.status_code == 403: - raise APIError('Access denied (403 Forbidden)') + raise APIError('Access denied (403 Forbidden)', data.status_code) elif data.status_code == 502: - raise APIError('Bad Gateway (502)') + raise APIError('Bad Gateway (502)', data.status_code) # Parse the text into JSON try: - data = data.json() + parsed_data = data.json() except ValueError: - raise APIError('Unable to parse JSON response') + raise APIError('Unable to parse JSON response', data.status_code) # Raise an exception if an error occurred - if type(data) == dict and 'error' in data: - raise APIError(data['error']) + if type(parsed_data) == dict and 'error' in parsed_data: + raise APIError(parsed_data['error'], data.status_code) # Return the data - return data + return parsed_data def count(self, query, facets=None): """Returns the total number of search results for the query. From 2c93aa6e91f5a5d2ab9fc4010f9b61e5777100ba Mon Sep 17 00:00:00 2001 From: Gal Birka Date: Fri, 19 Jul 2024 22:25:25 +0300 Subject: [PATCH 3/4] if not status code is provided, set default to 0 --- shodan/exception.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shodan/exception.py b/shodan/exception.py index 0121da8..2534904 100644 --- a/shodan/exception.py +++ b/shodan/exception.py @@ -1,6 +1,7 @@ class APIError(Exception): """This exception gets raised whenever a non-200 status code was returned by the Shodan API.""" - def __init__(self, value, status_code=None): + + def __init__(self, value, status_code=0): self.value = value self.status_code = status_code From c3ebf4dbe4c58f0be8c4106fc09e9058fa0778aa Mon Sep 17 00:00:00 2001 From: Gal Birka Date: Fri, 19 Jul 2024 22:26:38 +0300 Subject: [PATCH 4/4] use isinstace instead of type to check type --- shodan/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shodan/client.py b/shodan/client.py index e7bac69..8c1036b 100644 --- a/shodan/client.py +++ b/shodan/client.py @@ -397,7 +397,7 @@ def _request(self, function, params, service='shodan', method='get', json_data=N raise APIError('Unable to parse JSON response', data.status_code) # Raise an exception if an error occurred - if type(parsed_data) == dict and 'error' in parsed_data: + if isinstance(parsed_data, dict) and 'error' in parsed_data: raise APIError(parsed_data['error'], data.status_code) # Return the data