Skip to content

Commit

Permalink
Feature redo basiq api part2 (#129)
Browse files Browse the repository at this point in the history
* Redo Basiq API Part2

Added function annotation
Optimized some functions

* Redo API Part 3

Change the location of DB for next trimester
  • Loading branch information
userstarwind authored Dec 1, 2023
1 parent b6bf1d9 commit 77b5faf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
81 changes: 61 additions & 20 deletions neo_dolfin/api/optimized_api/API_db_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
data_instance = Data()
access_token = core_instance.generate_auth_token()

## Operations specifically for interacting with the Dolfin Database


# create "dolfin" database, that is capable of holding user data specific to the dolfin app that can then be passed on to the BASIQ API - not yet implemented in APP.PY
# Operations specifically for interacting with the Dolfin Database
def init_dolfin_db():
"""
Initialize the DolFin database and create user and transaction tables.
Connects to the SQLite database and sets up foreign key constraints.
Creates users and transactions tables if they don't exist.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
conn.execute("PRAGMA foreign_keys = ON;")
cursor = conn.cursor()
cursor.execute('''
Expand Down Expand Up @@ -57,8 +60,13 @@ class VARCHAR(50),


def register_user(email, mobile, first_name, middle_name, last_name, password):
"""
Registers a new DolFin user.
Inserts user information into the users table.
Parameters include email, mobile number, first name, middle name, last name, and password.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
cursor.execute('''INSERT INTO users (email, mobile, first_name, middle_name, last_name, password)
VALUES (?, ?, ?, ?, ?, ?)''',
Expand All @@ -68,10 +76,14 @@ def register_user(email, mobile, first_name, middle_name, last_name, password):
except sqlite3.Error as e:
return "An error occurred: " + str(e)

# retrive basiq_id (that will be need for most user-specific calls to the API) based on the user ID (for the dolfin app) that has been passed.

def get_basiq_id(user_id):
"""
Retrieves the basiq ID for a specific DolFin user.
Queries the users table for the basiq ID based on the user ID.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT basiq_id FROM users WHERE u_id = ?", (user_id,))
result = cursor.fetchone()
Expand All @@ -84,8 +96,12 @@ def get_basiq_id(user_id):


def get_user_info(user_id):
"""
Retrieves information of a DolFin user.
Queries for basic information of a user by user ID, including email, mobile, first name, middle name, and last name.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT email, mobile, first_name, middle_name, last_name FROM users WHERE u_id = ?",
(user_id,))
Expand All @@ -104,11 +120,15 @@ def get_user_info(user_id):
except sqlite3.Error as e:
return "An error occurred: " + str(e)

# add basiq

def register_basiq_id(user_id):
"""
Registers a basiq ID for a DolFin user.
Generates a basiq ID based on user information and updates the basiq ID field in the users table.
"""
try:
new_basiq_id = json.loads(core_instance.create_user_by_dict(get_user_info(user_id), access_token)).get('id')
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
cursor.execute("UPDATE users SET basiq_id = ? WHERE u_id = ?", (new_basiq_id, user_id))
if cursor.rowcount == 0:
Expand All @@ -118,10 +138,14 @@ def register_basiq_id(user_id):
except sqlite3.Error as e:
return "An error occurred: " + str(e)

# Creates authorisation link to user - can be presented as a popup or sent as an email
def create_link_bank_account(user_id):

def link_bank_account(user_id):
"""
Link a DolFin user to their bank accounts.
Generates an authorization link based on the user's basiq ID and opens it in a web browser.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT basiq_id FROM users WHERE u_id = ?", (user_id,))
result = cursor.fetchone()
Expand All @@ -133,9 +157,14 @@ def create_link_bank_account(user_id):
except sqlite3.Error as e:
return "An error occurred: " + str(e)

# returns dataframe with the transactions
def request_transactions_df(user_id):
tran_data = json.loads(data_instance.get_transaction_list(get_basiq_id(user_id), access_token))

def request_transactions_df(user_id, limit_para=500, filter_para=''):
"""
Requests and returns user transaction data in DataFrame format.
Fetches the transaction list based on the user's basiq ID and converts it into a DataFrame.
"""
tran_data = json.loads(
data_instance.get_transaction_list(get_basiq_id(user_id), limit_para, filter_para, access_token))
transaction_list = tran_data['data']
transactions = []
for transaction in transaction_list:
Expand All @@ -160,8 +189,12 @@ def request_transactions_df(user_id):


def cache_transactions(user_id, tran_data):
"""
Caches transaction data for a Dolfin user.
Inserts transaction data into the transactions table, including transaction ID, type, status, description, etc.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
conn.execute("PRAGMA foreign_keys = ON;")
cursor = conn.cursor()
insert_statement = '''
Expand All @@ -181,21 +214,29 @@ def cache_transactions(user_id, tran_data):


def fetch_transactions_by_user(user_id):
"""
Fetches cached transaction data based on the user ID.
Queries the transactions table for all transaction information for a specific user.
"""
try:
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
query = "SELECT * FROM transactions WHERE trans_u_id = ?"
return pd.read_sql_query(query, conn, params=(user_id,))
except sqlite3.Error as e:
print(f"An error occurred: {e}")


def clear_transactions():
"""
Clears all cached data from the transactions table.
Deletes all records from the transactions table.
"""
try:
# Database connection
with sqlite3.connect("../../db/dolfin_db.db") as conn:
with sqlite3.connect("dolfin_db.db") as conn:
cursor = conn.cursor()
# SQL statement to delete all data from the transactions table
cursor.execute("DELETE FROM transactions;")
return "Transactions table cleared successfully."
except sqlite3.Error as e:
return "An error occurred: " + str(e)
return "An error occurred: " + str(e)
File renamed without changes.
2 changes: 1 addition & 1 deletion neo_dolfin/api/optimized_api/optimized_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def get_account(user_id, account_id, access_token):
return response.text

@staticmethod
def get_transaction_list(user_id, access_token, limit_para=500, filter_para=''):
def get_transaction_list(user_id, limit_para, filter_para, access_token):
url = f"https://au-api.basiq.io/users/{user_id}/transactions?limit={limit_para}&filter={filter_para}"

headers = {
Expand Down

0 comments on commit 77b5faf

Please sign in to comment.