diff --git a/postgrestutils/__init__.py b/postgrestutils/__init__.py index 3f3dd564f598c123e57840a80ecf7b546553a15c..d3c1a1658253b4ec4af6e95e425ff76e6bd3484c 100644 --- a/postgrestutils/__init__.py +++ b/postgrestutils/__init__.py @@ -1,6 +1,7 @@ import logging from . import settings +from requests import HTTPError logger = logging.getLogger("postgrestutils") diff --git a/postgrestutils/client/postgrestclient.py b/postgrestutils/client/postgrestclient.py index 78bfb83a89c1f8f2171ea27f56f0760181f239ea..ce1cfc4b32333ef0d4d097b91def67f2ab41d2b2 100644 --- a/postgrestutils/client/postgrestclient.py +++ b/postgrestutils/client/postgrestclient.py @@ -16,16 +16,20 @@ class PostgrestClient: :param path_and_query: specifies the endpoint and optionally filter queries :param singular: if True returns a JSON object rather than a list (406 when multiple results are returned) :param parse_dt: if True attempts to parse datetime strings to python datetime objects - :return: result(s) as JSON or raises HTTPError + :return: result(s) as python object or raises HTTPError """ if singular: self.session.headers["Accept"] = "application/vnd.pgrst.object+json" else: self.session.headers["Accept"] = "application/json" - r = self.session.get(urljoin(self.base_uri, path_and_query)) - r.raise_for_status() + res = self.session.get(urljoin(self.base_uri, path_and_query)) + try: + res.raise_for_status() + except requests.HTTPError as e: + raise type(e)(res.status_code, res.reason, res.text) + if parse_dt: - json_result = r.json(object_hook=datetime_parser) + json_result = res.json(object_hook=datetime_parser) else: - json_result = r.json() + json_result = res.json() return json_result diff --git a/postgrestutils/settings.py b/postgrestutils/settings.py index 8bc5fe147d7c34fd83ad95c177bc0898a67d7344..e6cafc42c11eb1d933af8a1b0e6bb107a3d48363 100644 --- a/postgrestutils/settings.py +++ b/postgrestutils/settings.py @@ -1,6 +1,4 @@ -import re - -BASE_URI = str() +BASE_URI = "http://127.0.0.1:3000" JWT = str()