hen 0.20.1

Run protocol-aware API request collections from the command line or through MCP.
Documentation
use log::debug;

use crate::{
    error::{HenError, HenErrorKind, HenResult},
    parser,
    request::{OAuthProfile, Request},
    schema::SchemaRegistry,
};
use std::path::PathBuf;

#[derive(Debug)]
pub struct Collection {
    pub name: String,
    pub description: String,
    pub available_environments: Vec<String>,
    pub selected_environment: Option<String>,
    pub oauth_profiles: std::collections::HashMap<String, OAuthProfile>,
    pub schema_registry: SchemaRegistry,
    pub requests: Vec<Request>,
}

// import the collection file, parse it, and return a Collection struct.
impl Collection {
    pub fn new(collection_path: PathBuf) -> HenResult<Self> {
        Self::with_environment(collection_path, None)
    }

    pub fn with_environment(
        collection_path: PathBuf,
        selected_environment: Option<&str>,
    ) -> HenResult<Self> {
        let working_dir = collection_path
            .parent()
            .map(|path| path.to_path_buf())
            .unwrap_or_else(|| PathBuf::from(""));

        let working_dir = if working_dir.as_os_str().is_empty() {
            std::env::current_dir().map_err(|err| {
                HenError::new(HenErrorKind::Io, "Failed to determine current directory")
                    .with_detail(err.to_string())
            })?
        } else {
            working_dir
        };

        debug!("Working dir: {:?}", working_dir);

        let input = std::fs::read_to_string(&collection_path).map_err(|err| {
            HenError::new(
                HenErrorKind::Io,
                format!("Failed to read collection {}", collection_path.display()),
            )
            .with_detail(err.to_string())
        })?;

        parser::parse_collection_with_environment(
            input.as_str(),
            working_dir.clone(),
            selected_environment,
        )
            .map_err(|err| {
                parser::parse_error_to_hen_error(
                    input.as_str(),
                    &working_dir,
                    err,
                    Some(collection_path.as_path()),
                )
            })
            .map_err(|err| {
                err.with_detail(format!(
                    "While parsing collection file {}",
                    collection_path.display()
                ))
            })
    }
}