2023-09-03 20:12:18 +02:00
|
|
|
import requests
|
2023-09-03 21:29:04 +02:00
|
|
|
import json
|
2023-09-03 20:12:18 +02:00
|
|
|
|
2023-09-03 22:16:34 +02:00
|
|
|
class BaserowAPI:
|
|
|
|
def __init__(self, base_url, api_key, verbose=False):
|
|
|
|
self.base_url = base_url
|
|
|
|
self.api_key = api_key
|
|
|
|
self.verbose = verbose
|
2023-09-04 19:08:23 +02:00
|
|
|
self.headers = self.create_headers()
|
|
|
|
self.print_verbose_message(f"Headers:{self.headers}")
|
2023-09-03 22:16:34 +02:00
|
|
|
|
|
|
|
def create_headers(self):
|
|
|
|
"""Create headers for API requests."""
|
|
|
|
return {
|
|
|
|
"Authorization": f"Token {self.api_key}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
2023-09-04 18:04:41 +02:00
|
|
|
|
|
|
|
def print_verbose_message(self, message):
|
|
|
|
if self.verbose:
|
|
|
|
print(message)
|
2023-09-03 22:16:34 +02:00
|
|
|
|
|
|
|
def handle_api_response(self, response):
|
|
|
|
"""Handle API response, check for errors and decode JSON."""
|
2023-09-04 18:04:41 +02:00
|
|
|
self.print_verbose_message("[INFO] Handling API response...")
|
2023-09-04 19:08:23 +02:00
|
|
|
self.print_verbose_message(f"Response Status Code: {response.status_code}")
|
|
|
|
self.print_verbose_message(f"Response Headers: {response.headers}")
|
2023-09-03 22:16:34 +02:00
|
|
|
if response.status_code != 200:
|
|
|
|
print(f"Error: Received status code {response.status_code} from Baserow API.")
|
2023-09-04 19:08:23 +02:00
|
|
|
response_content=response.content.decode()
|
|
|
|
print("Response content: {response_content}")
|
2023-09-03 22:16:34 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
return response.json()
|
|
|
|
except requests.RequestsJSONDecodeError:
|
|
|
|
print("Error: Failed to decode the response as JSON.")
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_all_rows_from_table(self, table_id):
|
|
|
|
if self.verbose:
|
|
|
|
print(f"[INFO] Fetching all rows from table with ID: {table_id}...")
|
|
|
|
rows = []
|
2023-09-04 19:08:23 +02:00
|
|
|
next_url = f"database/rows/table/{table_id}/"
|
2023-09-03 22:16:34 +02:00
|
|
|
|
|
|
|
while next_url:
|
2023-09-04 19:08:23 +02:00
|
|
|
response=self.request_response(next_url)
|
|
|
|
self.print_verbose_message(f"Requesting: {next_url}")
|
2023-09-03 22:16:34 +02:00
|
|
|
data = self.handle_api_response(response)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
rows.extend(data['results'])
|
|
|
|
next_url = data['next']
|
|
|
|
|
|
|
|
return rows
|
|
|
|
|
2023-09-04 18:46:18 +02:00
|
|
|
def request_response(self,command):
|
|
|
|
return requests.get(f"{self.base_url}{command}", headers=self.headers)
|
|
|
|
|
2023-09-03 22:16:34 +02:00
|
|
|
def get_all_tables_from_database(self, database_id):
|
2023-09-04 18:04:41 +02:00
|
|
|
self.print_verbose_message("[INFO] Fetching all tables from database with ID: {database_id}...")
|
2023-09-04 19:08:23 +02:00
|
|
|
response = self.request_response(f"database/tables/database/{database_id}/")
|
2023-09-03 22:16:34 +02:00
|
|
|
return self.handle_api_response(response) or []
|
|
|
|
|
|
|
|
def get_all_data_from_database(self, database_id):
|
2023-09-04 18:04:41 +02:00
|
|
|
self.print_verbose_message("[INFO] Fetching all data from database with ID: {database_id}...")
|
2023-09-03 22:16:34 +02:00
|
|
|
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):
|
2023-09-04 23:00:45 +02:00
|
|
|
self.print_verbose_message("Fetch fields for a given table.")
|
2023-09-04 19:08:23 +02:00
|
|
|
response = self.request_response(f"database/fields/table/{table_id}/")
|
2023-09-03 22:16:34 +02:00
|
|
|
if response.status_code == 200:
|
|
|
|
return response.json()
|
2023-09-04 18:04:41 +02:00
|
|
|
raise Exception(f"Failed to fetch fields for table {table_id}. Status code: {response.status_code}")
|
2023-09-04 23:00:45 +02:00
|
|
|
|
2023-09-03 22:16:34 +02:00
|
|
|
|
2023-09-04 18:04:41 +02:00
|
|
|
def get_link_fields_for_all_tables(self, tables_data):
|
2023-09-03 22:16:34 +02:00
|
|
|
link_fields = {}
|
|
|
|
for table_name in tables_data:
|
2023-09-04 18:04:41 +02:00
|
|
|
link_fields_for_table = self.get_link_fields_for_table(table_name)
|
2023-09-03 22:37:59 +02:00
|
|
|
link_fields[table_name] = link_fields_for_table
|
2023-09-04 18:04:41 +02:00
|
|
|
return link_fields
|
2023-09-03 22:16:34 +02:00
|
|
|
|
2023-09-04 18:04:41 +02:00
|
|
|
def get_link_fields_for_table(self, table_name):
|
|
|
|
fields = self.fetch_fields_for_table(table_name)
|
|
|
|
return [field for field in fields if field['type'] == 'link_row']
|
|
|
|
|
2023-09-04 23:00:45 +02:00
|
|
|
def get_tables(self,table_ids):
|
|
|
|
tables_data = {}
|
|
|
|
for table_id in table_ids:
|
|
|
|
table_data = self.get_all_rows_from_table(table_id.strip())
|
|
|
|
tables_data[table_id] = table_data
|
|
|
|
return tables_data
|
|
|
|
|
|
|
|
def build_matrix(self, tables_data, reference_map={}):
|
|
|
|
"""Build a matrix with linked rows filled recursively."""
|
2023-09-04 23:37:07 +02:00
|
|
|
reference_map_child = reference_map.copy()
|
|
|
|
|
|
|
|
for table_name, table_rows in tables_data.copy().items():
|
|
|
|
self.process_link_fields(table_name, tables_data, reference_map_child)
|
|
|
|
self.fill_cells_with_related_content(table_name, table_rows, reference_map_child)
|
|
|
|
|
2023-09-04 23:00:45 +02:00
|
|
|
return tables_data
|
|
|
|
|
2023-09-04 23:37:07 +02:00
|
|
|
def fill_cells_with_related_content(self, table_name, table_rows, reference_map_child):
|
|
|
|
"""Fill cells with related content."""
|
|
|
|
for table_row in table_rows:
|
|
|
|
self.print_verbose_message(f"table_row: {table_row}")
|
|
|
|
for table_column_name, table_cell_content in table_row.items():
|
|
|
|
if table_column_name in reference_map_child:
|
2023-09-05 00:16:01 +02:00
|
|
|
#Iterriere über Zelleneinträge
|
|
|
|
#cell_identifier = self.generate_cell_identifier(table_name, table_column_name, table_row)
|
|
|
|
#self.print_verbose_message(f"cell_identifier: {cell_identifier}")
|
|
|
|
|
|
|
|
|
|
|
|
link_row_table_id = reference_map_child[table_column_name]["link_row_table_id"]
|
|
|
|
self.print_verbose_message(f"link_row_table_id: {link_row_table_id}")
|
|
|
|
|
|
|
|
link_row_related_field_id = reference_map_child[table_column_name]["link_row_related_field_id"]
|
|
|
|
self.print_verbose_message(f"link_row_related_field_id: {link_row_related_field_id}")
|
|
|
|
|
|
|
|
for entry_id,entry_content in table_cell_content:
|
|
|
|
related_cell_identifier=self.generate_related_cell_identifier(link_row_table_id,link_row_related_field_id,entry_id)
|
|
|
|
self.print_verbose_message(f"related_cell_identifier: {related_cell_identifier}")
|
|
|
|
|
|
|
|
if related_cell_identifier in reference_map_child[table_column_name]["embeded"]:
|
|
|
|
self.print_verbose_message(f"Skipped {related_cell_identifier}. Already implemented")
|
|
|
|
break
|
|
|
|
|
|
|
|
reference_map_child[table_column_name]["embeded"].add(related_cell_identifier)
|
|
|
|
|
2023-09-04 23:37:07 +02:00
|
|
|
|
2023-09-05 00:16:01 +02:00
|
|
|
def generate_related_cell_identifier(self, table_id, table_column_id, table_row_id):
|
|
|
|
return self.generate_cell_identifier(table_id, "field_" + str(table_column_id), table_row_id);
|
|
|
|
|
|
|
|
def generate_cell_identifier(self, table_name, table_column_name, table_row_id):
|
|
|
|
table_name=str(table_name)
|
|
|
|
table_column_name=str(table_column_name)
|
|
|
|
table_row_id=str(table_row_id)
|
|
|
|
return "table_" + table_name + "_" + table_column_name + "_row_" + table_row_id
|
2023-09-04 23:37:07 +02:00
|
|
|
|
|
|
|
def process_link_fields(self, table_name, tables_data, reference_map_child):
|
|
|
|
"""Process link fields for a given table."""
|
|
|
|
link_fields = self.get_link_fields_for_table(table_name)
|
|
|
|
|
|
|
|
for link_field in link_fields:
|
|
|
|
self.load_table_data_if_not_present(link_field, tables_data)
|
|
|
|
self.update_reference_map(link_field, reference_map_child)
|
|
|
|
|
|
|
|
def load_table_data_if_not_present(self, link_field, tables_data):
|
|
|
|
"""Load table data if it's not already loaded."""
|
|
|
|
link_row_table_id = link_field["link_row_table_id"]
|
|
|
|
|
|
|
|
if link_row_table_id not in tables_data:
|
|
|
|
tables_data[link_row_table_id] = self.get_all_rows_from_table(link_row_table_id)
|
|
|
|
|
|
|
|
def update_reference_map(self, link_field, reference_map_child):
|
|
|
|
"""Update the reference map with the link field data."""
|
|
|
|
link_field_name = "field_" + str(link_field["id"])
|
|
|
|
|
|
|
|
if link_field_name not in reference_map_child:
|
|
|
|
reference_map_child[link_field_name] = link_field
|
|
|
|
reference_map_child[link_field_name]["embeded"] = []
|
|
|
|
|
|
|
|
|
|
|
|
|