added max req params

This commit is contained in:
Linus E 2022-04-04 16:54:25 +02:00
parent f2c9f6365e
commit 0b8ae845ed

View File

@ -74,15 +74,19 @@ class API_Handler:
return None 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 """gets the keywords of the user
Args: Args:
user_id (int): id of the user user_id (int): id of the user
max_retries (int): max retries for the request
Returns: Returns:
list: list of keywords list: list of keywords
""" """
if max_retries <= 0:
return None
keywords = [] keywords = []
with r.Session() as s: with r.Session() as s:
headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)}
@ -95,7 +99,7 @@ class API_Handler:
return keywords return keywords
else: 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 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 """gets the shares of the user
Args: Args:
user_id (int): id of the user user_id (int): id of the user
max_retries (int): max retries for the request
Returns: Returns:
list: list of shares list: list of shares
""" """
if max_retries <= 0:
return None
with r.Session() as s: with r.Session() as s:
headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)}
req = s.get(self.db_adress + "/shares", headers=headers) req = s.get(self.db_adress + "/shares", headers=headers)
shares_json = req.json()["data"] if(req.status_code == 200):
shares = [] shares_json = req.json()["data"]
for share in shares_json: shares = []
shares.append(share["symbol"]) 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): def set_share(self, user_id, symbol):
@ -185,15 +197,19 @@ class API_Handler:
return req.status_code 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 """gets the transactions of the user
Args: Args:
user_id (int): id of the user user_id (int): id of the user
max_retries (int): max retries for the request
Returns: Returns:
dict: dictionary of transactions dict: dictionary of transactions
""" """
if max_retries <= 0:
return None
with r.Session() as s: with r.Session() as s:
headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)}
req = s.get(self.db_adress + "/transactions", headers=headers) req = s.get(self.db_adress + "/transactions", headers=headers)
@ -201,6 +217,8 @@ class API_Handler:
if req.status_code == 200: if req.status_code == 200:
transactions_dict = dict(req.json()["data"]) transactions_dict = dict(req.json()["data"])
return transactions_dict return transactions_dict
else:
return self.get_user_transactions(user_id, max_retries-1)
def set_transaction(self, user_id, count, price, symbol, timestamp): def set_transaction(self, user_id, count, price, symbol, timestamp):
@ -223,20 +241,27 @@ class API_Handler:
return req.status_code 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 """gets the portfolio of the user
Args: Args:
user_id (int): id of the user user_id (int): id of the user
max_retries (int): max retries for the request
Returns: Returns:
dict: dictionary of portfolio dict: dictionary of portfolio
""" """
if max_retries <= 0:
return None
with r.Session() as s: with r.Session() as s:
headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)} headers = {'Authorization': 'Bearer ' + self.token + ":" + str(user_id)}
req = s.get(self.db_adress + "/portfolio", headers=headers) req = s.get(self.db_adress + "/portfolio", headers=headers)
portfolio_dict = dict(req.json()["data"]) if req.status_code == 200:
return portfolio_dict portfolio_dict = dict(req.json()["data"])
return portfolio_dict
else:
return self.get_user_portfolio(user_id, max_retries-1)