backstage-client 0.1.2

A Rust client library for interacting with the Backstage Catalog API. Provides type-safe access to Backstage entities with async support, filtering, and comprehensive error handling.
Documentation
//! Example: Fetch All Entities
//!
//! This example demonstrates how to fetch all entities from a Backstage catalog.
//!
//! Usage:
//!   BACKSTAGE_URL=https://your-backstage.com BACKSTAGE_TOKEN=your-token cargo run --example fetch_all_entities

use backstage_client::{entities::Entity, BackstageClient};
use log::{error, info};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("INFO")).init();

    // Get configuration from environment variables
    let base_url =
        env::var("BACKSTAGE_URL").unwrap_or_else(|_| "https://demo.backstage.io".to_string());
    let token =
        env::var("BACKSTAGE_TOKEN").expect("BACKSTAGE_TOKEN environment variable must be set");

    info!("Connecting to Backstage at: {}", base_url);

    // Create a new Backstage client
    let client = BackstageClient::new(&base_url, &token)?;

    // Fetch all entities
    info!("Fetching all entities from Backstage...");
    match client.fetch_entities::<Entity>(None).await {
        Ok(entities) => {
            info!("Successfully fetched {} entities", entities.len());

            // Group entities by kind
            let mut entity_counts = std::collections::HashMap::new();
            for entity in &entities {
                *entity_counts.entry(entity.kind()).or_insert(0) += 1;
            }

            info!("Entity breakdown:");
            for (kind, count) in entity_counts {
                info!("  {}: {}", kind, count);
            }

            // Show first few entities as examples
            info!("\nFirst 5 entities:");
            for (i, entity) in entities.iter().take(5).enumerate() {
                info!(
                    "  {}. {} ({}) - {}",
                    i + 1,
                    entity.name(),
                    entity.kind(),
                    entity.description().unwrap_or("No description")
                );

                // Show namespace if not default
                if entity.namespace() != "default" {
                    info!("     Namespace: {}", entity.namespace());
                }

                // Show tags if available
                if let Some(tags) = entity.tags() {
                    if !tags.is_empty() {
                        info!("     Tags: {}", tags.join(", "));
                    }
                }
            }

            if entities.len() > 5 {
                info!("... and {} more entities", entities.len() - 5);
            }
        }
        Err(e) => {
            error!("Error fetching entities: {}", e);
            return Err(e.into());
        }
    }

    Ok(())
}