From 80f266e194928e137c7575778b452777e33cc7c6 Mon Sep 17 00:00:00 2001 From: kvillers Date: Mon, 20 Apr 2026 16:26:12 +0200 Subject: [PATCH] conv to struct test --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 50 +++++++++++++++++++++++++++++++-------------- src/template/mod.rs | 1 + 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 src/template/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 72aeec9..3e4f298 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "antares_get_data" version = "0.1.0" dependencies = [ "reqwest", + "serde", "serde_json", ] diff --git a/Cargo.toml b/Cargo.toml index 5228a2f..eb67d05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2024" [dependencies] reqwest = { version = "0.12", features = ["blocking"] } serde_json = "1.0" +serde = { version = "1.0", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 9041f17..489ace4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { // 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::(&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::(&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"); - eprintln!("Warning: response is not valid JSON; saved raw response to antares.json"); + // Fall back to pretty-printing raw JSON value, or raw text. + if let Ok(json_val) = serde_json::from_str::(&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(()) } diff --git a/src/template/mod.rs b/src/template/mod.rs new file mode 100644 index 0000000..524221a --- /dev/null +++ b/src/template/mod.rs @@ -0,0 +1 @@ +pub mod antares;