Add TimeseriesRequestArgs struct and refactor get_timeseries_batch method to use it

This commit is contained in:
Palash Tyagi 2024-11-06 14:37:22 +00:00
parent ce870b171e
commit 9efd434210

View File

@ -1,10 +1,8 @@
use reqwest::blocking::Client; use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
// use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use crate::oauth_client::OAuthClient; use crate::oauth_client::OAuthClient;
// use crate::timeseries::TimeSeries;
const API_BASE_URL: &str = "https://api-developer.jpmorgan.com/research/dataquery-authe/api/v2"; const API_BASE_URL: &str = "https://api-developer.jpmorgan.com/research/dataquery-authe/api/v2";
const HEARTBEAT_ENDPOINT: &str = "/services/heartbeat"; const HEARTBEAT_ENDPOINT: &str = "/services/heartbeat";
@ -15,6 +13,39 @@ pub struct Requester {
rqclient: Client, rqclient: Client,
} }
#[derive(Default)]
pub struct TimeseriesRequestArgs {
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 TimeseriesRequestArgs {
pub fn new(
start_date: Option<&str>,
end_date: Option<&str>,
calendar: Option<&str>,
frequency: Option<&str>,
conversion: Option<&str>,
nan_treatment: Option<&str>,
expressions: Vec<&str>,
) -> Self {
TimeseriesRequestArgs {
start_date: start_date.unwrap_or("2024-10-20").to_string(),
end_date: end_date.unwrap_or("TODAY").to_string(),
calendar: calendar.unwrap_or("CAL_ALLDAYS").to_string(),
frequency: frequency.unwrap_or("FREQ_DAY").to_string(),
conversion: conversion.unwrap_or("CONV_LASTBUS_ABS").to_string(),
nan_treatment: nan_treatment.unwrap_or("NA_NOTHING").to_string(),
expressions: expressions.into_iter().map(|s| s.to_string()).collect(),
}
}
}
impl Requester { impl Requester {
pub fn new(oauth_client: OAuthClient) -> Self { pub fn new(oauth_client: OAuthClient) -> Self {
Requester { Requester {
@ -58,32 +89,29 @@ impl Requester {
println!("Connection is successful: {}", response.status()); println!("Connection is successful: {}", response.status());
Ok(()) Ok(())
} }
pub fn get_timeseries_batch( pub fn get_timeseries_batch(
&mut self, &mut self,
start_date: &str, args: TimeseriesRequestArgs,
end_date: &str,
calendar: &str,
frequency: &str,
conversion: &str,
nan_treatment: &str,
expressions: Vec<&str>,
) -> Result<reqwest::blocking::Response, Box<dyn Error>> { ) -> Result<reqwest::blocking::Response, Box<dyn Error>> {
if args.expressions.len() < 1 || args.expressions.len() > 20 {
return Err("Number of expressions must be between 1 and 20".into());
}
// Construct the parameters // Construct the parameters
let mut params = vec![ let mut params = vec![
("format", "JSON"), ("format", "JSON"),
("start-date", start_date), ("start-date", &args.start_date),
("end-date", end_date), ("end-date", &args.end_date),
("calendar", calendar), ("calendar", &args.calendar),
("frequency", frequency), ("frequency", &args.frequency),
("conversion", conversion), ("conversion", &args.conversion),
("nan_treatment", nan_treatment), ("nan_treatment", &args.nan_treatment),
("data", "NO_REFERENCE_DATA"), ("data", "NO_REFERENCE_DATA"),
]; ];
if (20 < expressions.len()) || (expressions.len() < 1) {
return Err("Number of expressions must be between 1 and 20".into());
}
// Add expressions to the parameters // Add expressions to the parameters
for expression in expressions { for expression in &args.expressions {
params.push(("expressions", expression)); params.push(("expressions", expression));
} }
@ -93,21 +121,21 @@ impl Requester {
Ok(response) Ok(response)
} }
pub fn get_timeseries_with_defaults( pub fn get_timeseries_with_defaults(
&mut self, &mut self,
expressions: Vec<&str>, expressions: Vec<&str>,
) -> Result<reqwest::blocking::Response, Box<dyn Error>> { ) -> Result<reqwest::blocking::Response, Box<dyn Error>> {
self.get_timeseries_batch( let args = TimeseriesRequestArgs::new(
// start_date, "1990-01-01".into(),
// end_date, "TODAY+1".into(),
"2024-10-20", "CAL_ALLDAYS".into(),
"TODAY", "FREQ_DAY".into(),
// "CAL_WEEKDAYS", "CONV_LASTBUS_ABS".into(),
"CAL_ALLDAYS", "NA_NOTHING".into(),
"FREQ_DAY",
"CONV_LASTBUS_ABS",
"NA_NOTHING",
expressions, expressions,
) );
self.get_timeseries_batch(args)
} }
} }