Files
feishu_bitable/batch_get_records.py
2026-03-23 10:45:02 +08:00

59 lines
1.9 KiB
Python

# -*- coding: UTF-8 -*-
import api
import config
import logging
import json
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
def main():
client = api.Client(config.LARK_HOST)
# Get tenant access token
try:
access_token = client.get_tenant_access_token(config.APP_ID, config.APP_SECRET)
except Exception as e:
logging.error(f"Could not get access token: {e}")
return
# Use parameters from config or environment
APP_TOKEN = config.DEFAULT_APP_TOKEN
TABLE_ID = config.BATCH_TABLE_ID
# First, let's get some record IDs to demonstrate batch retrieval
# In a real scenario, you might already have these IDs.
logging.info(f"Fetching some record IDs from App Token: {APP_TOKEN}, Table ID: {TABLE_ID}")
try:
resp = client.get_records_list(access_token, APP_TOKEN, TABLE_ID, page_size=10)
records = resp.get('items', [])
record_ids = [r['record_id'] for r in records]
if not record_ids:
logging.warning("No records found in the table to perform batch retrieval.")
return
logging.info(f"Found {len(record_ids)} record IDs. Performing batch retrieval...")
# Performance batch retrieval
batch_records = client.batch_get_records(
access_token,
APP_TOKEN,
TABLE_ID,
record_ids,
with_shared_url=True,
automatic_fields=True
)
logging.info(f"Successfully retrieved {len(batch_records)} records via batch_get.")
if batch_records:
print("\nExample Batch Retrieved Record (First item):")
print(json.dumps(batch_records[0], ensure_ascii=False, indent=2))
except Exception as e:
logging.error(f"Error during batch record retrieval: {e}")
if __name__ == "__main__":
main()