Add token request step to demo with reqwest

This commit is contained in:
2024-12-23 19:12:09 -06:00
parent fc643e5223
commit f228cbc7e4
3 changed files with 1233 additions and 2 deletions

1181
demo/Cargo.lock generated
View File

File diff suppressed because it is too large Load Diff

View File

@@ -7,6 +7,7 @@ build = "build.rs"
[dependencies]
chrono = "0.4.39"
hex = "0.4.3"
reqwest = { version = "0.12.9", features = ["blocking", "json"] }
serde = { version = "1.0.216", features = ["derive"] }
serde_json = "1.0.134"
sha2 = "0.10.8"

View File

@@ -26,11 +26,60 @@ fn main() {
return;
}
// TODO: Use token to make request
// Check the hash of the value
let value_hash = sha2::Sha256::digest(key_data.value.as_bytes());
let hash_match = hex::encode(value_hash) == key_data.value_hash;
// if hash_match {
// return;
// }
// TODO: Use token to make request
let client = reqwest::blocking::Client::new();
let response = client
.post(&format!(
"http://localhost:5800/notify?key={}",
key_data.value
))
.send();
match response {
Ok(resp) => {
if resp.status().is_success() {
println!("Request successful");
} else {
println!("Request failed with status: {}", resp.status());
if resp
.headers()
.get(reqwest::header::CONTENT_TYPE)
.map(|v| v == "application/json")
.unwrap_or(false)
{
match resp.json::<serde_json::Value>() {
Ok(json_body) => {
println!(
"Response JSON: {}",
serde_json::to_string_pretty(&json_body).unwrap()
);
}
Err(e) => {
println!("Failed to parse JSON response: {}", e);
}
}
} else {
println!(
"Response body: {}",
resp.text()
.unwrap_or_else(|_| "Failed to read response body".to_string())
);
}
}
}
Err(e) => {
println!("Request error: {}", e);
}
}
println!("Hash match: {}", hash_match);
}