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: diff --git a/moira_client/models/contact.py b/moira_client/models/contact.py index 6f38407..a737ffc 100644 --- a/moira_client/models/contact.py +++ b/moira_client/models/contact.py @@ -21,6 +21,7 @@ def __init__(self, value='', type='', **kwargs): self.type = type self.value = value self.user = kwargs.get('user', None) + self.name = kwargs.get('name', None) self._id = kwargs.get('id', None) @@ -28,26 +29,34 @@ class ContactManager: def __init__(self, client): self._client = client - def add(self, value, contact_type): + def add(self, value, contact_type, name=None): """ Add new contact :param value: str contact value :param contact_type: str contact type (one of CONTACT_* constants) + :param name: str contact name :return: Contact :raises: ResponseStructureError """ - data = { - 'value': value, - 'type': contact_type - } contacts = self.fetch_by_current_user() for contact in contacts: if contact.value == value and contact.type == contact_type: + if name and contact.name != name: + contact.name = name + self.update(contact) + return contact return contact + data = { + 'value': value, + 'type': contact_type + } + if name: + data['name'] = name + result = self._client.put(self._full_path(), json=data) if 'id' not in result: raise ResponseStructureError('No id in response', result) @@ -135,6 +144,8 @@ def update(self, contact): 'type': contact.type, 'value': contact.value, } + if contact.name: + data['name'] = contact.name try: self._client.put(self._full_path(contact.id), json=data)