nym_credential_proxy_lib/
webhook.rs1use reqwest::header::AUTHORIZATION;
5use serde::Serialize;
6use tracing::{Instrument, Level, debug, error, instrument, span};
7use url::Url;
8use uuid::Uuid;
9
10#[derive(Debug, Clone)]
11pub struct ZkNymWebhook {
12 pub webhook_client_url: Url,
13
14 pub webhook_client_secret: String,
15}
16
17impl ZkNymWebhook {
18 fn bearer_token(&self) -> String {
19 format!("Bearer {}", self.webhook_client_secret)
20 }
21
22 #[instrument(skip_all)]
23 pub async fn try_trigger<T: Serialize + ?Sized>(&self, original_uuid: Uuid, payload: &T) {
24 let url = self.webhook_client_url.clone();
25 let span = span!(Level::DEBUG, "webhook", uuid = %original_uuid, url = %url);
26
27 async move {
28 debug!("🕸️ about to trigger the webhook");
29
30 match reqwest::Client::new()
31 .post(url)
32 .header(AUTHORIZATION, self.bearer_token())
33 .json(payload)
34 .send()
35 .await
36 {
37 Ok(res) => {
38 if !res.status().is_success() {
39 error!("❌🕸️ failed to call webhook: {res:?}");
40 } else {
41 debug!("✅🕸️ webhook triggered successfully: {res:?}");
42 if let Ok(body) = res.text().await {
43 debug!("body = {body}");
44 }
45 }
46 }
47 Err(err) => {
48 error!("failed to call webhook: {err}")
49 }
50 }
51 }
52 .instrument(span)
53 .await
54 }
55}