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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#[cfg(test)]
mod tests;

use std::{fs::File, path::Path};

use reqwest::{header::HeaderMap, multipart, Client};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::io::Read;
use std::io::Write;
use tokio::io::AsyncWriteExt;
use url::Url;

#[derive(Debug, Clone)]
pub enum ArchiveType {
    Zip,
    Gzip,
    Tar,
}
impl ArchiveType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ArchiveType::Zip => "zip",
            ArchiveType::Gzip => "gzip",
            ArchiveType::Tar => "tar",
        }
    }
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct OkJson {
    pub ok: bool,
}

#[derive(Clone, Debug)]
pub struct TlsConfig<'a> {
    pub insecure: Option<bool>,
    pub private_chain_bytes: Option<&'a [u8]>,
}

#[derive(Clone, Debug)]
pub struct HttpClient {
    base_url: Url,
    client: Client,
}

impl HttpClient {
    pub fn new(
        base_url: Url,
        tls_config: Option<TlsConfig<'static>>,
        default_headers: Option<HeaderMap>,
    ) -> Self {
        let mut builder = Client::builder();
        if let Some(headers) = default_headers {
            builder = builder.default_headers(headers);
        }
        if base_url.scheme() == "https" {
            builder = builder.use_rustls_tls();

            if let Some(tls_config) = tls_config {
                if let Some(true) = tls_config.insecure {
                    builder = builder.danger_accept_invalid_certs(true);
                } else if let Some(private_chain_bytes) = tls_config.private_chain_bytes {
                    let reqwest_certificate =
                        reqwest::Certificate::from_pem(private_chain_bytes).unwrap();

                    // print!("cert_chain: {:?}", cert_chain.as_slice());

                    builder = builder.add_root_certificate(reqwest_certificate);
                }
            }
        }

        let client = builder.build().unwrap();
        Self { base_url, client }
    }

    pub async fn get<T: DeserializeOwned>(
        &self,
        endpoint: &str,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<T> {
        let url = self.base_url.join(endpoint)?;

        let mut request_builder = self.client.get(url);

        if let Some(headers) = extra_headers {
            for (name, value) in headers.iter() {
                request_builder = request_builder.header(name, value);
            }
        }

        let resp = request_builder.send().await?;

        let response = resp.json::<T>().await?;
        Ok(response)
    }

    pub async fn post<T: DeserializeOwned, U: Serialize>(
        &self,
        endpoint: &str,
        body: &U,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<T> {
        let url = self.base_url.join(endpoint)?;

        let mut request_builder = self.client.post(url).json(body);

        if let Some(headers) = extra_headers {
            for (name, value) in headers.iter() {
                request_builder = request_builder.header(name, value);
            }
        }

        let resp = request_builder.send().await?;

        let response = resp.json::<T>().await?;
        Ok(response)
    }

    pub async fn post_file_as_zip(
        &self,
        url: Url,
        path: &Path,
        multipart_file_name: Option<String>,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        let tmp_dir = tempfile::tempdir()?;
        let zip_file_name = String::from("test.zip");
        let tmp_file_buff = tmp_dir.path().join(zip_file_name.clone());
        let tmp_file_path = Path::new(&tmp_file_buff).to_owned();
        let cloned_path = tmp_file_path.clone();
        let to_be_zipped = path.to_owned();
        tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
            let mut zip = zip::ZipWriter::new(std::fs::File::create(tmp_file_path)?);
            let options = zip::write::FileOptions::default()
                .compression_method(zip::CompressionMethod::Stored)
                .unix_permissions(0o755);
            zip.start_file(zip_file_name, options)?;
            zip.write_all(&std::fs::read(to_be_zipped)?)?;
            zip.finish()?;
            Ok(())
        })
        .await??;
        self.post_file_path(
            url,
            cloned_path.as_path(),
            multipart_file_name,
            extra_headers,
        )
        .await
    }

    pub async fn post_folder_as_zip(
        &self,
        url: Url,
        path: &Path,
        multipart_file_name: Option<String>,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        let tmp_dir = tempfile::tempdir()?;
        let tmp_file_buff = tmp_dir.path().join("test.zip");
        let tmp_file_path = Path::new(&tmp_file_buff).to_owned();
        let cloned_path = tmp_file_path.clone();
        let to_be_zipped = path.to_owned();
        tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
            let mut zip = zip::ZipWriter::new(std::fs::File::create(tmp_file_path)?);
            let options = zip::write::FileOptions::default()
                .compression_method(zip::CompressionMethod::Stored)
                .unix_permissions(0o755);
            for file in walkdir::WalkDir::new(to_be_zipped) {
                let file = file?;
                let path = file.path();
                let name = path.strip_prefix(path)?.to_str().unwrap();
                if path.is_file() {
                    zip.start_file(name, options)?;
                    zip.write_all(&std::fs::read(path)?)?;
                } else if path.is_dir() {
                    zip.add_directory(name, options)?;
                }
            }
            zip.finish()?;
            Ok(())
        })
        .await??;
        self.post_file_path(
            url,
            cloned_path.as_path(),
            multipart_file_name,
            extra_headers,
        )
        .await
    }

    pub async fn post_file_path(
        &self,
        url: Url,
        path: &Path,
        multipart_file_name: Option<String>,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        let file_name = path.file_name().unwrap().to_str().unwrap().to_string();

        let mut file = File::open(path)?;

        // Create a buffer to store the file's contents
        let mut buffer = Vec::new();

        // Read the file's contents into the buffer
        file.read_to_end(&mut buffer)?;
        let multipart = multipart_file_name;
        let headers = extra_headers;
        let url_address = url;

        let static_buffer: &'static [u8] = Box::leak(buffer.into_boxed_slice());

        self.post_file_buffer(url_address, file_name, static_buffer, multipart, headers)
            .await
    }

    pub async fn post_file_buffer(
        &self,
        url: Url,
        name: String,
        bytes: &'static [u8],
        mut multipart_file_name: Option<String>,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        if multipart_file_name.is_none() {
            multipart_file_name = Some(String::from("file"));
        }

        let multipart_file_name = multipart_file_name.unwrap();

        println!("UPLOAD {} {}", url.as_str(), &name);

        let part = multipart::Part::bytes(bytes)
            .file_name(name.clone())
            .mime_str("application/octet-stream")?;

        let form = multipart::Form::new().part(multipart_file_name, part);

        self.send_multipart_form(url, form, extra_headers).await
    }

    pub async fn send_multipart_form<T: DeserializeOwned>(
        &self,
        url: Url,
        multipart_form: multipart::Form,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<T> {
        let mut request_builder = self.client.post(url);

        if let Some(headers) = extra_headers {
            for (name, value) in headers.iter() {
                request_builder = request_builder.header(name, value);
            }
        }

        let response = request_builder
            .multipart(multipart_form)
            .send()
            .await?
            .json::<T>()
            .await?;

        Ok(response)
    }

    pub async fn get_file_buffer(
        &self,
        url: Url,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<tokio_util::bytes::Bytes> {
        // println!("GET {url}");
        let mut request_builder = self.client.get(url);

        if let Some(headers) = extra_headers {
            for (name, value) in headers.iter() {
                request_builder = request_builder.header(name, value);
            }
        }
        let resp = request_builder.send().await?;
        if resp.status().is_success() {
            let bytes = resp.bytes().await?;
            Ok(bytes)
        } else {
            Err(anyhow::anyhow!("Error downloading file"))
        }
    }

    pub async fn get_file_to_path(
        &self,
        url: Url,
        path: &Path,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        // println!("GET {url}");
        let file_buffer = self.get_file_buffer(url, extra_headers).await?;
        let mut file = tokio::fs::File::create(&path).await?;

        file.write_all(&file_buffer).await?;

        Ok(())
    }

    pub async fn get_archive_to_dir(
        &self,
        url: Url,
        archive_type: &ArchiveType,
        dir: &Path,
        extra_headers: Option<HeaderMap>,
    ) -> anyhow::Result<()> {
        let file_buffer = self.get_file_buffer(url, extra_headers).await?;

        match archive_type {
            ArchiveType::Zip => {
                let dir = dir.to_owned();
                tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
                    let mut archive = zip::ZipArchive::new(std::io::Cursor::new(file_buffer))?;
                    for i in 0..archive.len() {
                        let mut file = archive.by_index(i)?;
                        let outpath = dir.join(file.mangled_name());

                        if file.name().ends_with('/') {
                            std::fs::create_dir_all(&outpath)?;
                        } else {
                            if let Some(parent) = outpath.parent() {
                                std::fs::create_dir_all(parent)?;
                            }
                            let mut outfile = std::fs::File::create(&outpath)?;
                            let mut buffer = Vec::new();
                            file.read_to_end(&mut buffer)?;
                            outfile.write_all(&buffer)?;
                        }
                    }
                    Ok(())
                })
                .await??;
            }
            ArchiveType::Tar => {
                let dir = dir.to_owned();
                tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
                    let mut archive = tar::Archive::new(std::io::Cursor::new(file_buffer));
                    for entry in archive.entries()? {
                        let mut file = entry?;
                        let outpath = dir.join(file.path()?);

                        if file.header().entry_type().is_dir() {
                            std::fs::create_dir_all(&outpath)?;
                        } else {
                            if let Some(parent) = outpath.parent() {
                                std::fs::create_dir_all(parent)?;
                            }
                            let mut outfile = std::fs::File::create(&outpath)?;
                            let mut buffer = Vec::new();
                            file.read_to_end(&mut buffer)?;
                            outfile.write_all(&buffer)?;
                        }
                    }
                    Ok(())
                })
                .await??;
            }
            ArchiveType::Gzip => {
                let dir = dir.to_owned();
                tokio::task::spawn_blocking(move || -> anyhow::Result<()> {
                    let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(
                        std::io::Cursor::new(file_buffer),
                    ));
                    for entry in archive.entries()? {
                        let mut file = entry?;
                        let outpath = dir.join(file.path()?);

                        if file.header().entry_type().is_dir() {
                            std::fs::create_dir_all(&outpath)?;
                        } else {
                            if let Some(parent) = outpath.parent() {
                                std::fs::create_dir_all(parent)?;
                            }
                            let mut outfile = std::fs::File::create(&outpath)?;
                            let mut buffer = Vec::new();
                            file.read_to_end(&mut buffer)?;
                            outfile.write_all(&buffer)?;
                        }
                    }
                    Ok(())
                })
                .await??;
            }
        }

        Ok(())
    }
}