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
use crate::{
    builder::LoggerError,
    logger::{CompressedLogs, PlaintextLogs},
};
use awc::http::header::CONTENT_TYPE;
use awc::Client;
use std::time::Duration;

static TIMEOUT: Duration = Duration::from_secs(60);

pub async fn plaintext_log_upload(msg: PlaintextLogs, url: String) -> Result<(), LoggerError> {
    debug_eprintln!("post? to {} with {} bytes", url, msg.logs.len());
    // an Actix web client instance with the default setting. the main gotcha
    // to check here is maximum payload size if you want to go really big
    let client = Client::default();
    let res = client
        .post(&url)
        .append_header((CONTENT_TYPE, "application/json"))
        .timeout(TIMEOUT)
        .send_json(&msg)
        .await;
    debug_eprintln!("response {:?}", res);
    match res {
        Ok(_) => Ok(()),
        Err(e) => Err(LoggerError::ConnectionFailure(e.to_string())),
    }
}

pub async fn compressed_log_upload(msg: CompressedLogs, url: String) -> Result<(), LoggerError> {
    debug_eprintln!(
        "compressed post? to {} with {} bytes",
        url,
        msg.compressed_plaintext_logs.len()
    );

    // an Actix web client instance with the default setting. the main gotcha
    // to check here is maximum payload size if you want to go really big
    // Limit here is 33554432 (32MB?)
    let client = Client::default();
    let res = client
        .post(&url)
        .append_header((CONTENT_TYPE, "application/json"))
        .timeout(TIMEOUT)
        .send_json(&msg)
        .await;
    debug_eprintln!("response {:?}", res);
    match res {
        Ok(_) => Ok(()),
        Err(e) => Err(LoggerError::ConnectionFailure(e.to_string())),
    }
}