From 30bccbf2381d884096f348626a6a16763c0cd169 Mon Sep 17 00:00:00 2001 From: Linus E <75929322+Rripped@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:41:48 +0200 Subject: [PATCH] added user funcs of api --- telegram_bot/api_handling/api_handler.py | 48 +++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index bc8bc8a..33c9a41 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -74,6 +74,51 @@ class API_Handler: return None + def get_user(self, user_id, max_retries=10): + """gets the shares of the user + + Args: + user_id (int): id of the user + max_retries (int): max retries for the request + + Returns: + json: json of user infos + """ + if max_retries <= 0: + return None + + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} + req = s.get(self.db_adress + "/user", headers=headers) + if(req.status_code == 200): + return req.json()["data"] + + else: + return self.get_user(user_id, max_retries-1) + + + def get_all_users(self, max_retries=10): + """gets all users + + Args: + max_retries (int): max retries for the request + + Returns: + list: list of users + """ + if max_retries <= 0: + return None + + with r.Session() as s: + headers = {'Authorization': 'Bearer ' + self.token} + req = s.get(self.db_adress + "/users", headers=headers) + if(req.status_code == 200): + return req.json()["data"] + + else: + return self.get_all_users(max_retries-1) + + def get_user_keywords(self, user_id, max_retries=10): """gets the keywords of the user @@ -282,10 +327,11 @@ class API_Handler: 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", "bot@example.com", "bot") + handler = API_Handler("https://gruppe1.testsites.info/api", "bot@example.com", "bot") print(handler.token) keywords = handler.get_user_keywords(user_id = 1709356058) #user_id is currently mine (Linus) print(keywords) shares = handler.get_user_portfolio(user_id = 1709356058) + print(handler.set_cron_interval(user_id = 1709356058, cron_interval = "0 0 * * *")) print(shares) sys.exit(1) \ No newline at end of file