Skip to main content

kellnr_webhooks/
types.rs

1use kellnr_common::webhook::{Webhook, WebhookEvent};
2use serde::{Deserialize, Serialize};
3use thiserror::Error;
4use utoipa::ToSchema;
5
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
7pub struct RegisterWebhookRequest {
8    // `type` alias included for webhook standards compatibility
9    #[serde(alias = "type")]
10    pub event: WebhookEvent,
11    pub callback_url: String,
12    pub name: Option<String>,
13    /// Optional secret to sign deliveries according to the Standard Webhooks
14    /// specification. Base64 encoded, optionally prefixed with `whsec_`, at
15    /// least 24 bytes. The secret is never returned by the API.
16    #[serde(default)]
17    #[schema(write_only, example = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw")]
18    pub secret: Option<String>,
19}
20
21#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
22pub struct RegisterWebhookResponse {
23    pub id: String,
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
27pub struct GetWebhookResponse {
28    pub id: String,
29    pub event: WebhookEvent,
30    pub callback_url: String,
31    pub name: Option<String>,
32    /// Whether deliveries are signed. The secret itself is never returned.
33    #[serde(default)]
34    pub has_secret: bool,
35}
36
37impl From<Webhook> for GetWebhookResponse {
38    fn from(w: Webhook) -> Self {
39        Self {
40            id: w.id.unwrap_or_default(),
41            event: w.event,
42            callback_url: w.callback_url,
43            name: w.name,
44            has_secret: w.secret.is_some(),
45        }
46    }
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ToSchema)]
50pub struct GetAllWebhooksResponse(pub Vec<GetWebhookResponse>);
51
52#[derive(Error, Debug)]
53pub enum WebhookError {
54    #[error("Database error: {0}")]
55    DatabaseError(#[from] kellnr_db::error::DbError),
56}