From fa84080068052e572b692f22fb4eebd03d991d14 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Mon, 4 Sep 2023 19:08:23 +0200 Subject: [PATCH] Refactored and transformed to mvc modell --- baserow_api.py | 29 ++++++++++++++-------------- controller.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 38 ++----------------------------------- view.py | 12 ++++++++++++ 4 files changed, 80 insertions(+), 50 deletions(-) create mode 100644 controller.py create mode 100644 view.py diff --git a/baserow_api.py b/baserow_api.py index 90b6762..66dbf3d 100644 --- a/baserow_api.py +++ b/baserow_api.py @@ -6,8 +6,8 @@ class BaserowAPI: self.base_url = base_url self.api_key = api_key self.verbose = verbose - self.headers = create_headers(self) - self.print_verbose_message("Headers:", headers) + self.headers = self.create_headers() + self.print_verbose_message(f"Headers:{self.headers}") def create_headers(self): """Create headers for API requests.""" @@ -23,11 +23,12 @@ class BaserowAPI: def handle_api_response(self, response): """Handle API response, check for errors and decode JSON.""" self.print_verbose_message("[INFO] Handling API response...") - self.print_verbose_message("Response Status Code:", response.status_code) - self.print_verbose_message("Response Headers:", response.headers) + self.print_verbose_message(f"Response Status Code: {response.status_code}") + self.print_verbose_message(f"Response Headers: {response.headers}") if response.status_code != 200: print(f"Error: Received status code {response.status_code} from Baserow API.") - print("Response content:", response.content.decode()) + response_content=response.content.decode() + print("Response content: {response_content}") return None try: @@ -40,11 +41,11 @@ class BaserowAPI: if self.verbose: print(f"[INFO] Fetching all rows from table with ID: {table_id}...") rows = [] - next_url = "database/rows/table/{table_id}/" + next_url = f"database/rows/table/{table_id}/" while next_url: - request_response(next_url) - self.print_verbose_message("Requesting:", next_url) + response=self.request_response(next_url) + self.print_verbose_message(f"Requesting: {next_url}") data = self.handle_api_response(response) if not data: break @@ -58,7 +59,7 @@ class BaserowAPI: def get_all_tables_from_database(self, database_id): self.print_verbose_message("[INFO] Fetching all tables from database with ID: {database_id}...") - response = request_response("database/tables/database/{database_id}/") + response = self.request_response(f"database/tables/database/{database_id}/") return self.handle_api_response(response) or [] def get_all_data_from_database(self, database_id): @@ -75,7 +76,7 @@ class BaserowAPI: def fetch_fields_for_table(self, table_id): """Fetch fields for a given table.""" - response = requests.get(f"{self.base_url}database/fields/table/{table_id}/", headers=self.headers) + response = self.request_response(f"database/fields/table/{table_id}/") if response.status_code == 200: return response.json() raise Exception(f"Failed to fetch fields for table {table_id}. Status code: {response.status_code}") @@ -85,12 +86,12 @@ class BaserowAPI: indexed_data = self.index_tables_by_id(tables_data) link_fields = self.get_link_fields_for_all_tables(tables_data) self.embed_referenced_data_into_tables(tables_data, indexed_data, link_fields) - self.print_verbose_message("Merged Tables Data:", tables_data) + self.print_verbose_message(f"Merged Tables Data: {tables_data}") return tables_data def index_tables_by_id(self, tables_data): indexed_data = {table_name: {row['id']: row for row in rows} for table_name, rows in tables_data.items()} - self.print_verbose_message("Indexed Data: {indexed_data}") + self.print_verbose_message(f"Indexed Data: {indexed_data}") return indexed_data def get_link_fields_for_all_tables(self, tables_data): @@ -98,8 +99,8 @@ class BaserowAPI: for table_name in tables_data: link_fields_for_table = self.get_link_fields_for_table(table_name) link_fields[table_name] = link_fields_for_table - self.print_verbose_message("Link Fields For Table: {link_fields_for_table}") - self.print_verbose_message("Link Fields: {link_fields}") + self.print_verbose_message(f"Link Fields For Table: {link_fields_for_table}") + self.print_verbose_message(f"Link Fields: {link_fields}") return link_fields def get_link_fields_for_table(self, table_name): diff --git a/controller.py b/controller.py new file mode 100644 index 0000000..eea07cc --- /dev/null +++ b/controller.py @@ -0,0 +1,51 @@ +# controller.py + +import argparse +import json +from baserow_api import BaserowAPI +import view + +def main(): + args = parse_arguments() + api = BaserowAPI(args.base_url, args.api_key, args.verbose) + + if not args.database_id and not args.table_ids: + view.print_error_message("Either database_id or table_ids must be provided.") + exit(1) + + if args.table_ids: + tables_data = fetch_table_data(api, args.table_ids) + if args.matrix: + merged_data = api.merge_tables_on_reference(tables_data) + if not args.quiet: + view.print_json_output(merged_data) + else: + if not args.quiet: + view.print_json_output(tables_data) + else: + all_data = api.get_all_data_from_database(args.database_id) + if not args.quiet: + view.print_json_output(all_data) + +def parse_arguments(): + parser = argparse.ArgumentParser(description="Fetch all data from a Baserow database.") + parser.add_argument("base_url", help="Base URL of your Baserow instance, e.g., https://YOUR_BASEROW_INSTANCE_URL/api/") + parser.add_argument("api_key", help="Your Baserow API key.") + parser.add_argument("--database_id", help="ID of the Baserow database you want to fetch data from.", default=None) + parser.add_argument("--table_ids", help="IDs of the Baserow tables you want to fetch data from, separated by commas.", default=None) + parser.add_argument("--matrix", action="store_true", help="Merge tables based on references.") + parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose mode for debugging.") + parser.add_argument("--linked_fields", action="store_true", help="Outputs the linked tables") + parser.add_argument("--quiet", action="store_true", help="Suppress output of json") + return parser.parse_args() + +def fetch_table_data(api, table_ids_str): + table_ids = table_ids_str.split(',') + tables_data = {} + for table_id in table_ids: + table_data = api.get_all_rows_from_table(table_id.strip()) + tables_data[table_id] = table_data + return tables_data + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py index 1f54bc4..300b218 100644 --- a/main.py +++ b/main.py @@ -1,38 +1,4 @@ -import argparse -import json - -from baserow_api import BaserowAPI +import controller if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Fetch all data from a Baserow database.") - parser.add_argument("base_url", help="Base URL of your Baserow instance, e.g., https://YOUR_BASEROW_INSTANCE_URL/api/") - parser.add_argument("api_key", help="Your Baserow API key.") - parser.add_argument("--database_id", help="ID of the Baserow database you want to fetch data from.", default=None) - parser.add_argument("--table_ids", help="IDs of the Baserow tables you want to fetch data from, separated by commas.", default=None) - parser.add_argument("--matrix", action="store_true", help="Merge tables based on references.") - parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose mode for debugging.") - parser.add_argument("--linked_fields", action="store_true", help="Outputs the linked tables") - parser.add_argument("--quiet", action="store_true", help="Suppress output of json") - - args = parser.parse_args() - api = BaserowAPI(args.base_url, args.api_key, args.verbose) - - if not args.database_id and not args.table_ids: - print("Error: Either database_id or table_ids must be provided.") - exit(1) - - if args.table_ids: - table_ids = args.table_ids.split(',') - tables_data = {} - for table_id in table_ids: - table_data = api.get_all_rows_from_table(table_id.strip()) - tables_data[table_id] = table_data - - if args.matrix: - merged_data = api.merge_tables_on_reference(tables_data) - if not args.quiet: print(json.dumps(merged_data, indent=4)) - else: - if not args.quiet: print(json.dumps(tables_data, indent=4)) - else: - all_data = api.get_all_data_from_database(args.database_id) - if not args.quiet: print(json.dumps(all_data, indent=4)) + controller.main() \ No newline at end of file diff --git a/view.py b/view.py new file mode 100644 index 0000000..c2ddd74 --- /dev/null +++ b/view.py @@ -0,0 +1,12 @@ +import json + +def print_error_message(message): + print(f"Error: {message}") + +def print_json_output(data): + print(json.dumps(data, indent=4)) + +def print_verbose_message(message, data=None): + print(message) + if data: + print(data)