Skip to content

Commit

Permalink
feat: Add support for names in contacts (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tetrergeru authored May 30, 2024
1 parent dd6fead commit dd05340
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 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
21 changes: 16 additions & 5 deletions moira_client/models/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,42 @@ 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)


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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dd05340

Please sign in to comment.