diff --git a/telegram_bot/api_handling/api_handler.py b/telegram_bot/api_handling/api_handler.py index bc5ef79..88810ce 100644 --- a/telegram_bot/api_handling/api_handler.py +++ b/telegram_bot/api_handling/api_handler.py @@ -74,15 +74,19 @@ class API_Handler: return None - def get_user_keywords(self, user_id): + def get_user_keywords(self, user_id, max_retries=10): """gets the keywords of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: list: list of keywords """ + if max_retries <= 0: + return None + keywords = [] with r.Session() as s: headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} @@ -95,7 +99,7 @@ class API_Handler: return keywords else: - return self.get_user_keywords(user_id) # might end in infinite loop!! + return self.get_user_keywords(user_id, max_retries-1) @@ -133,24 +137,32 @@ class API_Handler: return req.status_code - def get_user_shares(self, user_id): + def get_user_shares(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: list: list of shares """ + 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 + "/shares", headers=headers) - shares_json = req.json()["data"] - shares = [] - for share in shares_json: - shares.append(share["symbol"]) + if(req.status_code == 200): + shares_json = req.json()["data"] + shares = [] + for share in shares_json: + shares.append(share["symbol"]) - return shares + return shares + + else: + return self.get_user_shares(user_id, max_retries-1) def set_share(self, user_id, symbol): @@ -185,15 +197,19 @@ class API_Handler: return req.status_code - def get_user_transactions(self, user_id): + def get_user_transactions(self, user_id, max_retries=10): """gets the transactions of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: dict: dictionary of transactions """ + 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 + "/transactions", headers=headers) @@ -201,6 +217,8 @@ class API_Handler: if req.status_code == 200: transactions_dict = dict(req.json()["data"]) return transactions_dict + else: + return self.get_user_transactions(user_id, max_retries-1) def set_transaction(self, user_id, count, price, symbol, timestamp): @@ -223,20 +241,27 @@ class API_Handler: return req.status_code - def get_user_portfolio(self, user_id): + def get_user_portfolio(self, user_id, max_retries=10): """gets the portfolio of the user Args: user_id (int): id of the user + max_retries (int): max retries for the request Returns: dict: dictionary of portfolio """ + 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 + "/portfolio", headers=headers) - portfolio_dict = dict(req.json()["data"]) - return portfolio_dict + if req.status_code == 200: + portfolio_dict = dict(req.json()["data"]) + return portfolio_dict + else: + return self.get_user_portfolio(user_id, max_retries-1)