Skip to content

Client

client

Client for the HelpDesk / LiveChat API

Documentation: https://api.helpdesk.com/docs

ToDo

JSON: TypeAlias = JSONObj | JSONLst module-attribute

Type of the JSON response as returned by the HelpDesk / LiveChat API

JSONLst: TypeAlias = list[JSONObj] module-attribute

Type of a JSON list of JSON objects

JSONObj: TypeAlias = dict[str, Any] module-attribute

Type of a JSON object (without recursion)

HelpDeskClient(config: Config | None = None)

Source code in src/pytanis/helpdesk/client.py
def __init__(self, config: Config | None = None):
    if config is None:
        config = get_cfg()
    self._config = config
    # Important: Always use a custom User-Agent, never a generic one.
    # Generic User-Agents are filtered by helpdesk to reduce spam.
    self._headers = {'User-Agent': 'Pytanis'}

    self._get_throttled = self._get
    self._post_throttled = self._post
    self.set_throttling(calls=1, seconds=10)  # Helpdesk is really strange when it comes to this

create_ticket(ticket: NewTicket)

Source code in src/pytanis/helpdesk/client.py
def create_ticket(self, ticket: NewTicket):
    return self.post('tickets', data=ticket.model_dump())

get(endpoint: str, params: QueryParams | None = None) -> JSON

Retrieve data via throttled GET request and return the JSON

Source code in src/pytanis/helpdesk/client.py
def get(self, endpoint: str, params: QueryParams | None = None) -> JSON:
    """Retrieve data via throttled GET request and return the JSON"""
    resp = self._get_throttled(endpoint, params)
    resp.raise_for_status()
    return resp.json()

list_agents() -> list[Agent]

Source code in src/pytanis/helpdesk/client.py
def list_agents(self) -> list[Agent]:
    agents = self.get('agents')
    if not isinstance(agents, list):
        msg = 'Received JSON is not a list object'
        raise ValueError(msg)
    return [Agent.model_validate(dct) for dct in agents]

list_teams() -> list[Team]

Source code in src/pytanis/helpdesk/client.py
def list_teams(self) -> list[Team]:
    teams = self.get('teams')
    if not isinstance(teams, list):
        msg = 'Received JSON is not a list object'
        raise ValueError(msg)
    return [Team.model_validate(dct) for dct in teams]

post(endpoint: str, data: dict[str, Any], params: QueryParams | None = None) -> JSON

Source code in src/pytanis/helpdesk/client.py
def post(self, endpoint: str, data: dict[str, Any], params: QueryParams | None = None) -> JSON:
    resp = self._post_throttled(endpoint, data, params)
    resp.raise_for_status()
    return resp.json()

set_throttling(calls: int, seconds: int)

Throttle the number of calls per seconds to the Pretalx API

Source code in src/pytanis/helpdesk/client.py
def set_throttling(self, calls: int, seconds: int):
    """Throttle the number of calls per seconds to the Pretalx API"""
    _logger.debug('throttling', calls=calls, seconds=seconds)
    self._get_throttled = throttle(calls, seconds)(self._get)
    self._post_throttled = throttle(calls, seconds)(self._post)