Skip to content

Commit

Permalink
Merge pull request #20 from alex9smith/handle-api-error
Browse files Browse the repository at this point in the history
Catch API errors when a query string is invalid
  • Loading branch information
alex9smith authored May 31, 2022
2 parents 5d873b9 + 88cb519 commit 471f53c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Changelog

## Unreleased
## 1.4.0
Validate `timespan` filter parameter to make sure it's an allowed value
Catch API errors when a query string is invalid and return them to the user

## 1.3.3
Fix a bug in `multi_repeat` which meant any filter using `OR` would fail
Expand Down
2 changes: 1 addition & 1 deletion gdeltdoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gdeltdoc.api_client import GdeltDoc
from gdeltdoc.filters import Filters, near, repeat, multi_repeat, VALID_TIMESPAN_UNITS

__version__ = "1.3.3"
__version__ = "1.4.0"
5 changes: 5 additions & 0 deletions gdeltdoc/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ def _query(self, mode: str, query_string: str) -> Dict:
if response.status_code not in [200, 202]:
raise ValueError("The gdelt api returned a non-successful statuscode. This is the response message: {}".
format(response.text))

# Response is text/html if it's an error and application/json if it's ok
if "text/html" in response.headers["content-type"]:
raise ValueError(f"The query was not valid. The API error message was: {response.text.strip()}")

return load_json(response.content, self.max_depth_json_parsing)
6 changes: 6 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ def test_vol_has_two_columns(self):

def test_vol_raw_has_three_columns(self):
self.assertEqual(self.all_results[1].shape[1], 3)

class QueryTestCase(unittest.TestCase):

def test_handles_invalid_query_string(self):
with self.assertRaisesRegex(ValueError, "The query was not valid. The API error message was"):
GdeltDoc()._query("artlist", "environment&timespan=mins15")

0 comments on commit 471f53c

Please sign in to comment.