feat(yarr): restructure into workspace with separate API and CLI crates
This commit is contained in:
125
yarr-api/examples/basic_usage.rs
Normal file
125
yarr-api/examples/basic_usage.rs
Normal file
@@ -0,0 +1,125 @@
|
||||
//! Basic usage example for the yarr-api crate
|
||||
//!
|
||||
//! This example demonstrates how to use the Sonarr API client to fetch basic information.
|
||||
//!
|
||||
//! To run this example:
|
||||
//! ```bash
|
||||
//! cargo run --example basic_usage
|
||||
//! ```
|
||||
//!
|
||||
//! Make sure to set the following environment variables:
|
||||
//! - SONARR_URL: Your Sonarr instance URL (e.g., "http://localhost:8989")
|
||||
//! - SONARR_API_KEY: Your Sonarr API key
|
||||
|
||||
use yarr_api::{Result, SonarrClient};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
// Initialize tracing for better error visibility
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// Get configuration from environment variables
|
||||
let sonarr_url =
|
||||
std::env::var("SONARR_URL").unwrap_or_else(|_| "http://localhost:8989".to_string());
|
||||
let sonarr_api_key =
|
||||
std::env::var("SONARR_API_KEY").expect("SONARR_API_KEY environment variable must be set");
|
||||
|
||||
// Create the API client
|
||||
let client = SonarrClient::new(sonarr_url, sonarr_api_key);
|
||||
|
||||
println!("Connecting to Sonarr...");
|
||||
|
||||
// Fetch and display system status
|
||||
match client.get_system_status().await {
|
||||
Ok(status) => {
|
||||
println!("✓ Connected to Sonarr successfully!");
|
||||
println!(" App Name: {}", status.app_name.unwrap_or_default());
|
||||
println!(" Version: {}", status.version.unwrap_or_default());
|
||||
println!(" Instance: {}", status.instance_name.unwrap_or_default());
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("✗ Failed to connect to Sonarr: {}", e);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch and display series count
|
||||
match client.get_series().await {
|
||||
Ok(series) => {
|
||||
println!("\n📺 Series Information:");
|
||||
println!(" Total series: {}", series.len());
|
||||
|
||||
let monitored_count = series.iter().filter(|s| s.monitored).count();
|
||||
println!(" Monitored series: {}", monitored_count);
|
||||
|
||||
if !series.is_empty() {
|
||||
println!("\n🎬 Sample series:");
|
||||
for (i, show) in series.iter().take(5).enumerate() {
|
||||
let title = show.title.as_deref().unwrap_or("Unknown");
|
||||
let status = if show.monitored {
|
||||
"Monitored"
|
||||
} else {
|
||||
"Not Monitored"
|
||||
};
|
||||
println!(" {}. {} ({})", i + 1, title, status);
|
||||
}
|
||||
|
||||
if series.len() > 5 {
|
||||
println!(" ... and {} more", series.len() - 5);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("✗ Failed to fetch series: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch and display queue information
|
||||
match client.get_queue().await {
|
||||
Ok(queue) => {
|
||||
println!("\n📥 Download Queue:");
|
||||
println!(" Items in queue: {}", queue.records.len());
|
||||
|
||||
if !queue.records.is_empty() {
|
||||
for (i, item) in queue.records.iter().take(3).enumerate() {
|
||||
let title = item.title.as_deref().unwrap_or("Unknown");
|
||||
let status = &item.status;
|
||||
println!(" {}. {} ({})", i + 1, title, status);
|
||||
}
|
||||
|
||||
if queue.records.len() > 3 {
|
||||
println!(" ... and {} more", queue.records.len() - 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("✗ Failed to fetch queue: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Check health status
|
||||
match client.get_health().await {
|
||||
Ok(health) => {
|
||||
println!("\n🏥 Health Status:");
|
||||
if health.is_empty() {
|
||||
println!(" ✓ All systems healthy!");
|
||||
} else {
|
||||
println!(" ⚠️ {} health issue(s) detected:", health.len());
|
||||
for (i, issue) in health.iter().take(3).enumerate() {
|
||||
let message = issue.message.as_deref().unwrap_or("Unknown issue");
|
||||
println!(" {}. {}", i + 1, message);
|
||||
}
|
||||
|
||||
if health.len() > 3 {
|
||||
println!(" ... and {} more issues", health.len() - 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("✗ Failed to fetch health status: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
println!("\n🎉 Example completed successfully!");
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user