webhookloader 0.1.6

Trigger HTTP webhooks by name from a TOML config. Small async library with retries and automatic Content-Type detection.
Documentation
pub mod webhook;

use std::path::Path;

pub use webhook::webhook::{WebHookError, WebHookLoader};

/// Send a webhook by name using the default config path.
///
/// Resolution order for config path:
/// 1) WEBHOOKLOADER_CONFIG env var, if set
/// 2) "config.toml" in the current working directory
pub async fn send_webhook_by_name(name: &str) -> Result<(), WebHookError> {
    let mut config_path = "config.toml".to_string();
    if let Ok(config) = std::env::var("WEBHOOKLOADER_CONFIG") {
        if Path::new(config.as_str()).exists() {
            config_path = config;
        }
    }

    if Path::new("/kmmq/webhookloader.toml").exists() {
        config_path = "/kmmq/webhookloader.toml".to_string();
    }

    if Path::new("./webhookloader.toml").exists() {
        config_path = "./webhookloader.toml".to_string();
    }

    if Path::new("consumers/webhooks/webhookloader.toml").exists() {
        config_path = "consumers/webhooks/webhookloader.toml".to_string();
    }



    let loader = WebHookLoader::new(&config_path)?;
    loader.fire_hook_result(name).await
}

/// Send a webhook by name using an explicit config path.
pub async fn send_webhook_by_name_with_config(config_path: &str, name: &str) -> Result<(), WebHookError> {
    let loader = WebHookLoader::new(config_path)?;
    loader.fire_hook_result(name).await
}
pub async fn send_webhook_by_name_with_config_and_body(config_path: &str, name: &str, body: &str) -> Result<(), WebHookError> {
    let mut loader = WebHookLoader::new(config_path)?;
    if !body.is_empty() {
        if let Some(value) = loader.hook_map.get(name).cloned() {
            // Preserve the configured URL and override the body provided by caller
            let mut parts = value.splitn(2, ":::");
            let url = parts.next().unwrap_or("").trim().to_string();
            let new_value = format!("{}:::{}", url, body);
            loader.hook_map.insert(name.to_string(), new_value);
        }
    }
    loader.fire_hook_result(name).await
}