Skip to content

Commit

Permalink
Add wrapper for raise_for_status
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru committed May 24, 2024
1 parent b3a5194 commit 9225a0f
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions moira_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 9225a0f

Please sign in to comment.