mirror of
https://github.com/kevinveenbirkenbach/baserow-ifto.git
synced 2024-11-23 11:21:04 +01:00
Refactored repositorories and API
This commit is contained in:
parent
6fdfdc7d0e
commit
13f5ca0f15
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
@ -28,6 +28,7 @@ class BaserowAPI:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return response.json()
|
return response.json()
|
||||||
except requests.RequestsJSONDecodeError:
|
except:
|
||||||
|
print({response.content})
|
||||||
print("Error: Failed to decode the response as JSON.")
|
print("Error: Failed to decode the response as JSON.")
|
||||||
return None
|
return None
|
@ -1,7 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
from baserow_api import BaserowAPI
|
from api.baserow_api import BaserowAPI
|
||||||
from data_processor import DataProcessor
|
from repository.table_repository import TableRepository
|
||||||
from matrix_builder import MatrixBuilder
|
from matrix_builder import MatrixBuilder
|
||||||
import view
|
import view
|
||||||
|
|
||||||
@ -12,7 +12,7 @@ def handle_output(quiet, data):
|
|||||||
def main():
|
def main():
|
||||||
args = parse_arguments()
|
args = parse_arguments()
|
||||||
api = BaserowAPI(args.base_url, args.api_key, args.verbose)
|
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:
|
if args.table_ids:
|
||||||
tables_data = fetch_table_data(data_processor, args.table_ids)
|
tables_data = fetch_table_data(data_processor, args.table_ids)
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
from data_processor import DataProcessor
|
from repository.table_repository import TableRepository
|
||||||
|
|
||||||
class MatrixBuilder:
|
class MatrixBuilder:
|
||||||
def __init__(self, data_processor, tables_data, verbose=False):
|
def __init__(self, data_processor, tables_data, verbose=False):
|
||||||
|
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
|
@ -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.
|
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.
|
Fetch specific fields for a table.
|
||||||
Identify and retrieve 'link_row' type fields for a table and for all tables in the provided data.
|
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.
|
Additionally, it offers a verbose mode to print detailed messages during its operations.
|
||||||
"""
|
"""
|
||||||
class DataProcessor:
|
class TableRepository(ApiRepository):
|
||||||
def __init__(self, api, verbose=False):
|
|
||||||
self.api = api
|
|
||||||
self.verbose = verbose
|
|
||||||
|
|
||||||
def print_verbose_message(self, message):
|
|
||||||
if self.verbose:
|
|
||||||
print(message)
|
|
||||||
|
|
||||||
def get_all_rows_from_table(self, table_id):
|
def get_all_rows_from_table(self, table_id):
|
||||||
rows = []
|
rows = []
|
||||||
next_url = f"database/rows/table/{table_id}/"
|
next_url = f"database/rows/table/{table_id}/"
|
||||||
@ -38,22 +29,6 @@ class DataProcessor:
|
|||||||
tables_data[table_id] = table_data
|
tables_data[table_id] = table_data
|
||||||
return tables_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):
|
def fetch_fields_for_table(self, table_id):
|
||||||
response = self.api.request_response(f"database/fields/table/{table_id}/")
|
response = self.api.request_response(f"database/fields/table/{table_id}/")
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
Loading…
Reference in New Issue
Block a user