summaryrefslogtreecommitdiffstats
path: root/api.py
blob: 9cfb820f93663ab1799d8b72d5278a09e1267fe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests


class TeamSpeakApi:
    """
    class to interact with the teamspeak rest api
    """

    def __init__(self, apikey: str, url: str):
        # variables
        self._apikey = apikey
        self._url = url

        # define api handler
        self.cmd = self._rest

        self.session = requests.Session()

    @property
    def _auth(self):
        if self._apikey is not None:
            return {"x-api-key": self._apikey}

    def _rest(self, command: str) -> dict:
        # add authentication header to the session obj
        auth_header = self._auth

        # build get request
        r = self.session.get("/".join([self._url, command]), headers=auth_header, verify=False)

        # proceed if response is ok
        if r.ok:
            return r.json()

        return {}