flawless_slack/http_client/
mod.rs

1use std::string::FromUtf8Error;
2
3#[cfg(all(feature = "flawless", target_arch = "wasm32"))]
4pub mod flawless_http_client;
5#[cfg(all(feature = "reqwest", not(target_arch = "wasm32")))]
6pub mod reqwest_http_client;
7
8/// Represents errors that can occur during HTTP client operations.
9#[derive(Debug)]
10pub enum HttpClientError {
11    /// Error related to JSON processing.
12    JsonError,
13    /// Error related to HTTP communication.
14    HttpError,
15    /// Error related to UTF-8 decoding.
16    FromUtf8Error,
17    /// Error related to an invalid URI.
18    InvalidUri,
19    /// Generic error with a descriptive string.
20    Error(String),
21}
22
23impl From<serde_json::Error> for HttpClientError {
24    fn from(_: serde_json::Error) -> Self {
25        HttpClientError::JsonError
26    }
27}
28
29impl From<FromUtf8Error> for HttpClientError {
30    fn from(_: FromUtf8Error) -> Self {
31        HttpClientError::FromUtf8Error
32    }
33}
34
35impl From<String> for HttpClientError {
36    fn from(err: String) -> Self {
37        HttpClientError::Error(err)
38    }
39}
40
41/// A trait defining the interface for an HTTP client.
42pub trait HttpClient {
43    /// Sends an HTTP POST request.
44    ///
45    /// # Arguments
46    ///
47    /// * `url` - The URL to which the POST request will be sent.
48    /// * `headers` - A slice of tuples representing HTTP headers.
49    /// * `payload` - The payload of the POST request.
50    ///
51    /// # Returns
52    ///
53    /// Returns a `Result` containing the response as a `String` on success,
54    /// or an error of type `HttpClientError` on failure.
55    fn post(&self, url: &str, headers: &[(&str, &str)], payload: &str) -> Result<String, HttpClientError>;
56
57    /// Sends an HTTP GET request.
58    ///
59    /// # Arguments
60    ///
61    /// * `url` - The URL to which the GET request will be sent.
62    /// * `params` - A slice of tuples representing HTTP headers.
63    /// * `queries` - A slice of tuples representing URL query parameters.
64    ///
65    /// # Returns
66    ///
67    /// Returns a `Result` containing the response as a `String` on success,
68    /// or an error of type `HttpClientError` on failure.
69    fn get(
70        &self,
71        url: &str,
72        params: &[(&str, &str)],
73        queries: &[(&str, &str)],
74    ) -> Result<String, HttpClientError>;
75}