conv to struct test

This commit is contained in:
Villers Krisztián 2026-04-20 16:26:12 +02:00
parent 0746ac8e89
commit 80f266e194
4 changed files with 38 additions and 15 deletions

1
Cargo.lock generated
View File

@ -7,6 +7,7 @@ name = "antares_get_data"
version = "0.1.0"
dependencies = [
"reqwest",
"serde",
"serde_json",
]

View File

@ -6,4 +6,5 @@ edition = "2024"
[dependencies]
reqwest = { version = "0.12", features = ["blocking"] }
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }

View File

@ -1,36 +1,56 @@
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 {
// Constructs the full URL by injecting user credentials and cikkszam into the base URL.
format!(
"{}&USERCODE={}&PASSWORD={}&PIN=&cikkszam={}",
base, usercode, password, cikkszam
)
}
fn main() {
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 base = "https://b2b.antares.hu/I4stechproductionWebInt/IntAntaresWebCikkDataService.svc/webhttps/Get_CikkInfokWeb?SCHEMA=ANTARESINT";
let cikkszam = ""; // supply a value if needed
let url = make_url(base, usercode, password, cikkszam);
let response = reqwest::blocking::get(&url)
.expect("Failed to send request")
.text()
.expect("Failed to read response body");
let response = reqwest::blocking::get(&url)?.text()?;
// Try to parse and pretty-print JSON; otherwise save raw response.
match serde_json::from_str::<serde_json::Value>(&response) {
Ok(json) => {
let pretty = serde_json::to_string_pretty(&json).expect("Failed to format JSON");
fs::write("antares.json", pretty).expect("Failed to write antares.json");
println!("Saved pretty JSON to antares.json");
// 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(_) => {
fs::write("antares.json", response).expect("Failed to write antares.json");
// 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(())
}

1
src/template/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod antares;