iadb_api/backend.rs
1use std::error::Error;
2use crate::{schemas::IADBSeries, utils::{VPD, CSVF, Param, call_api_endpoint}};
3
4
5pub struct IADB;
6
7impl IADB {
8
9 /// Makes an API request to the IADB and deserializes the response into a time series.
10 ///
11 /// # Input
12 /// - `series_code`: Code of the time series in the IADB.
13 /// - `date_from`: Date from which the data will be extracted (Note: Date format is `%d/%b/%Y`)
14 /// - `date_from`: Date up to which the data will be extracted (Note: Date format is `%d/%b/%Y`)
15 ///
16 /// # Examples
17 ///
18 /// ```rust
19 /// use iadb_api::{schemas::IADBSeries, backend::IADB};
20 ///
21 /// #[tokio::main]
22 /// async fn main() -> () {
23 ///
24 /// // Parameters
25 /// let series_code: String = SeriesCode::IUDSOIA.to_string();
26 /// let date_from: String = String::from("01/Jan/2000");
27 /// let date_to: String = String::from("01/Oct/2018");
28 ///
29 /// // Data collection
30 /// let data: IADBSeries = IADB::get_data(&series_code, &date_from, &date_to).await.unwrap();
31 ///
32 /// println!("{}", data);
33 ///
34 /// }
35 /// ```
36 pub async fn get_data(series_code: &String, date_from: &String, date_to: &String) -> Result<IADBSeries, Box<dyn Error>> {
37 // Parameters
38 let using_codes: String = String::from("Y");
39 let vfd: String = String::from("N");
40 // Request
41 let params: Vec<Param> = vec![
42 Param::DateFrom { v: &date_from }, Param::DateTo { v: &date_to }, Param::CSVF { v: &CSVF::TN }, Param::UsingCodes { v: &using_codes },
43 Param::VPD { v: &VPD::Y }, Param::VFD { v: &vfd },
44 ];
45 call_api_endpoint(&series_code, params, None).await
46 }
47}
48
49
50#[cfg(test)]
51mod tests {
52 use tokio;
53
54 #[tokio::test]
55 async fn unit_test_get_data() -> () {
56 use crate::{SeriesCode, schemas::IADBSeries, backend::IADB};
57 // Parameters
58 let series_code: String = SeriesCode::IUDSOIA.to_string();
59 let date_from: String = String::from("01/Jan/2000");
60 let date_to: String = String::from("01/Oct/2018");
61 // Data collection
62 let data: IADBSeries = IADB::get_data(&series_code, &date_from, &date_to).await.unwrap();
63 println!("{}", data);
64 }
65}