iadb_api/backend.rs
1use std::error::Error;
2use crate::{SeriesCode, 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::{SeriesCode, schemas::IADBSeries, backend::IADB};
20 ///
21 /// #[tokio::main]
22 /// async fn main() -> () {
23 ///
24 /// // Parameters
25 /// let date_from: String = String::from("01/Jan/2000");
26 /// let date_to: String = String::from("01/Oct/2018");
27 ///
28 /// // Data collection
29 /// let data: IADBSeries = IADB::get_data(&SeriesCode::IUDSOIA, &date_from, &date_to).await.unwrap();
30 ///
31 /// println!("{}", data);
32 ///
33 /// }
34 /// ```
35 pub async fn get_data(series_code: &SeriesCode, date_from: &String, date_to: &String) -> Result<IADBSeries, Box<dyn Error>> {
36 // Parameters
37 let using_codes: String = String::from("Y");
38 let vfd: String = String::from("N");
39 // Request
40 let params: Vec<Param> = vec![
41 Param::DateFrom { v: &date_from }, Param::DateTo { v: &date_to }, Param::CSVF { v: &CSVF::TN }, Param::UsingCodes { v: &using_codes },
42 Param::VPD { v: &VPD::Y }, Param::VFD { v: &vfd },
43 ];
44 call_api_endpoint(&series_code.to_string(), &Some(series_code.description()), params, None).await
45 }
46}
47
48
49#[cfg(test)]
50mod tests {
51 use tokio;
52
53 #[tokio::test]
54 async fn unit_test_get_data() -> () {
55 use crate::{SeriesCode, schemas::IADBSeries, backend::IADB};
56 // Parameters
57 let date_from: String = String::from("01/Jan/2000");
58 let date_to: String = String::from("01/Oct/2018");
59 // Data collection
60 let data: IADBSeries = IADB::get_data(&SeriesCode::IUDSOIA, &date_from, &date_to).await.unwrap();
61 println!("{}", data);
62 }
63}