mirror of
https://github.com/kevinveenbirkenbach/baserow-ifto.git
synced 2025-09-09 03:17:14 +02:00
Refactored repositorories and API
This commit is contained in:
0
repository/__init__.py
Normal file
0
repository/__init__.py
Normal file
8
repository/api_repository.py
Normal file
8
repository/api_repository.py
Normal 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)
|
25
repository/database_repository.py
Normal file
25
repository/database_repository.py
Normal 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
|
47
repository/table_repository.py
Normal file
47
repository/table_repository.py
Normal file
@@ -0,0 +1,47 @@
|
||||
from .api_repository import ApiRepository
|
||||
"""
|
||||
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 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 TableRepository(ApiRepository):
|
||||
def get_all_rows_from_table(self, table_id):
|
||||
rows = []
|
||||
next_url = f"database/rows/table/{table_id}/"
|
||||
|
||||
while next_url:
|
||||
response = self.api.request_response(next_url)
|
||||
data = self.api.handle_api_response(response)
|
||||
if not data:
|
||||
break
|
||||
rows.extend(data['results'])
|
||||
next_url = data['next']
|
||||
|
||||
return rows
|
||||
|
||||
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 fetch_fields_for_table(self, table_id):
|
||||
response = self.api.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}")
|
||||
|
||||
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']
|
||||
|
||||
def get_link_fields_for_all_tables(self, tables_data):
|
||||
link_fields = {}
|
||||
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
|
||||
return link_fields
|
Reference in New Issue
Block a user