wme-cli 0.1.3

CLI tool for the Wikimedia Enterprise API
use anyhow::Result;
use tracing::info;

use crate::client::map_client_error;
use crate::commands::GlobalOpts;
use crate::output::OutputHandler;
use crate::util::build_params;

/// Get a single article
pub async fn get(name: &str, opts: &GlobalOpts) -> Result<()> {
    info!("Fetching article: {}", name);

    let client = opts.build_client().await?;

    let params = build_params(opts)?;
    let articles = client
        .on_demand()
        .get_article(name, params.as_ref())
        .await
        .map_err(map_client_error)?;

    let output = OutputHandler::new(opts.output);

    if articles.is_empty() {
        println!("Article '{}' not found.", name);
    } else {
        let json_value = serde_json::to_value(&articles)?;
        output.output(&json_value)?;
    }

    Ok(())
}

/// Get structured article data (BETA)
pub async fn structured(name: &str, opts: &GlobalOpts) -> Result<()> {
    info!("Fetching structured article: {}", name);
    println!("Note: Structured article API is in BETA");

    let client = opts.build_client().await?;

    let params = build_params(opts)?;
    let articles = client
        .on_demand()
        .get_structured_article(name, params.as_ref())
        .await
        .map_err(map_client_error)?;

    let output = OutputHandler::new(opts.output);

    if articles.is_empty() {
        println!("Article '{}' not found.", name);
    } else {
        let json_value = serde_json::to_value(&articles)?;
        output.output(&json_value)?;
    }

    Ok(())
}