uarp-sdk 0.5.7

Async Rust client for the UARP (Snaga) Universal Agent Runtime Platform API
Documentation
// Code generated by @uarp/codegen from spec/openapi.json. DO NOT EDIT.
//!
//! Webhook subscriptions

#![allow(unused_imports, clippy::too_many_arguments)]

use reqwest::Method;
use serde::{Deserialize, Serialize};

use crate::client::{Client, Request, NO_BODY, NO_QUERY};
use crate::error::Result;
use crate::generated::models;
use crate::multipart::{field_text, FilePart};
use crate::util::encode_path;

/// Query and header parameters for `sensorWebhook`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SensorWebhookParams {
    /// Hex-encoded HMAC-SHA256 of the request body using the subscription's signing secret.
    #[serde(skip)]
    pub x_sensor_signature: String,
}

/// Query and header parameters for `shopifyWebhook`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ShopifyWebhookParams {
    /// Base64-encoded HMAC-SHA256 of the request body using the shop's webhook secret.
    #[serde(skip)]
    pub x_shopify_hmac_sha256: String,
    /// Event topic (e.g. `orders/create`).
    #[serde(skip)]
    pub x_shopify_topic: Option<String>,
    #[serde(skip)]
    pub x_shopify_shop_domain: Option<String>,
}

/// Webhook subscriptions
#[derive(Debug, Clone)]
pub struct WebhooksApi {
    pub(crate) client: Client,
}

impl Client {
    /// Webhook subscriptions
    pub fn webhooks(&self) -> WebhooksApi {
        WebhooksApi { client: self.clone() }
    }
}

impl WebhooksApi {
    /// Create a webhook subscription
    ///
    /// `POST /api/v1/webhooks`
    ///
    /// Required scopes: `webhooks:write`.
    pub async fn create(&self, body: &models::CreateWebhookRequest) -> Result<models::WebhookSubscription> {
        self.client
            .request_json(Request {
                method: Method::POST,
                path: "/api/v1/webhooks".to_string(),
                query: NO_QUERY,
                body: Some(body),
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Delete a webhook subscription
    ///
    /// `DELETE /api/v1/webhooks/{webhookId}`
    ///
    /// Required scopes: `webhooks:write`.
    pub async fn delete(&self, webhook_id: &str) -> Result<serde_json::Value> {
        self.client
            .request_json(Request {
                method: Method::DELETE,
                path: format!("/api/v1/webhooks/{}", encode_path(webhook_id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }

    /// Get a webhook subscription
    ///
    /// `GET /api/v1/webhooks/{webhookId}`
    ///
    /// Required scopes: `webhooks:read`.
    pub async fn get(&self, webhook_id: &str) -> Result<serde_json::Value> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/webhooks/{}", encode_path(webhook_id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// List webhook subscriptions
    ///
    /// `GET /api/v1/webhooks`
    ///
    /// Required scopes: `webhooks:read`.
    pub async fn list(&self) -> Result<models::ListWebhooksResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: "/api/v1/webhooks".to_string(),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// List webhook deliveries
    ///
    /// `GET /api/v1/webhooks/{webhookId}/deliveries`
    ///
    /// Required scopes: `webhooks:read`.
    pub async fn list_webhook_deliveries(&self, webhook_id: &str) -> Result<models::ListWebhookDeliveriesResponse> {
        self.client
            .request_json(Request {
                method: Method::GET,
                path: format!("/api/v1/webhooks/{}/deliveries", encode_path(webhook_id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: false,
            })
            .await
    }

    /// Sensor webhook (HMAC-authenticated, triggers an agent run)
    ///
    /// External-system webhook. Bypasses normal auth — `X-Sensor-Signature` header is HMAC-verified
    /// against the per-subscription secret. On valid signature, fires an agent run with the request
    /// body as input.
    ///
    /// `POST /api/v1/webhooks/sensor/{webhookId}`
    pub async fn sensor_webhook(&self, webhook_id: &str, body: &serde_json::Map<String, serde_json::Value>, params: &SensorWebhookParams) -> Result<models::SensorWebhookResponse> {
        let mut headers: Vec<(&'static str, String)> = Vec::new();
        headers.push(("X-Sensor-Signature", params.x_sensor_signature.clone()));
        self.client
            .request_json(Request {
                method: Method::POST,
                path: format!("/api/v1/webhooks/sensor/{}", encode_path(webhook_id)),
                query: NO_QUERY,
                body: Some(body),
                headers,
                idempotent: true,
            })
            .await
    }

    /// Shopify webhook
    ///
    /// Shopify webhook receiver. Bypasses normal auth — `X-Shopify-Hmac-Sha256` header is
    /// HMAC-verified against the per-shop secret.
    ///
    /// `POST /api/v1/webhooks/shopify`
    pub async fn shopify_webhook(&self, params: &ShopifyWebhookParams) -> Result<serde_json::Value> {
        let mut headers: Vec<(&'static str, String)> = Vec::new();
        headers.push(("X-Shopify-Hmac-Sha256", params.x_shopify_hmac_sha256.clone()));
        if let Some(value) = &params.x_shopify_topic {
            headers.push(("X-Shopify-Topic", value.clone()));
        }
        if let Some(value) = &params.x_shopify_shop_domain {
            headers.push(("X-Shopify-Shop-Domain", value.clone()));
        }
        self.client
            .request_json(Request {
                method: Method::POST,
                path: "/api/v1/webhooks/shopify".to_string(),
                query: NO_QUERY,
                body: NO_BODY,
                headers,
                idempotent: true,
            })
            .await
    }

    /// Send test delivery
    ///
    /// `POST /api/v1/webhooks/{webhookId}/test`
    ///
    /// Required scopes: `webhooks:write`.
    pub async fn test_webhook(&self, webhook_id: &str) -> Result<serde_json::Value> {
        self.client
            .request_json(Request {
                method: Method::POST,
                path: format!("/api/v1/webhooks/{}/test", encode_path(webhook_id)),
                query: NO_QUERY,
                body: NO_BODY,
                headers: Vec::new(),
                idempotent: true,
            })
            .await
    }
}