Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I928162008cb1ba02e1aa0e7aa971e8326a6a6964
71 lines
1.8 KiB
Rust
71 lines
1.8 KiB
Rust
use axum::{Json, extract::State};
|
|
use serde::Serialize;
|
|
|
|
use crate::{error::ApiError, state::AppState};
|
|
|
|
#[derive(Debug, Serialize, utoipa::ToSchema)]
|
|
pub struct WebhookInfo {
|
|
pub url: String,
|
|
pub events: Vec<String>,
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/v1/webhooks",
|
|
tag = "webhooks",
|
|
responses(
|
|
(status = 200, description = "List of configured webhooks", body = Vec<WebhookInfo>),
|
|
(status = 401, description = "Unauthorized"),
|
|
(status = 403, description = "Forbidden"),
|
|
),
|
|
security(("bearer_auth" = []))
|
|
)]
|
|
pub async fn list_webhooks(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<Vec<WebhookInfo>>, ApiError> {
|
|
let config = state.config.read().await;
|
|
let hooks: Vec<WebhookInfo> = config
|
|
.webhooks
|
|
.iter()
|
|
.map(|h| {
|
|
WebhookInfo {
|
|
url: h.url.clone(),
|
|
events: h.events.clone(),
|
|
}
|
|
})
|
|
.collect();
|
|
Ok(Json(hooks))
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/v1/webhooks/test",
|
|
tag = "webhooks",
|
|
responses(
|
|
(status = 200, description = "Test webhook sent"),
|
|
(status = 401, description = "Unauthorized"),
|
|
(status = 403, description = "Forbidden"),
|
|
),
|
|
security(("bearer_auth" = []))
|
|
)]
|
|
pub async fn test_webhook(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<serde_json::Value>, ApiError> {
|
|
let config = state.config.read().await;
|
|
let count = config.webhooks.len();
|
|
drop(config);
|
|
|
|
if let Some(ref dispatcher) = state.webhook_dispatcher {
|
|
dispatcher.dispatch(pinakes_core::webhooks::WebhookEvent::Test);
|
|
Ok(Json(serde_json::json!({
|
|
"webhooks_configured": count,
|
|
"test_sent": true
|
|
})))
|
|
} else {
|
|
Ok(Json(serde_json::json!({
|
|
"webhooks_configured": 0,
|
|
"test_sent": false,
|
|
"message": "no webhooks configured"
|
|
})))
|
|
}
|
|
}
|