diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/baserow_api.py b/api/baserow_api.py similarity index 95% rename from baserow_api.py rename to api/baserow_api.py index c8d344c..1e9a031 100644 --- a/baserow_api.py +++ b/api/baserow_api.py @@ -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 \ No newline at end of file diff --git a/controller.py b/controller.py index f5f9dd7..5c94085 100644 --- a/controller.py +++ b/controller.py @@ -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) diff --git a/matrix_builder.py b/matrix_builder.py index 5c33722..1508fcc 100644 --- a/matrix_builder.py +++ b/matrix_builder.py @@ -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): diff --git a/repository/__init__.py b/repository/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/repository/api_repository.py b/repository/api_repository.py new file mode 100644 index 0000000..aedea14 --- /dev/null +++ b/repository/api_repository.py @@ -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) \ No newline at end of file diff --git a/repository/database_repository.py b/repository/database_repository.py new file mode 100644 index 0000000..e6d2e1b --- /dev/null +++ b/repository/database_repository.py @@ -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 \ No newline at end of file diff --git a/data_processor.py b/repository/table_repository.py similarity index 62% rename from data_processor.py rename to repository/table_repository.py index 04440b0..320ee77 100644 --- a/data_processor.py +++ b/repository/table_repository.py @@ -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: