alchemy_api/api/notify/webhook_addresses/
update.rs

1use crate::cores::endpoint::Endpoint;
2use derive_builder::Builder;
3use ethers_core::types::Address;
4use http::Method;
5use serde::{Deserialize, Serialize};
6
7/// Add or remove addresses from a specific webhook.
8///
9/// This webhook endpoint is idempotent, meaning that identical requests can be made once or
10/// several times in a row with the same effect.
11#[derive(Default, Debug, Builder, Clone, Serialize, Deserialize)]
12#[builder(default, setter(into), build_fn(validate = "Self::validate"))]
13pub struct UpdateWebhookAddresses {
14    /// ID of the address activity webhook.
15    #[builder(setter)]
16    webhook_id: String,
17    /// List of addresses to add, use [] if none.
18    addresses_to_add: Vec<Address>,
19    /// List of addresses to remove, use [] if none.
20    addresses_to_remove: Vec<Address>,
21}
22
23impl UpdateWebhookAddresses {
24    /// Create a builder for the endpoint.
25    pub fn builder() -> UpdateWebhookAddressesBuilder {
26        UpdateWebhookAddressesBuilder::default()
27    }
28}
29
30impl UpdateWebhookAddressesBuilder {
31    /// Pre-Build Validation.
32    fn validate(&self) -> Result<(), String> {
33        // Return Err if neither `addresses_to_add` and `addresses_to_remove` are empty.
34        match (&self.addresses_to_add, &self.addresses_to_remove) {
35            (Some(adds), Some(removes)) if !adds.is_empty() || !removes.is_empty() => Ok(()),
36            (Some(adds), _) if !adds.is_empty() => Ok(()),
37            (_, Some(removes)) if !removes.is_empty() => Ok(()),
38            _ => Err("you must provide at least one address".to_string()),
39        }
40    }
41}
42
43impl Endpoint for UpdateWebhookAddresses {
44    fn method(&self) -> http::Method {
45        Method::PATCH
46    }
47
48    fn endpoint(&self) -> std::borrow::Cow<'static, str> {
49        "api/update-webhook-addresses".into()
50    }
51
52    fn body(&self) -> anyhow::Result<Option<(&'static str, Vec<u8>)>> {
53        let body = serde_json::to_vec(self)?;
54        Ok(Some(("application/json", body)))
55    }
56}