pling/
webhook.rs

1use url::Url;
2
3pub struct Webhook {
4    pub webhook: Url,
5}
6
7impl Webhook {
8    /// Send a Webhook via [`ureq`] to the given URL.
9    ///
10    /// # Errors
11    ///
12    /// This method errors when the request could not be send or when the target server returns a not successful status.
13    #[allow(clippy::result_large_err)]
14    #[cfg(feature = "ureq")]
15    pub fn send_ureq(&self, body: &str) -> Result<(), ureq::Error> {
16        ureq::post(self.webhook.as_str())
17            .set("User-Agent", crate::USER_AGENT)
18            .send_string(body)?;
19        Ok(())
20    }
21
22    /// Send a Webhook via [`reqwest`] to the given URL.
23    ///
24    /// # Errors
25    ///
26    /// This method errors when the request could not be send or when the target server returns a not successful status.
27    #[cfg(feature = "reqwest")]
28    pub async fn send_reqwest(&self, body: &str) -> reqwest::Result<()> {
29        reqwest::ClientBuilder::new()
30            .user_agent(crate::USER_AGENT)
31            .build()?
32            .post(self.webhook.clone())
33            .body(body.to_owned())
34            .send()
35            .await
36            .and_then(reqwest::Response::error_for_status)
37            .map_err(reqwest::Error::without_url)?;
38        Ok(())
39    }
40}