Commit 7ad56e70 authored by Jonas Braathen's avatar Jonas Braathen
Browse files

Add method for listing operator groups

parent 5d386029
......@@ -5,7 +5,7 @@ import uuid
from typing import Optional, List, Iterator
from urllib.parse import urljoin, urlparse
from .models import (Branch, BranchReference, Operator, Person,
from .models import (Branch, BranchReference, Operator, OperatorGroup, Person,
Department, DepartmentRef, BudgetHolder, BudgetHolderRef)
logger = logging.getLogger(__name__)
......@@ -21,6 +21,9 @@ class Endpoints:
def list_branches(self):
return self._prepend_base_url('branches/')
def list_operatorgroups(self):
return self._prepend_base_url('operatorgroups')
def get_branch(self, branch_id):
return urljoin(self._prepend_base_url('branches/id/'), branch_id)
......@@ -102,6 +105,25 @@ class TopDeskClient:
def patch(self, url, **kwargs):
return self.call('PATCH', url, **kwargs)
def depaginate(self, url, page_size=100):
offset = 0
while True:
params = {
'page_size': page_size,
'start': offset,
}
page = self.get(url, params=params, return_response=True)
if page.status_code == 204:
break
data = page.json()
for item in data:
yield item
num = len(data)
if num < page_size:
break
if num == page_size:
offset += num
def include_fields(self, params: dict, fields: Optional[List[str]] = None) -> None:
if not fields:
return
......@@ -215,6 +237,11 @@ class TopDeskClient:
for x in self.get(self.urls.get_operators()):
yield Operator.from_dict(x)
def list_operatorgroups(self):
url = self.urls.list_operatorgroups()
for x in self.depaginate(url):
yield OperatorGroup.from_dict(x)
def get_person(self, identity):
if isinstance(identity, Person):
url = self.urls.get_person(identity.id)
......
......@@ -190,6 +190,17 @@ class Operator(BaseModel):
return super(Operator, self).dict(*args, **kwargs)
class OperatorGroup(BaseModel):
id: str
groupName: str
# TODO: add the other 40 fields if needed
class Config:
fields = {
'name': {'alias': 'groupName'},
}
class ExternalLink(BaseModel):
id: str
type: str
......
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