abs_data/builders/
sdmx_data_request_builder.rs

1use crate::{
2    builders::url_builder::UrlBuilder,
3    config::Config,
4    models::typed::{
5        dataflow_identifier::DataflowIdentifier, datakey::DataKey, detail::Detail,
6        dimension_at_observation::DimensionAtObservation, period::Period,
7        sdmx_data_request::SdmxDataRequest, sdmx_request::SdmxRequest,
8    },
9};
10
11pub struct SdmxDataRequestBuilder<'a> {
12    base_url: &'a str,
13    path: &'a str,
14    dataflow_identifier: &'a DataflowIdentifier,
15    data_key: Option<&'a DataKey>,
16    start_period: Option<&'a Period>,
17    end_period: Option<&'a Period>,
18    detail: Option<&'a Detail>,
19    dimension_at_observation: Option<&'a DimensionAtObservation>,
20    key: Option<&'a str>,
21    headers: &'a [(&'a str, &'a str)],
22}
23
24impl<'a> SdmxDataRequestBuilder<'a> {
25    pub fn new(dataflow_identifier: &'a DataflowIdentifier) -> Self {
26        Self {
27            base_url: Config::BASE_URL,
28            path: Config::DATA_PATH,
29            dataflow_identifier,
30            data_key: None,
31            start_period: None,
32            end_period: None,
33            detail: None,
34            dimension_at_observation: None,
35            key: None,
36            headers: &[Config::USER_AGENT_ANONYMOUS, Config::ACCEPT_DATA_JSON],
37        }
38    }
39
40    pub fn data_key(mut self, data_key: &'a DataKey) -> Self {
41        self.data_key = Some(data_key);
42        self
43    }
44
45    pub fn start_period(mut self, start_period: &'a Period) -> Self {
46        self.start_period = Some(start_period);
47        self
48    }
49
50    pub fn end_period(mut self, end_period: &'a Period) -> Self {
51        self.end_period = Some(end_period);
52        self
53    }
54
55    pub fn detail(mut self, detail: &'a Detail) -> Self {
56        self.detail = Some(detail);
57        self
58    }
59
60    pub fn dimension_at_observation(
61        mut self,
62        dimension_at_observation: &'a DimensionAtObservation,
63    ) -> Self {
64        self.dimension_at_observation = Some(dimension_at_observation);
65        self
66    }
67
68    pub fn key(mut self, key: &'a str) -> Self {
69        self.key = Some(key);
70        self
71    }
72
73    pub fn build(&self) -> SdmxDataRequest {
74        let mut url_builder = UrlBuilder::new(self.base_url)
75            .add_path_segment(self.path)
76            .add_path_segment(self.dataflow_identifier.key());
77
78        if let Some(data_key) = self.data_key {
79            url_builder = url_builder.add_path_segment(data_key.to_string());
80        } else {
81            url_builder = url_builder.add_path_segment(DataKey::default().to_string());
82        }
83
84        if let Some(start_period) = &self.start_period {
85            url_builder =
86                url_builder.add_query_param(Config::QUERY_START_PERIOD, start_period.to_string());
87        }
88        if let Some(end_period) = &self.end_period {
89            url_builder =
90                url_builder.add_query_param(Config::QUERY_END_PERIOD, end_period.to_string());
91        }
92        if let Some(detail) = &self.detail {
93            url_builder = url_builder.add_query_param(Config::QUERY_DETAIL, detail.to_string());
94        }
95        if let Some(dimension_at_observation) = &self.dimension_at_observation {
96            url_builder = url_builder.add_query_param(
97                Config::QUERY_DIMENSION_AT_OBSERVATION,
98                dimension_at_observation.to_string(),
99            );
100        }
101
102        let url = url_builder.build().expect("Failed to build url");
103
104        let request = SdmxRequest::new(url, self.key, self.headers);
105
106        SdmxDataRequest::from(request)
107    }
108}