antares_get_data/src/main.rs

57 lines
2.2 KiB
Rust

use std::fs;
use std::error::Error;
mod template;
use template::antares::Antares;
fn make_url(base: &str, usercode: &str, password: &str, cikkszam: &str) -> String {
format!(
"{}&USERCODE={}&PASSWORD={}&PIN=&cikkszam={}",
base, usercode, password, cikkszam
)
}
fn main() -> Result<(), Box<dyn Error>> {
// Separate values
let base = "https://b2b.antares.hu/I4stechproductionWebInt/IntAntaresWebCikkDataService.svc/webhttps/Get_CikkInfokWeb?SCHEMA=ANTARESINT";
let usercode = "orinkkft";
let password = "8E7DCB55F3B4ECC52D0451A1F8D851D0EF2193FCE4B83E528C18A8F68F8F2658EFCF2EA08563EC702A1C701934C3FBF5F6880BE894A16387326C7180A9A4C361";
let cikkszam = ""; // supply a value if needed
let url = make_url(base, usercode, password, cikkszam);
let response = reqwest::blocking::get(&url)?.text()?;
// Try to deserialize into strongly-typed `Antares` list.
let items: Antares = match serde_json::from_str::<Antares>(&response) {
Ok(items) => {
// Re-serialize via the structs to ensure consistent formatting
let pretty = serde_json::to_string_pretty(&items)?;
fs::write("antares.json", pretty)?;
println!("Parsed {} items and saved to antares.json", items.len());
items
}
Err(_) => {
// Fall back to pretty-printing raw JSON value, or raw text.
if let Ok(json_val) = serde_json::from_str::<serde_json::Value>(&response) {
let pretty = serde_json::to_string_pretty(&json_val)?;
fs::write("antares.json", pretty)?;
eprintln!("Warning: response didn't match expected struct but was valid JSON; saved pretty JSON to antares.json");
} else {
fs::write("antares.json", &response)?;
eprintln!("Warning: response is not valid JSON; saved raw response to antares.json");
}
Vec::new()
}
};
// Example: print first item's Cikkszam if available
if let Some(first) = items.get(0) {
if let Some(ref cs) = first.cikkszam {
println!("First item Cikkszam: {}", cs);
}
}
Ok(())
}