Refactored repositorories and API

This commit is contained in:
Kevin Veen-Birkenbach 2023-09-12 16:38:44 +02:00
parent 6fdfdc7d0e
commit 13f5ca0f15
8 changed files with 42 additions and 33 deletions

0
api/__init__.py Normal file
View File

View File

@ -28,6 +28,7 @@ class BaserowAPI:
try:
return response.json()
except requests.RequestsJSONDecodeError:
except:
print({response.content})
print("Error: Failed to decode the response as JSON.")
return None

View File

@ -1,7 +1,7 @@
import argparse
import json
from baserow_api import BaserowAPI
from data_processor import DataProcessor
from api.baserow_api import BaserowAPI
from repository.table_repository import TableRepository
from matrix_builder import MatrixBuilder
import view
@ -12,7 +12,7 @@ def handle_output(quiet, data):
def main():
args = parse_arguments()
api = BaserowAPI(args.base_url, args.api_key, args.verbose)
data_processor = DataProcessor(api, args.verbose)
data_processor = TableRepository(api, args.verbose)
if args.table_ids:
tables_data = fetch_table_data(data_processor, args.table_ids)

View File

@ -1,4 +1,4 @@
from data_processor import DataProcessor
from repository.table_repository import TableRepository
class MatrixBuilder:
def __init__(self, data_processor, tables_data, verbose=False):

0
repository/__init__.py Normal file
View File

View File

@ -0,0 +1,8 @@
class ApiRepository:
def __init__(self, api, verbose=False):
self.api = api
self.verbose = verbose
def print_verbose_message(self, message):
if self.verbose:
print(message)

View File

@ -0,0 +1,25 @@
from api_repository import ApiRepository
"""
This class, Database, is responsible for interacting with a given API to fetch and process data related to databases and tables. It provides functionalities to:
Fetch all tables associated with a given database.
Extract all data from a specified database.
Additionally, it offers a verbose mode to print detailed messages during its operations.
@Todo This is buggy and needs to be optimized
"""
class DatabaseRepository(ApiRepository):
def get_all_tables_from_database(self, database_id):
response = self.api.request_response(f"database/tables/database/{database_id}/")
return self.api.handle_api_response(response) or []
def get_all_data_from_database(self, database_id):
tables = self.get_all_tables_from_database(database_id)
data = {}
for table in tables:
table_id = table['id']
table_name = table['name']
data[table_name] = self.get_all_rows_from_table(table_id)
return data

View File

@ -1,22 +1,13 @@
from .api_repository import ApiRepository
"""
This class, DataProcessor, is responsible for interacting with a given API to fetch and process data related to databases and tables. It provides functionalities to:
This class, Table, is responsible for interacting with a given API to fetch and process data related to tables. It provides functionalities to:
Retrieve all rows from a specified table.
Fetch all tables associated with a given database.
Extract all data from a specified database.
Fetch specific fields for a table.
Identify and retrieve 'link_row' type fields for a table and for all tables in the provided data.
Additionally, it offers a verbose mode to print detailed messages during its operations.
"""
class DataProcessor:
def __init__(self, api, verbose=False):
self.api = api
self.verbose = verbose
def print_verbose_message(self, message):
if self.verbose:
print(message)
class TableRepository(ApiRepository):
def get_all_rows_from_table(self, table_id):
rows = []
next_url = f"database/rows/table/{table_id}/"
@ -38,22 +29,6 @@ class DataProcessor:
tables_data[table_id] = table_data
return tables_data
def get_all_tables_from_database(self, database_id):
response = self.api.request_response(f"database/tables/database/{database_id}/")
return self.api.handle_api_response(response) or []
def get_all_data_from_database(self, database_id):
tables = self.get_all_tables_from_database(database_id)
data = {}
for table in tables:
table_id = table['id']
table_name = table['name']
data[table_name] = self.get_all_rows_from_table(table_id)
return data
def fetch_fields_for_table(self, table_id):
response = self.api.request_response(f"database/fields/table/{table_id}/")
if response.status_code == 200: