2023-09-03 20:12:18 +02:00
|
|
|
import requests
|
2023-09-03 20:22:20 +02:00
|
|
|
import argparse
|
2023-09-03 20:12:18 +02:00
|
|
|
|
2023-09-03 20:22:20 +02:00
|
|
|
def get_all_rows_from_table(base_url, api_key, table_id):
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Token {api_key}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
2023-09-03 20:12:18 +02:00
|
|
|
rows = []
|
2023-09-03 20:22:20 +02:00
|
|
|
next_url = f"{base_url}database/rows/table/{table_id}/"
|
2023-09-03 20:12:18 +02:00
|
|
|
|
|
|
|
while next_url:
|
2023-09-03 20:22:20 +02:00
|
|
|
response = requests.get(next_url, headers=headers)
|
2023-09-03 20:12:18 +02:00
|
|
|
data = response.json()
|
|
|
|
rows.extend(data['results'])
|
|
|
|
next_url = data['next']
|
|
|
|
|
|
|
|
return rows
|
|
|
|
|
2023-09-03 20:22:20 +02:00
|
|
|
def get_all_tables_from_database(base_url, api_key, database_id):
|
|
|
|
headers = {
|
|
|
|
"Authorization": f"Token {api_key}",
|
|
|
|
"Content-Type": "application/json"
|
|
|
|
}
|
|
|
|
response = requests.get(f"{base_url}database/tables/database/{database_id}/", headers=headers)
|
2023-09-03 20:25:39 +02:00
|
|
|
|
|
|
|
# Check if the response status code indicates success
|
|
|
|
if response.status_code != 200:
|
|
|
|
print(f"Error: Received status code {response.status_code} from Baserow API.")
|
|
|
|
print("Response content:", response.content.decode())
|
|
|
|
return []
|
|
|
|
|
|
|
|
try:
|
|
|
|
tables = response.json()
|
|
|
|
return tables
|
|
|
|
except requests.RequestsJSONDecodeError:
|
|
|
|
print("Error: Failed to decode the response as JSON.")
|
|
|
|
return []
|
2023-09-03 20:12:18 +02:00
|
|
|
|
2023-09-03 20:22:20 +02:00
|
|
|
def get_all_data_from_database(base_url, api_key, database_id):
|
|
|
|
tables = get_all_tables_from_database(base_url, api_key, database_id)
|
2023-09-03 20:12:18 +02:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
for table in tables:
|
|
|
|
table_id = table['id']
|
|
|
|
table_name = table['name']
|
2023-09-03 20:22:20 +02:00
|
|
|
data[table_name] = get_all_rows_from_table(base_url, api_key, table_id)
|
2023-09-03 20:12:18 +02:00
|
|
|
|
|
|
|
return data
|
|
|
|
|
2023-09-03 20:22:20 +02:00
|
|
|
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.")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
all_data = get_all_data_from_database(args.base_url, args.api_key, args.database_id)
|
|
|
|
print(all_data)
|