Started dashboard implementation

This commit is contained in:
Lucas Mathews
2024-05-26 18:03:50 +02:00
parent 241ab8529b
commit b835653fb8
9 changed files with 320 additions and 40 deletions

View File

@@ -9,16 +9,10 @@ import json
def authenticate_client(client_id, client_password):
try:
# Send a POST request to the /Client/Login endpoint with the client_id and password
response = requests.post(CONFIG["server"]["url"] + "/Client/Login", params={'client_id': client_id, 'password': client_password})
# Return the response from the API
return response
except requests.exceptions.RequestException as e:
# If a RequestException is raised, print the exception message
print(f"RequestException: {e}")
# Create a new Response object with a status code of 500 and the error message in the JSON body
response = Response()
response.status_code = 500
response._content = b'{"success": false, "message": "Could not connect to the server. Please try again later."}'
@@ -26,21 +20,93 @@ def authenticate_client(client_id, client_password):
def logout_client():
try:
# Load the session cookie from the file
with open('application\\session_cookie.json', 'r') as f:
cookies = json.load(f)
# Send a POST request to the /Client/Logout endpoint
response = requests.post(CONFIG["server"]["url"] + "/Client/Logout", cookies=cookies)
# Return the response from the API
with open('application\\session_data.json', 'r') as f: # Open the session_data.json file in read mode
session_data = json.load(f)
response = requests.post(CONFIG["server"]["url"] + "/Client/Logout", cookies=session_data['session_cookie'])
return response
except requests.exceptions.RequestException as e:
# If a RequestException is raised, print the exception message
print(f"RequestException: {e}")
# Create a new Response object with a status code of 500 and the error message in the JSON body
response = Response()
response.status_code = 500
response._content = b'{"success": false, "message": "Could not connect to the server. Please try again later."}'
return response
return response
def get_client(client_id):
try:
with open('application\\session_data.json', 'r') as f: # Open the session_data.json file in read mode
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Client", cookies=session_data['session_cookie'], params={'client_id': client_id})
return response.json()
except requests.exceptions.RequestException as e:
print(f"RequestException: {e}")
response = Response()
response.status_code = 500
response._content = b'{"success": false, "message": "Could not connect to the server. Please try again later."}'
return response.json()
def update_client(client_id, email=None, phone_number=None, address=None):
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
# Create a dictionary of parameters to update
params = {'client_id': client_id}
if email is not None:
params['email'] = email
if phone_number is not None:
params['phone_number'] = phone_number
if address is not None:
params['address'] = address
response = requests.put(
CONFIG["server"]["url"] + "/Client",
cookies=session_data['session_cookie'],
params=params
)
response.raise_for_status() # Raise an exception if the request failed
return response.json()
except requests.exceptions.RequestException as e:
print(f"RequestException: {e}")
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def get_accounts(client_id):
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(
CONFIG["server"]["url"] + "/Client/Accounts",
cookies=session_data['session_cookie'],
params={'client_id': client_id}
)
response.raise_for_status() # Raise an exception if the request failed
accounts = response.json()
if isinstance(accounts, str): # If the response is a string, convert it to a list of dictionaries
accounts = json.loads(accounts)
return accounts
except requests.exceptions.RequestException as e:
print(f"RequestException: {e}")
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def format_balance(balance): # Formats the balance as a currency string with comma seperator
return f"{balance:,.2f}"
def get_transactions(account_id):
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(
CONFIG["server"]["url"] + "/Account/Transactions",
cookies=session_data['session_cookie'],
params={'account_id': account_id}
)
response.raise_for_status() # Raise an exception if the request failed
transactions = response.json()
if isinstance(transactions, str): # If the response is a string, convert it to a list of dictionaries
transactions = json.loads(transactions)
return transactions
except requests.exceptions.RequestException as e:
print(f"RequestException: {e}")
return {'success': False, 'message': "Could not connect to the server. Please try again later."}