mirror of
https://github.com/kevinveenbirkenbach/baserow-ifto.git
synced 2024-11-21 10:21:05 +01:00
Refactored and transformed to mvc modell
This commit is contained in:
parent
0dbde23304
commit
fa84080068
@ -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):
|
||||
|
51
controller.py
Normal file
51
controller.py
Normal file
@ -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()
|
38
main.py
38
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()
|
Loading…
Reference in New Issue
Block a user