mirror of
https://github.com/Magnus167/msyrs.git
synced 2025-08-20 04:30:01 +00:00
Add TimeseriesRequestArgs struct and refactor get_timeseries_batch method to use it
This commit is contained in:
parent
ce870b171e
commit
9efd434210
@ -1,10 +1,8 @@
|
||||
use reqwest::blocking::Client;
|
||||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
||||
// use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
|
||||
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 HEARTBEAT_ENDPOINT: &str = "/services/heartbeat";
|
||||
@ -15,6 +13,39 @@ pub struct Requester {
|
||||
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 {
|
||||
pub fn new(oauth_client: OAuthClient) -> Self {
|
||||
Requester {
|
||||
@ -58,32 +89,29 @@ impl Requester {
|
||||
println!("Connection is successful: {}", response.status());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_timeseries_batch(
|
||||
&mut self,
|
||||
start_date: &str,
|
||||
end_date: &str,
|
||||
calendar: &str,
|
||||
frequency: &str,
|
||||
conversion: &str,
|
||||
nan_treatment: &str,
|
||||
expressions: Vec<&str>,
|
||||
args: TimeseriesRequestArgs,
|
||||
) -> 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
|
||||
let mut params = vec![
|
||||
("format", "JSON"),
|
||||
("start-date", start_date),
|
||||
("end-date", end_date),
|
||||
("calendar", calendar),
|
||||
("frequency", frequency),
|
||||
("conversion", conversion),
|
||||
("nan_treatment", nan_treatment),
|
||||
("start-date", &args.start_date),
|
||||
("end-date", &args.end_date),
|
||||
("calendar", &args.calendar),
|
||||
("frequency", &args.frequency),
|
||||
("conversion", &args.conversion),
|
||||
("nan_treatment", &args.nan_treatment),
|
||||
("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
|
||||
for expression in expressions {
|
||||
for expression in &args.expressions {
|
||||
params.push(("expressions", expression));
|
||||
}
|
||||
|
||||
@ -93,21 +121,21 @@ impl Requester {
|
||||
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
pub fn get_timeseries_with_defaults(
|
||||
&mut self,
|
||||
expressions: Vec<&str>,
|
||||
) -> Result<reqwest::blocking::Response, Box<dyn Error>> {
|
||||
self.get_timeseries_batch(
|
||||
// start_date,
|
||||
// end_date,
|
||||
"2024-10-20",
|
||||
"TODAY",
|
||||
// "CAL_WEEKDAYS",
|
||||
"CAL_ALLDAYS",
|
||||
"FREQ_DAY",
|
||||
"CONV_LASTBUS_ABS",
|
||||
"NA_NOTHING",
|
||||
let args = TimeseriesRequestArgs::new(
|
||||
"1990-01-01".into(),
|
||||
"TODAY+1".into(),
|
||||
"CAL_ALLDAYS".into(),
|
||||
"FREQ_DAY".into(),
|
||||
"CONV_LASTBUS_ABS".into(),
|
||||
"NA_NOTHING".into(),
|
||||
expressions,
|
||||
)
|
||||
);
|
||||
|
||||
self.get_timeseries_batch(args)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user