From 9225a0f4ec0f0cfd15085ebe54cb5beb4fdfe1bc Mon Sep 17 00:00:00 2001 From: Tetrergeru Date: Fri, 24 May 2024 15:20:34 +0200 Subject: [PATCH] Add wrapper for raise_for_status --- moira_client/client.py | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/moira_client/client.py b/moira_client/client.py index d171cda..a8c149a 100644 --- a/moira_client/client.py +++ b/moira_client/client.py @@ -21,6 +21,14 @@ def __init__(self, content): """ self.content = content +class MoiraApiError(Exception): + def __init__(self, body): + """ + + :param body: response body + """ + self.body = body + class Client: def __init__(self, api_url, auth_custom=None, auth_user=None, auth_pass=None, login=None): @@ -64,7 +72,12 @@ def get(self, path='', **kwargs): :raises: InvalidJSONError """ r = requests.get(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs) - r.raise_for_status() + + try: + r.raise_for_status() + except requests.exceptions.HTTPError as err: + raise MoiraApiError(r.content) + try: return r.json() except ValueError: @@ -81,7 +94,12 @@ def delete(self, path='', **kwargs): :raises: InvalidJSONError """ r = requests.delete(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs) - r.raise_for_status() + + try: + r.raise_for_status() + except requests.exceptions.HTTPError as err: + raise MoiraApiError(r.content) + try: return r.json() except ValueError: @@ -98,7 +116,12 @@ def put(self, path='', **kwargs): :raises: InvalidJSONError """ r = requests.put(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs) - r.raise_for_status() + + try: + r.raise_for_status() + except requests.exceptions.HTTPError as err: + raise MoiraApiError(r.content) + try: return r.json() except ValueError: @@ -115,7 +138,12 @@ def post(self, path='', **kwargs): :raises: InvalidJSONError """ r = requests.post(self._path_join(path), headers=self.headers, auth=self.auth, **kwargs) - r.raise_for_status() + + try: + r.raise_for_status() + except requests.exceptions.HTTPError as err: + raise MoiraApiError(r.content) + try: return r.json() except ValueError: