rsiot/components_config/http_client_old/
config.rs

1use std::time::Duration;
2
3use crate::{message::MsgDataBound, serde_utils::SerdeAlgKind};
4
5use super::{request_input::RequestInput, request_periodic::RequestPeriodic};
6
7/// Параметры компонента http-client
8#[derive(Clone, Debug)]
9pub struct Config<TMessage>
10where
11    TMessage: MsgDataBound,
12{
13    /// Алгоритм сериализации / десериализации
14    pub serde_alg: SerdeAlgKind,
15
16    /// URL сервера
17    ///
18    /// *Примеры:*
19    ///
20    /// ```
21    /// base_url: "http://10.0.6.5:80".into()
22    /// ```
23    pub base_url: String,
24    /// Таймаут запроса
25    pub timeout: Duration,
26    /// Запросы, которые формируются на основе входящих сообщений
27    pub requests_input: Vec<RequestInput<TMessage>>,
28    /// Периодические запросы
29    pub requests_periodic: Vec<RequestPeriodic<TMessage>>,
30}
31
32#[cfg(test)]
33mod tests {
34    use std::time::Duration;
35
36    use crate::{
37        message::{example_message::*, *},
38        serde_utils::SerdeAlgKind,
39    };
40
41    use super::super::*;
42
43    #[test]
44    fn connect_with_http_server() {
45        Config::<Custom> {
46            serde_alg: SerdeAlgKind::Json,
47            base_url: "http://10.0.6.5:80".into(),
48            timeout: Duration::from_secs(5),
49            requests_input: vec![RequestInput {
50                fn_input: |msg| {
51                    let param = HttpParam::Post {
52                        endpoint: "/messages".into(),
53                        body: msg.serialize().unwrap(),
54                    };
55                    Some(param)
56                },
57                on_success: |_| Ok(vec![]),
58                on_failure: Vec::new,
59            }],
60            requests_periodic: vec![RequestPeriodic {
61                period: Duration::from_secs(2),
62                http_param: HttpParam::Get {
63                    endpoint: "/messages".into(),
64                },
65                on_success: |data| {
66                    let msgs = Message::deserialize_many(data)?;
67                    Ok(msgs)
68                },
69                on_failure: Vec::new,
70            }],
71        };
72    }
73}