From 8d5d777de60031efe5ed2cfd21074d5c9799ae0a Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Mon, 28 Mar 2022 18:07:39 +0200 Subject: [PATCH] implemented apihandler class and functions --- telegram_bot/api_handler.py | 49 ++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handler.py b/telegram_bot/api_handler.py index 69c4974..aa9b4da 100644 --- a/telegram_bot/api_handler.py +++ b/telegram_bot/api_handler.py @@ -4,4 +4,51 @@ script for communicating with webservice to get data from database __author__ = "Florian Kellermann, Linus Eickhoff" __date__ = "16.03.2022" __version__ = "0.0.1" -__license__ = "None" \ No newline at end of file +__license__ = "None" + +#side-dependencies: none +#Work in Progress + +import sys +import os +import requests as r + +class API_Handler: + #class for communicating with webservice to get data from database + def __init__(self, db_adress): + self.db_adress = db_adress + return + + def get_auth_token(self, email, password): + payload = {'email': email, 'password': password} + with r.Session() as s: + p = s.post(self.db_adress + "/user/login", json=payload) + if p.status_code == 200: + self.token = p.json()["data"]['token'] + return p.json()["data"]['token'] + else: + #print server response + print(p.text) + return None + + def get_user_keywords(self, token, user_id): + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + token + ":" + user_id} + p = s.get(self.db_adress + "/api/keywords", headers=headers) + keywords_json = p.json()["data"] + keywords = [] + for keyword in keywords_json: + keywords.append(keyword["keyword"]) + return keywords + + +if __name__ == "__main__": + + print("This is a module for the telegram bot. It is not intended to be run directly.") + handler = API_Handler("https://aktienbot.flokaiser.com/api") + try: + handler.get_auth_token("bot@example.com", "bot") + except None: + print("Could not connect to server.") + + sys.exit(1) \ No newline at end of file