feat: libpaket utils APIChange detection

This commit is contained in:
jane400 2024-09-17 11:52:46 +02:00 committed by jane400
parent b7ca6d9b27
commit ec5896df52

View file

@ -179,15 +179,23 @@ macro_rules! request_json {
macro_rules! parse_json_response { macro_rules! parse_json_response {
($res: expr, $type: ty) => {{ ($res: expr, $type: ty) => {{
let res = $res.text().await.unwrap(); let status = $res.status();
let res: String = $res.text().await.unwrap();
// Catch HTML Response early
if status == 200 {
let res = res.clone();
let res = res.trim();
if res.starts_with("<!DOCTYPE html>") {
println!("got html, exptected json with type {}", stringify!($type));
return Err(crate::LibraryError::APIChange);
}
}
let jd = &mut serde_json::Deserializer::from_str(res.as_str()); let jd = &mut serde_json::Deserializer::from_str(res.as_str());
let mut unused = std::collections::BTreeSet::new(); let mut unused = std::collections::BTreeSet::new();
println!("res({}): {}", stringify!($type), res);
let res: Result<$type, _> = serde_ignored::deserialize(jd, |path| { let res: Result<$type, _> = serde_ignored::deserialize(jd, |path| {
unused.insert(path.to_string()); unused.insert(path.to_string());
}); });
println!("res({}): {:?}", stringify!($type), unused);
let res: $type = match res { let res: $type = match res {
Ok(res) => res, Ok(res) => res,
@ -200,15 +208,23 @@ macro_rules! parse_json_response {
macro_rules! parse_json_response_from_apiresult { macro_rules! parse_json_response_from_apiresult {
($res: expr, $type: ty) => {{ ($res: expr, $type: ty) => {{
let res = $res.text().await.unwrap(); let status = $res.status();
let res: String = $res.text().await.unwrap();
// Catch HTML Response early
if status == 200 {
let res = res.clone();
let res = res.trim();
if res.starts_with("<!DOCTYPE html>") {
println!("got html, exptected json with type {}", stringify!($type));
return Err(crate::LibraryError::APIChange);
}
}
let jd = &mut serde_json::Deserializer::from_str(res.as_str()); let jd = &mut serde_json::Deserializer::from_str(res.as_str());
let mut unused = std::collections::BTreeSet::new(); let mut unused = std::collections::BTreeSet::new();
println!("res({}): {}", stringify!($type), res);
let res: Result<APIResult<$type>, _> = serde_ignored::deserialize(jd, |path| { let res: Result<APIResult<$type>, _> = serde_ignored::deserialize(jd, |path| {
unused.insert(path.to_string()); unused.insert(path.to_string());
}); });
println!("res({}): {:?}", stringify!($type), unused);
let res: LibraryResult<$type> = match res { let res: LibraryResult<$type> = match res {
Ok(res) => res.into(), Ok(res) => res.into(),