mirror of
https://github.com/Magnus167/msyrs.git
synced 2025-08-20 07:20:01 +00:00
wip...
This commit is contained in:
parent
8bda1da6fe
commit
01fea29fde
@ -1 +1,2 @@
|
||||
pub mod oauth;
|
||||
pub mod oauth_client;
|
||||
pub mod requester;
|
||||
|
@ -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<HashMap<String, String>, Error> {
|
||||
pub fn get_headers(&mut self) -> Result<HashMap<String, String>, Box<dyn Error>> {
|
||||
// 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();
|
||||
// }
|
52
src/requester.rs
Normal file
52
src/requester.rs
Normal file
@ -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<dyn Error>> {
|
||||
// Get headers with authorization from OAuthClient
|
||||
let headers_map = self.oauth_client.get_headers()?;
|
||||
|
||||
// Convert HashMap<String, String> 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()))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user