flawless_slack/http_client/
mod.rs

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
68
69
70
71
72
73
74
75
use std::string::FromUtf8Error;

#[cfg(all(feature = "flawless", target_arch = "wasm32"))]
pub mod flawless_http_client;
#[cfg(all(feature = "reqwest", not(target_arch = "wasm32")))]
pub mod reqwest_http_client;

/// Represents errors that can occur during HTTP client operations.
#[derive(Debug)]
pub enum HttpClientError {
    /// Error related to JSON processing.
    JsonError,
    /// Error related to HTTP communication.
    HttpError,
    /// Error related to UTF-8 decoding.
    FromUtf8Error,
    /// Error related to an invalid URI.
    InvalidUri,
    /// Generic error with a descriptive string.
    Error(String),
}

impl From<serde_json::Error> for HttpClientError {
    fn from(_: serde_json::Error) -> Self {
        HttpClientError::JsonError
    }
}

impl From<FromUtf8Error> for HttpClientError {
    fn from(_: FromUtf8Error) -> Self {
        HttpClientError::FromUtf8Error
    }
}

impl From<String> for HttpClientError {
    fn from(err: String) -> Self {
        HttpClientError::Error(err)
    }
}

/// A trait defining the interface for an HTTP client.
pub trait HttpClient {
    /// Sends an HTTP POST request.
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to which the POST request will be sent.
    /// * `headers` - A slice of tuples representing HTTP headers.
    /// * `payload` - The payload of the POST request.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the response as a `String` on success,
    /// or an error of type `HttpClientError` on failure.
    fn post(&self, url: &str, headers: &[(&str, &str)], payload: &str) -> Result<String, HttpClientError>;

    /// Sends an HTTP GET request.
    ///
    /// # Arguments
    ///
    /// * `url` - The URL to which the GET request will be sent.
    /// * `params` - A slice of tuples representing HTTP headers.
    /// * `queries` - A slice of tuples representing URL query parameters.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing the response as a `String` on success,
    /// or an error of type `HttpClientError` on failure.
    fn get(
        &self,
        url: &str,
        params: &[(&str, &str)],
        queries: &[(&str, &str)],
    ) -> Result<String, HttpClientError>;
}