use crate::configuration::{ApiEventSchema, Settings};
use crate::telemetry::{SERVICE_NAME, SERVICE_VERSION};
use actix_web::{HttpResponse, web};
use serde_json::json;
use std::collections::HashMap;
use tracing::info;
#[utoipa::path(get, path = "/api/v1/schema", tag = "schema")]
#[tracing::instrument]
pub async fn get_notification_schema() -> HttpResponse {
let schema = Settings::get_global_notification_schema();
match schema {
Some(schema_map) => {
let api_schema: HashMap<String, ApiEventSchema> = schema_map
.iter()
.map(|(key, value)| (key.clone(), ApiEventSchema::from(value)))
.collect();
info!(
service_name = SERVICE_NAME,
service_version = SERVICE_VERSION,
event_name = "api.schema.list.succeeded",
schema_count = api_schema.len(),
event_types = ?api_schema.keys().collect::<Vec<_>>(),
"Returning filtered notification schema configuration"
);
HttpResponse::Ok().json(json!({
"status": "success",
"schema": api_schema,
"event_types": api_schema.keys().collect::<Vec<_>>(),
"total_schemas": api_schema.len()
}))
}
None => {
info!(
service_name = SERVICE_NAME,
service_version = SERVICE_VERSION,
event_name = "api.schema.list.empty",
"No notification schema configured, returning empty schema"
);
HttpResponse::Ok().json(json!({
"status": "success",
"schema": {},
"event_types": [],
"total_schemas": 0,
"message": "No notification schema configured"
}))
}
}
}
#[utoipa::path(
get,
path = "/api/v1/schema/{event_type}",
tag = "schema",
params(
("event_type" = String, Path, description = "Event type identifier")
),
)]
#[tracing::instrument]
pub async fn get_event_schema(path: web::Path<String>) -> HttpResponse {
let event_type = path.into_inner();
let schema = Settings::get_global_notification_schema();
match schema {
Some(schema_map) => {
if let Some(event_schema) = schema_map.get(&event_type) {
let api_schema = ApiEventSchema::from(event_schema);
info!(
service_name = SERVICE_NAME,
service_version = SERVICE_VERSION,
event_name = "api.schema.get.succeeded",
event_type = %event_type,
field_count = api_schema.identifier.len(),
"Returning filtered schema for specific event type"
);
HttpResponse::Ok().json(json!({
"status": "success",
"event_type": event_type,
"schema": api_schema
}))
} else {
HttpResponse::NotFound().json(json!({
"status": "error",
"message": format!("Event type '{}' not found", event_type),
"available_types": schema_map.keys().collect::<Vec<_>>()
}))
}
}
None => HttpResponse::NotFound().json(json!({
"status": "error",
"message": "No notification schema configured"
})),
}
}