detsys_ids_client/
builder.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
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
use std::time::Duration;

use reqwest::Certificate;
use url::Url;

use crate::transport::TransportsError;
use crate::{system_snapshot::SystemSnapshotter, DeviceId, DistinctId, Map};
use crate::{Recorder, Worker};

#[derive(Default)]
pub struct Builder {
    distinct_id: Option<DistinctId>,
    device_id: Option<DeviceId>,
    endpoint: Option<String>,
    facts: Option<Map>,
    groups: Option<Map>,
    ssl_cert: Option<Certificate>,
    timeout: Option<Duration>,
    proxy: Option<Url>,
}

impl Builder {
    pub fn new() -> Self {
        Builder {
            distinct_id: None,
            device_id: None,
            endpoint: None,
            facts: None,
            groups: None,
            ssl_cert: None,
            timeout: None,
            proxy: None,
        }
    }

    pub fn set_distinct_id(&mut self, distinct_id: impl Into<DistinctId>) -> &mut Self {
        self.distinct_id = Some(distinct_id.into());
        self
    }

    pub fn set_device_id(&mut self, device_id: impl Into<DeviceId>) -> &mut Self {
        self.device_id = Some(device_id.into());
        self
    }

    pub fn set_facts(&mut self, facts: Map) -> &mut Self {
        self.facts = Some(facts);
        self
    }

    pub fn set_groups(&mut self, groups: Map) -> &mut Self {
        self.groups = Some(groups);
        self
    }

    pub fn add_fact(
        &mut self,
        key: impl Into<String> + std::fmt::Debug,
        value: impl Into<serde_json::Value>,
    ) -> &mut Self {
        self.facts
            .get_or_insert_with(Default::default)
            .insert(key.into(), value.into());
        self
    }

    pub fn set_endpoint(&mut self, endpoint: impl Into<String>) -> &mut Self {
        self.endpoint = Some(endpoint.into());
        self
    }

    pub fn set_timeout(&mut self, duration: impl Into<Duration>) -> &mut Self {
        self.timeout = Some(duration.into());
        self
    }

    #[tracing::instrument(skip(self))]
    pub async fn try_set_ssl_cert_file(
        &mut self,
        ssl_cert_file: impl AsRef<std::path::Path> + std::fmt::Debug,
    ) -> Result<&mut Self, TransportsError> {
        self.ssl_cert = Some(read_cert_file(&ssl_cert_file).await?);
        Ok(self)
    }

    pub fn set_proxy(&mut self, proxy: Url) -> &mut Self {
        self.proxy = Some(proxy);
        self
    }

    #[tracing::instrument(skip(self))]
    pub async fn build(self) -> Result<(Recorder, Worker), TransportsError> {
        self.build_with_snapshotter(crate::system_snapshot::Generic::default())
            .await
    }

    #[tracing::instrument(skip(self, snapshotter))]
    pub async fn build_with_snapshotter<S: SystemSnapshotter>(
        mut self,
        snapshotter: S,
    ) -> Result<(Recorder, Worker), TransportsError> {
        let transport = crate::transport::Transports::try_new(
            self.endpoint.take(),
            self.timeout
                .take()
                .unwrap_or_else(|| Duration::from_secs(3)),
            self.ssl_cert.take(),
            self.proxy.take(),
        )
        .await?;

        let (recorder, worker) = Worker::new(
            self.distinct_id.take(),
            self.device_id.take(),
            self.facts.take(),
            self.groups.take(),
            snapshotter,
            transport,
        )
        .await;

        Ok((recorder, worker))
    }
}

#[tracing::instrument(ret(level = tracing::Level::TRACE))]
async fn read_cert_file(
    ssl_cert_file: impl AsRef<std::path::Path> + std::fmt::Debug,
) -> Result<Certificate, TransportsError> {
    let cert_buf = tokio::fs::read(&ssl_cert_file)
        .await
        .map_err(|e| TransportsError::Read(ssl_cert_file.as_ref().to_path_buf(), e))?;

    if let Ok(cert) = Certificate::from_pem(cert_buf.as_slice()) {
        return Ok(cert);
    }

    if let Ok(cert) = Certificate::from_der(cert_buf.as_slice()) {
        return Ok(cert);
    }

    Err(TransportsError::UnknownCertFormat)
}