use crate::client::BigRag;
use crate::core::urlencode;
use crate::error::BigRagError;
use crate::types::common::{PaginationOptions, StatusResponse};
use crate::types::webhooks::{
CreateWebhookBody, CreateWebhookResponse, UpdateWebhookBody, Webhook,
WebhookDeliveryListResponse, WebhookListResponse, WebhookTestResponse,
};
pub struct Webhooks<'a> {
pub(crate) client: &'a BigRag,
}
impl Webhooks<'_> {
pub async fn create(
&self,
body: CreateWebhookBody,
) -> Result<CreateWebhookResponse, BigRagError> {
self.client
.transport
.post("/v1/admin/webhooks", &body)
.await
}
pub async fn list(&self) -> Result<WebhookListResponse, BigRagError> {
self.client
.transport
.get("/v1/admin/webhooks", vec![])
.await
}
pub async fn get(&self, id: &str) -> Result<Webhook, BigRagError> {
let path = format!("/v1/admin/webhooks/{}", urlencode(id));
self.client.transport.get(&path, vec![]).await
}
pub async fn update(
&self,
id: &str,
body: UpdateWebhookBody,
) -> Result<Webhook, BigRagError> {
let path = format!("/v1/admin/webhooks/{}", urlencode(id));
self.client.transport.put(&path, &body).await
}
pub async fn delete(&self, id: &str) -> Result<StatusResponse, BigRagError> {
let path = format!("/v1/admin/webhooks/{}", urlencode(id));
self.client.transport.delete(&path).await
}
pub async fn list_deliveries(
&self,
id: &str,
options: Option<PaginationOptions>,
) -> Result<WebhookDeliveryListResponse, BigRagError> {
let mut query = Vec::new();
if let Some(opts) = options {
if let Some(limit) = opts.limit {
query.push(("limit".into(), limit.to_string()));
}
if let Some(offset) = opts.offset {
query.push(("offset".into(), offset.to_string()));
}
}
let path = format!("/v1/admin/webhooks/{}/deliveries", urlencode(id));
self.client.transport.get(&path, query).await
}
pub async fn test(&self, id: &str) -> Result<WebhookTestResponse, BigRagError> {
let path = format!("/v1/admin/webhooks/{}/test", urlencode(id));
self.client
.transport
.post(&path, &serde_json::Value::Null)
.await
}
}