summaryrefslogtreecommitdiffstats
path: root/api.py
diff options
context:
space:
mode:
Diffstat (limited to 'api.py')
-rw-r--r--api.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/api.py b/api.py
new file mode 100644
index 0000000..9cfb820
--- /dev/null
+++ b/api.py
@@ -0,0 +1,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 {}