Commit 8599e72e authored by Jonas Braathen's avatar Jonas Braathen
Browse files

Add more functionality related to branches

parent 3ce5d485
......@@ -2,9 +2,10 @@ import logging
import requests
import uuid
from typing import Optional, List, Iterator
from urllib.parse import urljoin, urlparse
from .models import Operator, Person
from .models import Branch, BranchReference, Operator, Person
logger = logging.getLogger(__name__)
......@@ -16,6 +17,12 @@ class Endpoints:
def _prepend_base_url(self, path):
return urljoin(self.base_url, path)
def list_branches(self):
return self._prepend_base_url('branches/')
def get_branch(self, branch_id):
return urljoin(self._prepend_base_url('branches/id/'), branch_id)
def get_operator(self, identity):
return urljoin(self._prepend_base_url('operators/id/'), identity)
......@@ -88,6 +95,11 @@ class TopDeskClient:
def patch(self, url, **kwargs):
return'PATCH', url, **kwargs)
def include_fields(self, params: dict, fields: Optional[List[str]] = None) -> None:
if not fields:
params['$fields'] = ','.join(fields)
def get_operator(self, identity):
if isinstance(identity, Operator):
url = self.urls.get_operator(
......@@ -172,6 +184,28 @@ class TopDeskClient:
for x in self.get(self.urls.get_persons()):
yield Person.from_dict(x)
def list_branches(self, fields: Optional[list] = None, name_filter: Optional[str] = None) -> Iterator[BranchReference]:
params = {}
fields or ['id', 'name', 'clientReferenceNumber'])
if name_filter:
params['nameFragment'] = name_filter
for x in self.get(self.urls.list_branches(), params=params):
yield BranchReference.from_dict(x)
def get_branch(self, branch_id: str) -> Optional[Branch]:
url = self.urls.get_branch(branch_id)
r = self.get(url, return_response=True)
if r.status_code == 200:
return Branch.from_dict(r.json())
return None
def create_branch(self, branch: Branch):
url = self.urls.list_branches()
def get_client(config):
return TopDeskClient(**config)
......@@ -76,14 +76,14 @@ class AddressTypeEnum(str, Enum):
class Address(BaseModel):
street: str
number: str
country: Reference
city: str
county: str
postcode: str
memo: str
type: AddressTypeEnum
street: Optional[str]
number: Optional[str]
country: Optional[Reference]
city: Optional[str]
county: Optional[str]
postcode: Optional[str]
memo: Optional[str]
type: Optional[AddressTypeEnum]
class Config:
fields = {
......@@ -99,27 +99,27 @@ class BranchTypeEnum(str, Enum):
class Branch(BaseModel):
id: str
name: str
specification: str
client_reference_number: str
timezone: str
extra_a: Reference
extra_b: Reference
phone: str
fax: str
address: Address
postal_address: Address
email: str
website: str
type: BranchTypeEnum
head_branch: Identifier
creator: Reference
created: datetime.datetime
modified: datetime.datetime
modifier: Reference
optional_fields_1: OptionalFields
optional_fields_2: OptionalFields
id: str = None
name: str = None
specification: str = None
client_reference_number: str = None
timezone: str = None
extra_a: Optional[Reference]
extra_b: Optional[Reference]
phone: str = None
fax: str = None
address: Address = None
postal_address: Address = None
email: str = None
website: str = None
type: Optional[BranchTypeEnum]
head_branch: Optional[Identifier]
creator: Optional[Reference]
created: Optional[datetime.datetime]
modified: Optional[datetime.datetime]
modifier: Optional[Reference]
optional_fields_1: Optional[OptionalFields]
optional_fields_2: Optional[OptionalFields]
class Config:
fields = {
......@@ -136,12 +136,17 @@ class Branch(BaseModel):
'modified': {'alias': 'modificationDate'},
def dict(self, *args, **kwargs):
kwargs['exclude'] = {'id'}
return super().dict(*args, **kwargs)
class BranchReference(BaseModel):
id: str
name: str
client_reference_number: str
timezone: str
client_reference_number: Optional[str]
timezone: Optional[str]
extra_a: Optional[Reference]
extra_b: Optional[Reference]
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