From 01fea29fde97a846bb786d625f5b3a0dbd41a2e9 Mon Sep 17 00:00:00 2001 From: Palash Tyagi <23239946+Magnus167@users.noreply.github.com> Date: Sat, 2 Nov 2024 03:26:31 +0000 Subject: [PATCH] wip... --- src/lib.rs | 3 +- src/{oauth.rs => oauth_client.rs} | 33 +++++--------------- src/requester.rs | 52 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 26 deletions(-) rename src/{oauth.rs => oauth_client.rs} (73%) create mode 100644 src/requester.rs diff --git a/src/lib.rs b/src/lib.rs index f3982a9..6d5e26c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ -pub mod oauth; +pub mod oauth_client; +pub mod requester; diff --git a/src/oauth.rs b/src/oauth_client.rs similarity index 73% rename from src/oauth.rs rename to src/oauth_client.rs index 00639c2..20b12a1 100644 --- a/src/oauth.rs +++ b/src/oauth_client.rs @@ -1,13 +1,14 @@ use reqwest::blocking::Client; -use reqwest::Error; +use reqwest::Error as ReqwestError; use serde_json::Value; use std::collections::HashMap; -use std::env; +// use std::env; +use std::error::Error; use std::time::{Duration, SystemTime}; pub const OAUTH_TOKEN_URL: &str = "https://authe.jpmchase.com/as/token.oauth2"; -struct OAuthClient { +pub struct OAuthClient { client_id: String, client_secret: String, token_url: String, @@ -16,7 +17,7 @@ struct OAuthClient { } impl OAuthClient { - fn new(client_id: String, client_secret: String) -> Self { + pub fn new(client_id: String, client_secret: String) -> Self { OAuthClient { client_id, client_secret, @@ -26,7 +27,7 @@ impl OAuthClient { } } - fn fetch_token(&mut self) -> Result<(), Error> { + pub fn fetch_token(&mut self) -> Result<(), ReqwestError> { let client = Client::new(); // Set up the form parameters for the request @@ -72,7 +73,7 @@ impl OAuthClient { } // Method to get headers with authorization, renewing the token if expired - fn get_headers(&mut self) -> Result, Error> { + pub fn get_headers(&mut self) -> Result, Box> { // Check if the token is expired; if it is, fetch a new one if self.is_token_expired() { println!("Token has expired. Fetching a new token..."); @@ -84,27 +85,9 @@ impl OAuthClient { if let Some(token) = &self.access_token { headers.insert("Authorization".to_string(), format!("Bearer {}", token)); } else { - eprintln!("No access token available."); + return Err("No access token available.".into()); } Ok(headers) } } - -// fn retrive_oauth_headers() -> Result<(), Error> { -// // Retrieve client_id and client_secret from environment variables -// let client_id = env::var("DQ_CLIENT_ID").expect("CLIENT_ID not set in environment"); -// let client_secret = env::var("DQ_CLIENT_SECRET").expect("CLIENT_SECRET not set in environment"); -// println!("Client ID: {}", client_id); -// let mut oauth_client = OAuthClient::new(client_id, client_secret); -// println!("OAuth client created."); -// print!("Fetching token..."); -// // Fetch headers with authorization, automatically refreshing token if needed -// let headers = oauth_client.get_headers()?; -// println!("Headers: {:?}", headers); -// Ok(()) -// } - -// fn main() { -// retrive_oauth_headers().unwrap(); -// } diff --git a/src/requester.rs b/src/requester.rs new file mode 100644 index 0000000..0aaa030 --- /dev/null +++ b/src/requester.rs @@ -0,0 +1,52 @@ +use reqwest::blocking::Client; +use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; +use std::collections::HashMap; +use std::error::Error; + +use crate::oauth_client::OAuthClient; + +const API_BASE_URL: &str = "https://api-developer.jpmorgan.com/research/dataquery-authe/api/v2"; +const HEARTBEAT_ENDPOINT: &str = "/services/heartbeat"; + +pub struct Requester { + oauth_client: OAuthClient, + client: Client, +} + +impl Requester { + pub fn new(oauth_client: OAuthClient) -> Self { + Requester { + oauth_client, + client: Client::new(), + } + } + + pub fn check_connection(&mut self) -> Result<(), Box> { + // Get headers with authorization from OAuthClient + let headers_map = self.oauth_client.get_headers()?; + + // Convert HashMap to HeaderMap + let mut headers = HeaderMap::new(); + for (key, value) in headers_map { + headers.insert( + HeaderName::from_bytes(key.as_bytes())?, + HeaderValue::from_str(&value)?, + ); + } + + // Construct the full URL + let url = format!("{}{}", API_BASE_URL, HEARTBEAT_ENDPOINT); + + // Make the GET request with the authorization headers + let response = self.client.get(&url).headers(headers).send()?; + + // Check the status of the response + if response.status().is_success() { + println!("Connection is successful: {}", response.status()); + Ok(()) + } else { + eprintln!("Failed to connect: {}", response.status()); + Err(Box::new(response.error_for_status().unwrap_err())) + } + } +}