Add DQTimeseriesRequestArgs struct and related methods; update requester module

This commit is contained in:
Palash Tyagi 2024-11-10 23:56:39 +00:00
parent c3a49abdf7
commit b3d36411f8
3 changed files with 87 additions and 79 deletions

View File

@ -2,3 +2,4 @@ pub mod jpmaqsdownload;
pub mod oauth_client; pub mod oauth_client;
pub mod requester; pub mod requester;
pub mod timeseries; pub mod timeseries;
pub mod parreq;

View File

@ -2,6 +2,7 @@ use crate::download::oauth_client::OAuthClient;
use crate::download::timeseries::DQCatalogueResponse; use crate::download::timeseries::DQCatalogueResponse;
use crate::download::timeseries::DQCatalogueSingleResponse; use crate::download::timeseries::DQCatalogueSingleResponse;
use crate::download::timeseries::DQTimeSeriesResponse; use crate::download::timeseries::DQTimeSeriesResponse;
use crate::download::timeseries::DQTimeseriesRequestArgs;
use crossbeam::channel; use crossbeam::channel;
use reqwest; use reqwest;
use reqwest::blocking::Client; use reqwest::blocking::Client;
@ -27,74 +28,6 @@ pub struct DQRequester {
rqclient: Client, rqclient: Client,
} }
#[derive(Debug, Clone)]
pub struct DQTimeseriesRequestArgs {
pub start_date: String,
pub end_date: String,
pub calendar: String,
pub frequency: String,
pub conversion: String,
pub nan_treatment: String,
pub expressions: Vec<String>,
}
impl DQTimeseriesRequestArgs {
pub fn new(
start_date: &str,
end_date: &str,
calendar: &str,
frequency: &str,
conversion: &str,
nan_treatment: &str,
expressions: Vec<String>,
) -> Self {
DQTimeseriesRequestArgs {
start_date: start_date.to_string(),
end_date: end_date.to_string(),
calendar: calendar.to_string(),
frequency: frequency.to_string(),
conversion: conversion.to_string(),
nan_treatment: nan_treatment.to_string(),
expressions,
}
}
pub fn update_expressions(&mut self, expressions: Vec<String>) {
self.expressions = expressions;
}
pub fn as_query_string(&self) -> String {
let mut params = vec![
("format", "JSON"),
("start-date", &self.start_date),
("end-date", &self.end_date),
("calendar", &self.calendar),
("frequency", &self.frequency),
("conversion", &self.conversion),
("nan_treatment", &self.nan_treatment),
("data", "NO_REFERENCE_DATA"),
];
for expression in &self.expressions {
params.push(("expressions", expression));
}
serde_urlencoded::to_string(&params).unwrap()
}
}
impl Default for DQTimeseriesRequestArgs {
fn default() -> Self {
DQTimeseriesRequestArgs {
start_date: "1990-01-01".to_string(),
end_date: "TODAY+2D".to_string(),
calendar: "CAL_ALLDAYS".to_string(),
frequency: "FREQ_DAY".to_string(),
conversion: "CONV_LASTBUS_ABS".to_string(),
nan_treatment: "NA_NOTHING".to_string(),
expressions: Vec::new(),
}
}
}
impl Default for DQRequester { impl Default for DQRequester {
fn default() -> Self { fn default() -> Self {
DQRequester { DQRequester {
@ -211,18 +144,19 @@ impl DQRequester {
Ok(response) Ok(response)
} }
pub fn get_timeseries(
&mut self,
args: DQTimeseriesRequestArgs,
) -> Result<Vec<DQTimeSeriesResponse>, Box<dyn Error>> {
let max_retries = 5;
println!(
"Invoking recursive function for {:?} expressions",
args.expressions.len()
);
_fetch_timeseries_recursive(self, args, max_retries)
}
} }
// pub fn get_timeseries(
// &mut self,
// args: DQTimeseriesRequestArgs,
// ) -> Result<Vec<DQTimeSeriesResponse>, Box<dyn Error>> {
// let max_retries = 5;
// println!(
// "Invoking recursive function for {:?} expressions",
// args.expressions.len()
// );
// _fetch_timeseries_recursive(self, args, max_retries)
// }
// }
fn _fetch_expression_batch( fn _fetch_expression_batch(
client: Arc<Mutex<DQRequester>>, client: Arc<Mutex<DQRequester>>,

View File

@ -8,6 +8,79 @@ use std::collections::HashSet;
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
#[derive(Debug, Clone)]
pub struct DQTimeseriesRequestArgs {
pub start_date: String,
pub end_date: String,
pub calendar: String,
pub frequency: String,
pub conversion: String,
pub nan_treatment: String,
pub expressions: Vec<String>,
}
impl DQTimeseriesRequestArgs {
pub fn new(
start_date: &str,
end_date: &str,
calendar: &str,
frequency: &str,
conversion: &str,
nan_treatment: &str,
expressions: Vec<String>,
) -> Self {
DQTimeseriesRequestArgs {
start_date: start_date.to_string(),
end_date: end_date.to_string(),
calendar: calendar.to_string(),
frequency: frequency.to_string(),
conversion: conversion.to_string(),
nan_treatment: nan_treatment.to_string(),
expressions,
}
}
pub fn update_expressions(&mut self, expressions: Vec<String>) {
self.expressions = expressions;
}
pub fn as_query_string(&self) -> String {
let mut params = vec![
("format", "JSON"),
("start-date", &self.start_date),
("end-date", &self.end_date),
("calendar", &self.calendar),
("frequency", &self.frequency),
("conversion", &self.conversion),
("nan_treatment", &self.nan_treatment),
("data", "NO_REFERENCE_DATA"),
];
for expression in &self.expressions {
params.push(("expressions", expression));
}
serde_urlencoded::to_string(&params).unwrap()
}
}
impl Default for DQTimeseriesRequestArgs {
fn default() -> Self {
DQTimeseriesRequestArgs {
start_date: "1990-01-01".to_string(),
end_date: "TODAY+2D".to_string(),
calendar: "CAL_ALLDAYS".to_string(),
frequency: "FREQ_DAY".to_string(),
conversion: "CONV_LASTBUS_ABS".to_string(),
nan_treatment: "NA_NOTHING".to_string(),
expressions: Vec::new(),
}
}
}
/// Response from the DataQuery API /// Response from the DataQuery API
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct DQTimeSeriesResponse { pub struct DQTimeSeriesResponse {