use std::collections::HashMap;
use std::sync::Arc;
use autumn_web::actuator::{HealthCheckOutput, HealthIndicator, HealthStatus};
use crate::client::ShopifyClient;
#[derive(Debug, Clone)]
pub struct ShopifyHealth {
client: ShopifyClient,
}
impl ShopifyHealth {
#[must_use]
pub const fn new(client: ShopifyClient) -> Self {
Self { client }
}
}
impl HealthIndicator for ShopifyHealth {
fn check(&self) -> futures::future::BoxFuture<'_, HealthCheckOutput> {
Box::pin(async move {
let (status, key, value) = match self.client.shop_info().await {
Ok(info) => (HealthStatus::Up, "shop", info.name),
Err(err) => (HealthStatus::Down, "error", err.to_string()),
};
let mut details = HashMap::new();
details.insert(key.to_owned(), serde_json::Value::String(value));
HealthCheckOutput { status, details }
})
}
}
#[must_use]
pub fn indicator(client: &ShopifyClient) -> Arc<ShopifyHealth> {
Arc::new(ShopifyHealth::new(client.clone()))
}
#[cfg(test)]
mod tests;