1use crate::{Error, Payload, Result};
2
3use reqwest::{blocking::Client, Url};
4
5#[derive(Debug, Clone)]
7pub struct Slack {
8 hook: Url,
9 client: Client,
10}
11
12impl Slack {
13 pub fn new<T: reqwest::IntoUrl>(hook: T) -> Result<Slack> {
15 Self::new_with_client(hook, Client::new())
16 }
17
18 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 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}