How do I gather Entities in Python?


It is common to want to get all Entity records in Python for processing or analysis.

Here is a simple example that lets you configure an Entity Type ID, and then fetch all records for that Entity Type.


You will want to install our Python library to facilitate this process.

pip install bruce-models

Python
import bruce_models as BM

ENTITY_TYPE_ID = "YOUR_ENTITY_TYPE_ID"

# Setup the API instance to communicate with your account.
bruce = BM.BruceApi({
    "account_id": "YOUR_ACCOUNT_ID"
})

def fetch_entities(bruce, entity_type_id):
    """
    Fetches all Entities for a given Entity Type ID in batches.
    """
    page_index = 0
    page_size = 1000

    # List to collect all Entities.
    all_entities = []

    has_more_data = True
    while has_more_data:
        try:
            # Get a batch of Entities.
            entities = BM.Entity.get_list(
                bruce,
                entity_type_id=entity_type_id,
                page_index=page_index,
                page_size=page_size
            ).get("Items")

            if not entities:
                print("No more Entities found.")
                has_more_data = False
            else:
                # Add fetched Entities to the list.
                all_entities.extend(entities)
                print(f"Fetched {len(entities)} Entities from page {page_index}.")
                page_index += 1

        # Depending on the error, you may want to retry.
        # Eg: Connectivity issues.
        except Exception as e:
            print("Failed to fetch data.")
            print(e)
            has_more_data = False

    return all_entities

entities = fetch_entities(bruce, ENTITY_TYPE_ID)
print(f"Total Entities collected: {len(entities)}")