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
use reqwest::{blocking::Client, StatusCode, Url};

use crate::notifications::{
    requests::Request, responses::create_message::CreateMessageResponse, Error,
};

pub fn send_create_message(
    url: Url,
    notification: &Request,
) -> Result<CreateMessageResponse, Error> {
    let client = Client::new();
    let response = client
        .post(url)
        .body(serde_json::to_string(&notification)?)
        .send()?;
    match response.status() {
        StatusCode::OK => {}
        StatusCode::BAD_REQUEST => {
            return Err(Error::BadDataSent {
                request: serde_json::to_string_pretty(&notification)?,
            })
        }
        _ => {
            return Err(Error::UnsuccessfulRequest {
                response: response.text()?,
            })
        }
    };
    let response_message = response.json()?;
    Ok(response_message)
}