Commit 72b6d1b2 authored by Jo Sama's avatar Jo Sama 😱
Browse files

Better interface for get_(person|operator)

parent 049ce76a
......@@ -88,23 +88,25 @@ class TopDeskClient:
def patch(self, url, **kwargs):
return self.call('PATCH', url, **kwargs)
def get_operator(self, identity=None, username=None):
if identity:
url = self.urls.get_operator(identity)
return Operator.from_dict(self.get(url))
elif username:
def get_operator(self, identity):
if isinstance(identity, Operator):
url = self.urls.get_operator(identity.id)
r = self.get(url, return_response=True)
if r.status_code == 200:
return Operator.from_dict(r.json())
else:
return None
else:
url = self.urls.get_operators()
operators = [Operator.from_dict(x) for x in
self.get(url,
params={'topdesk_login_name': username})]
f = filter(lambda x: x.user_name.lower() == username,
params={'topdesk_login_name': identity})]
f = filter(lambda x: x.user_name.lower() == identity,
operators)
try:
return next(f)
except StopIteration:
return None
else:
raise ValueError('identity or username must be set')
def create_operator(self, operator):
url = self.urls.get_operators()
......@@ -129,8 +131,24 @@ class TopDeskClient:
yield Operator.from_dict(x)
def get_person(self, identity):
url = self.urls.get_person(identity)
return Person.from_dict(self.get(url))
if isinstance(identity, Person):
url = self.urls.get_person(identity.id)
r = self.get(url, return_response=True)
if r.status_code == 200:
return Person.from_dict(r.json())
else:
return None
else:
url = self.urls.get_persons()
persons = [Person.from_dict(x) for x in
self.get(url,
params={'ssp_login_name': identity})]
f = filter(lambda x: x.user_name.lower() == identity,
persons)
try:
return next(f)
except StopIteration:
return None
def create_person(self, person):
url = self.urls.get_persons()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment