backstage-client 0.1.4

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
use backstage_client::{entities::Entity, BackstageClient};
use log::{error, info};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    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");

    let client = BackstageClient::new(&base_url, &token)?;

    // Example: Fetch all entities
    info!("Fetching all entities from Backstage...");
    match client.fetch_entities::<Entity>(None).await {
        Ok(entities) => {
            info!("Successfully fetched {} entities", entities.len());
            for (i, entity) in entities.iter().take(5).enumerate() {
                info!("Entity {}: {} ({})", i + 1, entity.name(), entity.kind());
            }
            if entities.len() > 5 {
                info!("... and {} more entities", entities.len() - 5);
            }
        }
        Err(e) => {
            error!("Error fetching entities: {}", e);
            return Err(e.into());
        }
    }

    // Example: Fetch only components
    info!("Fetching component entities...");
    let mut filters = std::collections::HashMap::new();
    filters.insert("kind".to_string(), "Component".to_string());

    match client.fetch_entities::<Entity>(Some(filters)).await {
        Ok(components) => {
            info!("Successfully fetched {} components", components.len());
            for component in components.iter().take(3) {
                info!(
                    "Component: {} - {}",
                    component.name(),
                    component.description().unwrap_or("No description")
                );
            }
        }
        Err(e) => {
            error!("Error fetching components: {}", e);
            return Err(e.into());
        }
    }

    Ok(())
}