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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
pub mod api;
mod builder;
mod entity;
mod error;

pub use error::*;
use std::io::Read;

pub use builder::*;
pub use entity::*;
use hyper::http::request::Builder;
use hyper::{Body, Method};
use hyper_tls::HttpsConnector;
use multipart::client::lazy::Multipart;

use serde_json::{json, Value};

pub struct SlackClient {
    pub(crate) context: SlackContext,
}

impl SlackClient {
    pub fn new(token: &str) -> Self {
        Self {
            context: SlackContext {
                token: Some(token.to_string()),
            },
        }
    }
}

struct SlackContext {
    token: Option<String>,
}

pub type SlackApiResponse<T> = Result<T, SlackError>;

impl SlackClient {
    async fn http_response<R>(response: hyper::Result<hyper::Response<Body>>) -> SlackApiResponse<R>
    where
        R: for<'de> serde::Deserialize<'de>,
    {
        let response = match response {
            Ok(v) => v,
            Err(e) => return Err(SlackSystemError::new(e.to_string()).into()),
        };

        let status = response.status();
        let body = match hyper::body::to_bytes(response.into_body()).await {
            Ok(v) => v.to_vec(),
            Err(e) => {
                return Err(SlackHttpError::new(status.as_u16(), e.message().to_string()).into());
            }
        };

        let http_response_body = match String::from_utf8(body) {
            Ok(v) => v,
            Err(e) => return Err(SlackSystemError::new(e.to_string()).into()),
        };

        let value = match serde_json::from_str::<Value>(http_response_body.as_str()) {
            Ok(v) => v,
            Err(e) => return Err(SlackSystemError::new(e.to_string()).into()),
        };
        // dbg!(&value);
        //
        if let Some(ok) = value.get("ok") {
            if ok.as_bool() == Some(false) {
                return Err(SlackApiError {
                    status: status.as_u16(),
                    errors: Some(get_error_value(&value)),
                    warnings: None,
                    http_response_body: Some(http_response_body),
                }
                .into());
            }
        }

        Ok(serde_json::from_value(value).unwrap())
    }
    pub(crate) async fn http_get<P, R>(
        &self,
        token: &str,
        url: &str,
        value: &P,
    ) -> SlackApiResponse<R>
    where
        P: serde::Serialize,
        R: for<'de> serde::Deserialize<'de>,
    {
        let build = builder(url, token).method(Method::GET);
        let request = build.body(Body::from(json!(value).to_string())).unwrap();
        let client = hyper::Client::builder().build(HttpsConnector::new());

        let response = client.request(request).await;
        SlackClient::http_response(response).await
        // let body = hyper::body::to_bytes(response.into_body())
        //     .await
        //     .unwrap()
        //     .to_vec();
        // let body = String::from_utf8(body).unwrap();
        // let value = serde_json::from_str::<Value>(body.as_str());
        // dbg!(value);
        //
        // Err(SlackError::Http(SlackHttpError {
        //     status: 0,
        //     http_response_body: None,
        // }))
    }
    pub(crate) async fn http_post<P, R>(&self, url: &str, value: &P) -> SlackApiResponse<R>
    where
        P: serde::Serialize,
        R: for<'de> serde::Deserialize<'de>,
    {
        let build = builder(url, self.context.token.clone().unwrap_or_default().as_str())
            .method(Method::POST);
        // println!("hoge2 {}", serde_json::to_string(value).unwrap());
        let request = build.body(Body::from(json!(value).to_string())).unwrap();
        let response = hyper::Client::builder()
            .build(HttpsConnector::new())
            .request(request)
            .await;
        Self::http_response(response).await
    }
    pub(crate) async fn http_post_data<R>(
        &self,
        url: &str,
        multipart: Multipart<'_, '_>,
    ) -> SlackApiResponse<R>
    where
        R: for<'de> serde::Deserialize<'de>,
    {
        let mut build = builder_file(url, self.context.token.clone().unwrap_or_default().as_str())
            .method(Method::POST);
        let mut s = "".to_string();
        let mut multipart = multipart;
        let mut aa = multipart.prepare().unwrap();
        build = build.header(
            "Content-Type",
            format!("multipart/form-data; boundary={}", aa.boundary()),
        );
        aa.read_to_string(&mut s).unwrap();
        let request = build.body(Body::from(s)).unwrap();
        let response = hyper::Client::builder()
            .build(HttpsConnector::new())
            .request(request)
            .await;
        Self::http_response(response).await
    }
}

fn builder(url: &str, token: &str) -> Builder {
    let mut build = hyper::Request::builder().uri(format!("https://slack.com/api/{}", url));
    if !token.is_empty() {
        build = build.header("Authorization", format!("Bearer {}", token))
    }
    build.header("Content-type", "application/json; charset=UTF-8")
}

fn builder_file(url: &str, token: &str) -> Builder {
    let mut build = hyper::Request::builder().uri(format!("https://slack.com/api/{}", url));
    if !token.is_empty() {
        build = build.header("Authorization", format!("Bearer {}", token))
    }
    build
    // build.header(CONTENT_TYPE, "multipart/form-data")
    // build.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
}

fn get_error_value(value: &Value) -> Vec<String> {
    match value.get("error") {
        None => {
            vec![]
        }
        Some(v) => {
            if v.is_string() {
                return match v.as_str() {
                    None => {
                        vec![]
                    }
                    Some(v) => {
                        vec![v.to_string()]
                    }
                };
            }
            vec![]
        }
    }
}