|
| 1 | +""" |
| 2 | +This script connects to an eLabFTW instance via the REST API to retrieve and list all available resource categories (also called item types). |
| 3 | +For each category, it prints the category ID and title, followed by the number of entries (items) that belong to that category. |
| 4 | +""" |
| 5 | + |
| 6 | +import elabapi_python |
| 7 | +# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects |
| 8 | +from client import api_client |
| 9 | + |
| 10 | +# Create API instances for fetching categories (ItemsTypes) and items (Entries) for each category |
| 11 | +items_api = elabapi_python.ItemsTypesApi(api_client) |
| 12 | +items_api2 = elabapi_python.ItemsApi(api_client) |
| 13 | + |
| 14 | +try: |
| 15 | + # Fetch the categories (ItemsTypes) from the API |
| 16 | + item_types = items_api.read_items_types() # Fetch all categories |
| 17 | + |
| 18 | + # Print the number of categories |
| 19 | + print(f"Number of categories: {len(item_types)}") |
| 20 | + |
| 21 | + # Iterate through each category and display the ID and Title |
| 22 | + for item_type in item_types: |
| 23 | + print(f"ID: {item_type.id}, Title: {item_type.title}") |
| 24 | + |
| 25 | + # Fetch the items (entries) for each category by ID |
| 26 | + entries = items_api2.read_items(cat=item_type.id, limit=100) # Get the items for the category |
| 27 | + entry_count = len(entries) # Count the number of items |
| 28 | + |
| 29 | + # Display the number of items in the current category |
| 30 | + print(f"Number of entries in this category: {entry_count}\n") |
| 31 | + |
| 32 | +# Error handling for API requests |
| 33 | +except elabapi_python.rest.ApiException as e: |
| 34 | + print(f"Error fetching categories or entries: {e}") |
0 commit comments