slack_hook/
blocking.rs

1use crate::{Error, Payload, Result};
2
3use reqwest::{blocking::Client, Url};
4
5/// Handles sending messages to slack
6#[derive(Debug, Clone)]
7pub struct Slack {
8    hook: Url,
9    client: Client,
10}
11
12impl Slack {
13    /// Construct a new instance of slack for a specific incoming url endpoint.
14    pub fn new<T: reqwest::IntoUrl>(hook: T) -> Result<Slack> {
15        Self::new_with_client(hook, Client::new())
16    }
17
18    /// The same as [`Slack::new()`], but with a custom [`reqwest::Client`]
19    ///
20    /// This allows for configuring custom proxies, DNS resolvers, etc.
21    pub fn new_with_client<T: reqwest::IntoUrl>(hook: T, client: Client) -> Result<Self> {
22        let hook = hook.into_url()?;
23        Ok(Self { hook, client })
24    }
25
26    /// Send payload to slack service
27    pub fn send(&self, payload: &Payload) -> Result<()> {
28        let response = self.client.post(self.hook.clone()).json(payload).send()?;
29
30        if response.status().is_success() {
31            Ok(())
32        } else {
33            Err(Error::Slack(format!("HTTP error {}", response.status())))
34        }
35    }
36}