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
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)