1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use super::call_method;
use crate::{
    bot::InnerBot, errors, types::parameters::AllowedUpdates, Multipart,
};
use std::{net::IpAddr, num::NonZeroU32};

/// This method isn't meant to be used by users directly.
#[derive(Debug, Clone)]
#[must_use]
pub struct SetWebhook<'a> {
    bot: &'a InnerBot,
    url: &'a str,
    ip_address: Option<IpAddr>,
    certificate: Option<&'a str>,
    max_connections: Option<NonZeroU32>,
    allowed_updates: Option<AllowedUpdates>,
    drop_pending_updates: bool,
}

impl<'a> SetWebhook<'a> {
    pub(crate) const fn new(
        bot: &'a InnerBot,
        url: &'a str,
        ip_address: Option<IpAddr>,
        certificate: Option<&'a str>,
        max_connections: Option<NonZeroU32>,
        allowed_updates: Option<AllowedUpdates>,
        drop_pending_updates: bool,
    ) -> Self {
        Self {
            bot,
            url,
            ip_address,
            certificate,
            max_connections,
            allowed_updates,
            drop_pending_updates,
        }
    }
}

impl SetWebhook<'_> {
    /// Calls the method.
    pub async fn call(self) -> Result<(), errors::MethodCall> {
        let mut multipart = Multipart::new(5)
            .str("url", self.url)
            .maybe_string("ip_address", self.ip_address)
            .maybe_string("max_connections", self.max_connections)
            .maybe_json("allowed_updates", self.allowed_updates)
            .string("drop_pending_updates", &self.drop_pending_updates);

        if let Some(certificate) = self.certificate {
            multipart = multipart.file(
                "certificate",
                "certificate.pem",
                certificate.as_bytes(),
            );
        }

        let (boundary, body) = multipart.finish();

        call_method::<bool>(self.bot, "setWebhook", Some(boundary), body)
            .await?;

        Ok(())
    }
}