Files
nym/common/credential-proxy/src/webhook.rs
T
Jędrzej Stuczyński d3cdaf373b Feature/credential proxy crate (#6018)
* moved storage and deposits buffer to the common lib

* move more of the state into the shared lib

* extracted the rest of the features into the shared lib

* fixed test imports

* clippy
2025-09-10 09:28:38 +01:00

56 lines
1.7 KiB
Rust

// Copyright 2025 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use reqwest::header::AUTHORIZATION;
use serde::Serialize;
use tracing::{debug, error, instrument, span, Instrument, Level};
use url::Url;
use uuid::Uuid;
#[derive(Debug, Clone)]
pub struct ZkNymWebhook {
pub webhook_client_url: Url,
pub webhook_client_secret: String,
}
impl ZkNymWebhook {
fn bearer_token(&self) -> String {
format!("Bearer {}", self.webhook_client_secret)
}
#[instrument(skip_all)]
pub async fn try_trigger<T: Serialize + ?Sized>(&self, original_uuid: Uuid, payload: &T) {
let url = self.webhook_client_url.clone();
let span = span!(Level::DEBUG, "webhook", uuid = %original_uuid, url = %url);
async move {
debug!("🕸️ about to trigger the webhook");
match reqwest::Client::new()
.post(url)
.header(AUTHORIZATION, self.bearer_token())
.json(payload)
.send()
.await
{
Ok(res) => {
if !res.status().is_success() {
error!("❌🕸️ failed to call webhook: {res:?}");
} else {
debug!("✅🕸️ webhook triggered successfully: {res:?}");
if let Ok(body) = res.text().await {
debug!("body = {body}");
}
}
}
Err(err) => {
error!("failed to call webhook: {err}")
}
}
}
.instrument(span)
.await
}
}