Skip to main content

rsiot/components_config/http_client/
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// ANCHOR: Config
8/// Параметры компонента http-client
9#[derive(Clone, Debug)]
10pub struct Config<TMessage>
11where
12    TMessage: MsgDataBound,
13{
14    /// Алгоритм сериализации / десериализации
15    pub serde_alg: SerdeAlgKind,
16
17    /// URL сервера
18    ///
19    /// *Примеры:*
20    ///
21    /// ```
22    /// base_url: "http://10.0.6.5:80".into()
23    /// ```
24    pub base_url: String,
25    /// Таймаут запроса
26    pub timeout: Duration,
27    /// Запросы, которые формируются на основе входящих сообщений
28    pub requests_input: Vec<Box<dyn RequestInput<TMessage>>>,
29    /// Периодические запросы
30    pub requests_periodic: Vec<Box<dyn RequestPeriodic<TMessage>>>,
31}
32// ANCHOR: Config
33
34#[cfg(test)]
35mod tests {
36    use std::time::Duration;
37
38    use crate::{message::example_message::*, serde_utils::SerdeAlgKind};
39
40    use super::super::*;
41
42    #[test]
43    fn connect_with_http_server() {
44        Config::<Custom> {
45            serde_alg: SerdeAlgKind::Unspecified,
46            base_url: "http://10.0.6.5:80".into(),
47            timeout: Duration::from_secs(5),
48            requests_input: vec![Box::new(RequestInputConfig::<Custom, (), ()> {
49                serde_alg: SerdeAlgKind::Unspecified,
50                request_kind: RequestKind::Post,
51                endpoint: "/messages".into(),
52                fn_create_request: |_msg| Some(()),
53                fn_process_response_success: |_| vec![],
54                fn_process_response_error: Vec::new,
55            })],
56            requests_periodic: vec![Box::new(RequestPeriodicConfig::<Custom, (), ()> {
57                serde_alg: SerdeAlgKind::Unspecified,
58                request_kind: RequestKind::Get,
59                endpoint: "/messages".into(),
60                period: Duration::from_secs(2),
61                request_body: (),
62                fn_process_response_success: |_| vec![],
63                fn_process_response_error: Vec::new,
64            })],
65        };
66    }
67}