conv to struct test
This commit is contained in:
parent
0746ac8e89
commit
80f266e194
|
|
@ -7,6 +7,7 @@ name = "antares_get_data"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"reqwest",
|
"reqwest",
|
||||||
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@ edition = "2024"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.12", features = ["blocking"] }
|
reqwest = { version = "0.12", features = ["blocking"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
||||||
|
|
|
||||||
48
src/main.rs
48
src/main.rs
|
|
@ -1,36 +1,56 @@
|
||||||
use std::fs;
|
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 {
|
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!(
|
format!(
|
||||||
"{}&USERCODE={}&PASSWORD={}&PIN=&cikkszam={}",
|
"{}&USERCODE={}&PASSWORD={}&PIN=&cikkszam={}",
|
||||||
base, usercode, password, cikkszam
|
base, usercode, password, cikkszam
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// Separate values
|
// Separate values
|
||||||
|
let base = "https://b2b.antares.hu/I4stechproductionWebInt/IntAntaresWebCikkDataService.svc/webhttps/Get_CikkInfokWeb?SCHEMA=ANTARESINT";
|
||||||
let usercode = "orinkkft";
|
let usercode = "orinkkft";
|
||||||
let password = "8E7DCB55F3B4ECC52D0451A1F8D851D0EF2193FCE4B83E528C18A8F68F8F2658EFCF2EA08563EC702A1C701934C3FBF5F6880BE894A16387326C7180A9A4C361";
|
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 cikkszam = ""; // supply a value if needed
|
||||||
|
|
||||||
let url = make_url(base, usercode, password, cikkszam);
|
let url = make_url(base, usercode, password, cikkszam);
|
||||||
|
|
||||||
let response = reqwest::blocking::get(&url)
|
let response = reqwest::blocking::get(&url)?.text()?;
|
||||||
.expect("Failed to send request")
|
|
||||||
.text()
|
|
||||||
.expect("Failed to read response body");
|
|
||||||
|
|
||||||
// Try to parse and pretty-print JSON; otherwise save raw response.
|
// Try to deserialize into strongly-typed `Antares` list.
|
||||||
match serde_json::from_str::<serde_json::Value>(&response) {
|
let items: Antares = match serde_json::from_str::<Antares>(&response) {
|
||||||
Ok(json) => {
|
Ok(items) => {
|
||||||
let pretty = serde_json::to_string_pretty(&json).expect("Failed to format JSON");
|
// Re-serialize via the structs to ensure consistent formatting
|
||||||
fs::write("antares.json", pretty).expect("Failed to write antares.json");
|
let pretty = serde_json::to_string_pretty(&items)?;
|
||||||
println!("Saved pretty JSON to antares.json");
|
fs::write("antares.json", pretty)?;
|
||||||
|
println!("Parsed {} items and saved to antares.json", items.len());
|
||||||
|
items
|
||||||
}
|
}
|
||||||
Err(_) => {
|
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");
|
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(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod antares;
|
||||||
Loading…
Reference in New Issue