Added fetch draft

This commit is contained in:
Kevin Veen-Birkenbach 2023-09-03 20:22:20 +02:00
parent 316343c6c2
commit 0f265ba381
2 changed files with 71 additions and 19 deletions

View File

@ -1,2 +1,45 @@
# baserow-ifto
This repository contains the Input Filter Transform and Output scripts to work with baserow data
This repository contains the Input Filter Transform and Output (IFTO) scripts to work with Baserow data.
## Usage
### Fetching All Data from a Baserow Database
We have a Python script that allows you to fetch all data from a Baserow database using its API.
#### Requirements
- Python 3.x
- `requests` library. You can install it using pip:
```bash
pip install requests
```
#### How to Use
1. Clone this repository:
```bash
git clone https://github.com/yourusername/baserow-ifto.git
cd baserow-ifto
```
2. Run the script with the required arguments:
```bash
python fetch-all-data.py BASE_URL API_KEY DATABASE_ID
```
Replace `BASE_URL`, `API_KEY`, `DATABASE_ID` with the appropriate values:
- `BASE_URL`: Base URL of your Baserow instance, e.g., `https://YOUR_BASEROW_INSTANCE_URL/api/`
- `API_KEY`: Your Baserow API key.
- `DATABASE_ID`: ID of the Baserow database you want to fetch data from.
The script will then fetch all the data from the specified Baserow database and print it to the console.
## Contributing
If you have suggestions or improvements, feel free to open an issue or submit a pull request. Your contributions are welcome!

View File

@ -1,40 +1,49 @@
import requests
import argparse
BASE_URL = "https://YOUR_BASEROW_INSTANCE_URL/api/"
API_KEY = "YOUR_API_KEY"
HEADERS = {
"Authorization": f"Token {API_KEY}",
def get_all_rows_from_table(base_url, api_key, table_id):
headers = {
"Authorization": f"Token {api_key}",
"Content-Type": "application/json"
}
def get_all_rows_from_table(table_id):
rows = []
next_url = f"{BASE_URL}database/rows/table/{table_id}/"
next_url = f"{base_url}database/rows/table/{table_id}/"
while next_url:
response = requests.get(next_url, headers=HEADERS)
response = requests.get(next_url, headers=headers)
data = response.json()
rows.extend(data['results'])
next_url = data['next']
return rows
def get_all_tables_from_database(database_id):
response = requests.get(f"{BASE_URL}database/tables/database/{database_id}/", headers=HEADERS)
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)
tables = response.json()
return tables
def get_all_data_from_database(database_id):
tables = get_all_tables_from_database(database_id)
def get_all_data_from_database(base_url, api_key, database_id):
tables = get_all_tables_from_database(base_url, api_key, database_id)
data = {}
for table in tables:
table_id = table['id']
table_name = table['name']
data[table_name] = get_all_rows_from_table(table_id)
data[table_name] = get_all_rows_from_table(base_url, api_key, table_id)
return data
database_id = "YOUR_DATABASE_ID"
all_data = get_all_data_from_database(database_id)
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)