use serde::Deserialize;
use serde_json::{json, Value};
use crate::error::Result;
use crate::api_client::CxClient;
use crate::serde_helpers::string_or_number;
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Webhook {
#[serde(default, deserialize_with = "string_or_number")]
pub id: Option<String>,
pub name: Option<String>,
#[serde(rename = "type")]
pub webhook_type: Option<String>,
pub url: Option<String>,
pub created_at: Option<String>,
}
impl Webhook {
pub fn display_name(&self) -> &str {
self.name.as_deref().unwrap_or("-")
}
pub fn display_type(&self) -> &str {
self.webhook_type.as_deref().unwrap_or("-")
}
pub fn display_url(&self) -> &str {
self.url.as_deref().unwrap_or("-")
}
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ListWebhooksResponse {
#[serde(default)]
pub deployed: Vec<Webhook>,
}
#[derive(Debug, Deserialize)]
pub struct DeleteWebhookResponse {}
pub fn update_request_body(id: &str, body: &Value) -> Value {
let mut out = if body.get("data").is_some() {
body.clone()
} else {
json!({ "data": body })
};
out["id"] = json!(id);
out
}
const WEBHOOKS_BASE: &str = "/mgmt/openapi/5/integrations/webhooks/v1";
const WEBHOOK_TYPES_BASE: &str = "/mgmt/openapi/5/integrations/webhook-types/v1";
pub struct WebhooksApi<'a> {
client: &'a CxClient,
}
impl<'a> WebhooksApi<'a> {
pub fn new(client: &'a CxClient) -> Self {
Self { client }
}
pub async fn list_all(&self) -> Result<ListWebhooksResponse> {
self.client.get(WEBHOOKS_BASE, &[]).await
}
pub async fn get(&self, id: &str) -> Result<Value> {
let path = format!("{WEBHOOKS_BASE}/{id}");
self.client.get(&path, &[]).await
}
pub async fn create(&self, body: &Value) -> Result<Value> {
self.client.post(WEBHOOKS_BASE, body).await
}
pub async fn update(&self, id: &str, body: &Value) -> Result<Value> {
self.client
.put(WEBHOOKS_BASE, &update_request_body(id, body))
.await
}
pub async fn delete(&self, id: &str) -> Result<DeleteWebhookResponse> {
let path = format!("{WEBHOOKS_BASE}/{id}");
self.client.delete(&path).await
}
pub async fn test(&self, id: &str) -> Result<Value> {
let path = format!("{WEBHOOKS_BASE}/{id}/test");
self.client.get(&path, &[]).await
}
pub async fn list_types(&self) -> Result<Value> {
self.client.get(WEBHOOK_TYPES_BASE, &[]).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn deserialize_list_response() {
let json = json!({
"deployed": [
{ "id": "wh-001", "name": "Slack Notify", "type": "slack", "url": "https://hooks.slack.com/..." }
]
});
let resp: ListWebhooksResponse = serde_json::from_value(json).unwrap();
assert_eq!(resp.deployed.len(), 1);
assert_eq!(resp.deployed[0].display_name(), "Slack Notify");
assert_eq!(resp.deployed[0].display_type(), "slack");
}
#[test]
fn deserialize_empty_list() {
let json = json!({ "deployed": [] });
let resp: ListWebhooksResponse = serde_json::from_value(json).unwrap();
assert!(resp.deployed.is_empty());
}
#[test]
fn update_body_wraps_bare_data_and_sets_id() {
let bare = json!({ "name": "n", "type": "GENERIC", "url": "https://example.com" });
let out = update_request_body("wh-1", &bare);
assert_eq!(out["id"], "wh-1");
assert_eq!(out["data"], bare);
}
#[test]
fn update_body_keeps_envelope_and_overrides_id() {
let wrapped = json!({ "id": "stale", "data": { "name": "n" } });
let out = update_request_body("wh-2", &wrapped);
assert_eq!(out["id"], "wh-2");
assert_eq!(out["data"]["name"], "n");
}
#[test]
fn display_missing_fields() {
let w = Webhook {
id: None,
name: None,
webhook_type: None,
url: None,
created_at: None,
};
assert_eq!(w.display_name(), "-");
assert_eq!(w.display_type(), "-");
assert_eq!(w.display_url(), "-");
}
}