env tool
This commit is contained in:
parent
2228a6e03e
commit
de03588cfc
86
src/main.rs
86
src/main.rs
|
|
@ -1,41 +1,55 @@
|
|||
use reqwest::blocking::Client;
|
||||
use std::error::Error;
|
||||
use std::fs;
|
||||
use std::time::Duration;
|
||||
|
||||
mod api;
|
||||
mod template;
|
||||
mod tools;
|
||||
|
||||
use reqwest::blocking::Client;
|
||||
use std::{
|
||||
error::Error,
|
||||
fs,
|
||||
time::Duration,
|
||||
io::{
|
||||
Error as IO_Error,
|
||||
ErrorKind::{
|
||||
Other,
|
||||
ConnectionRefused
|
||||
}
|
||||
}
|
||||
};
|
||||
use api::make_url;
|
||||
use template::antares::Antares;
|
||||
use tools::{Logger, export_to_excel};
|
||||
use tools::{
|
||||
Logger,
|
||||
get_env,
|
||||
export_to_excel
|
||||
};
|
||||
|
||||
fn make_request(
|
||||
url: &str,
|
||||
logger: &Logger,
|
||||
) -> Result<String, Box<dyn Error>> {
|
||||
const URL_KEY: &str = "URL";
|
||||
const USERCODE_KEY: &str = "ANTARES_USERCODE";
|
||||
const PASSWORD_KEY: &str = "ANTARES_PASSWORD";
|
||||
const OUT_KEY: &str = "OUT";
|
||||
|
||||
fn make_request(url: &str, logger: &Logger) -> Result<String, Box<dyn Error>> {
|
||||
let client = Client::builder()
|
||||
.timeout(Duration::from_secs(300))
|
||||
.build().map_err(|e| {
|
||||
logger.log_error(&format!("Failed to create HTTP client: {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?;
|
||||
|
||||
let response = client.get(url).send().map_err(|e| {
|
||||
logger.log_error(&format!("HTTP request failed: {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, e.to_string()))
|
||||
Box::new(IO_Error::new(ConnectionRefused, e.to_string()))
|
||||
})?;
|
||||
|
||||
if response.status().is_success() {
|
||||
Ok(response.text().map_err(|e| {
|
||||
logger.log_error(&format!("Failed to read response body: {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?)
|
||||
} else {
|
||||
let error_msg = format!("HTTP error: {:?}", response.status());
|
||||
logger.log_error(&error_msg);
|
||||
Err(Box::new(std::io::Error::new(std::io::ErrorKind::ConnectionRefused, error_msg)))
|
||||
Err(Box::new(IO_Error::new(ConnectionRefused, error_msg)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -45,31 +59,33 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
let usercode = std::env::var("ANTARES_USERCODE")
|
||||
let usercode = get_env(USERCODE_KEY)
|
||||
.map_err(|e| {
|
||||
logger.log_error(&format!("Missing environment variable: ANTARES_USERCODE. {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Missing ANTARES_USERCODE env var".to_string()))
|
||||
})?;
|
||||
let password = std::env::var("ANTARES_PASSWORD")
|
||||
.map_err(|e| {
|
||||
logger.log_error(&format!("Missing environment variable: ANTARES_PASSWORD. {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Missing ANTARES_PASSWORD env var".to_string()))
|
||||
})?;
|
||||
let out_path = std::env::var("OUT")
|
||||
.map_err(|e| {
|
||||
logger.log_error(&format!("Missing environment variable: OUT. {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Missing OUT env var".to_string()))
|
||||
logger.log_error(&e);
|
||||
e
|
||||
})?;
|
||||
|
||||
let base = std::env::var("URL")
|
||||
let password = get_env(PASSWORD_KEY)
|
||||
.map_err(|e| {
|
||||
logger.log_error(&format!("Missing environment variable: URL. {}", e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Missing URL env var".to_string()))
|
||||
logger.log_error(&e);
|
||||
e
|
||||
})?;
|
||||
|
||||
let out_path = get_env(OUT_KEY)
|
||||
.map_err(|e| {
|
||||
logger.log_error(&e);
|
||||
e
|
||||
})?;
|
||||
|
||||
let url = get_env(URL_KEY)
|
||||
.map_err(|e| {
|
||||
logger.log_error(&e);
|
||||
e
|
||||
})?;
|
||||
|
||||
let cikkszam = ""; // supply a value if needed;
|
||||
|
||||
let url = make_url(&base, &usercode, &password, cikkszam);
|
||||
let url = make_url(&url, &usercode, &password, cikkszam);
|
||||
|
||||
let response = match make_request(&url, &logger) {
|
||||
Ok(resp) => {
|
||||
|
|
@ -86,7 +102,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
let antares_file = format!("{}/antares.json", temp_dir);
|
||||
fs::create_dir_all(temp_dir).map_err(|e| {
|
||||
logger.log_error(&format!("Failed to create temp directory '{}': {}", temp_dir, e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?;
|
||||
|
||||
// Try to deserialize into strongly-typed `Antares` list.
|
||||
|
|
@ -102,7 +118,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
};
|
||||
fs::write(&antares_file, &pretty).map_err(|e| {
|
||||
logger.log_error(&format!("Failed to write {}: {}", antares_file, e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?;
|
||||
logger.log(&format!("API call successful - Fetched {} items", items.len()));
|
||||
items
|
||||
|
|
@ -119,13 +135,13 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||
};
|
||||
fs::write(&antares_file, &pretty).map_err(|e| {
|
||||
logger.log_error(&format!("Failed to write {}: {}", antares_file, e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?;
|
||||
logger.log_info("Response saved as JSON (schema mismatch)");
|
||||
} else {
|
||||
fs::write(&antares_file, &response).map_err(|e| {
|
||||
logger.log_error(&format!("Failed to write {}: {}", antares_file, e));
|
||||
Box::new(std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
|
||||
Box::new(IO_Error::new(Other, e.to_string()))
|
||||
})?;
|
||||
logger.log_info("Response saved as raw text (not valid JSON)");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
// .env handling
|
||||
use std::env;
|
||||
|
||||
pub fn get_env(key_name: &str) -> Result<String, String> {
|
||||
match env::var(key_name) {
|
||||
Ok(value) => Ok(value),
|
||||
Err(e) => Err(format!(
|
||||
"Failed to read environment variable key: {}, error: {}",
|
||||
key_name,
|
||||
e
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
pub mod excel;
|
||||
pub mod logger;
|
||||
pub mod env;
|
||||
|
||||
pub use excel::export_to_excel;
|
||||
pub use logger::Logger;
|
||||
pub use env::get_env;
|
||||
Loading…
Reference in New Issue