Skip to content

Commit

Permalink
fetch & fetchpost: add JSON response validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Apr 6, 2024
1 parent 4dc459e commit ebc7287
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 20 deletions.
35 changes: 25 additions & 10 deletions src/cmd/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,8 @@ fn get_response(
let mut final_value = String::new();
let mut api_status;
let mut api_respheader = HeaderMap::new();
let mut api_value = String::new();
let mut api_value_json_result: Result<serde_json::Value, serde_json::Error>;

let debug_flag = log_enabled!(Debug);

Expand All @@ -1230,7 +1232,7 @@ fn get_response(
// debug!("{resp:?}");
api_respheader.clone_from(resp.headers());
api_status = resp.status();
let api_value: String = resp.text().unwrap_or_default();
api_value = resp.text().unwrap_or_default();

if api_status.is_client_error() || api_status.is_server_error() {
error_flag = true;
Expand Down Expand Up @@ -1270,16 +1272,29 @@ fn get_response(
error_flag = true;
},
}
} else if flag_pretty {
let api_value_json: serde_json::Value =
serde_json::from_str(&api_value).unwrap();
if let Ok(pretty_json) = serde_json::to_string_pretty(&api_value_json) {
final_value = pretty_json;
} else {
final_value = api_value;
}
} else {
final_value = api_value;
// validate the JSON response
api_value_json_result = serde_json::from_str::<serde_json::Value>(&api_value);
match api_value_json_result {
Ok(api_value_json) => {
if flag_pretty {
final_value = format!("{api_value_json:#?}");
} else {
// use serde_json CompactFormatter to minify the JSON
final_value = format!("{api_value_json}");
}
},
Err(e) => {
error!("json error. json: {api_value:?}, error: {e:?}");

if flag_store_error {
final_value = e.to_string();
} else {
final_value = String::new();
}
error_flag = true;
},
}
}
}
} else {
Expand Down
35 changes: 25 additions & 10 deletions src/cmd/fetchpost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,8 @@ fn get_response(
let mut final_value = String::new();
let mut api_status;
let mut api_respheader = HeaderMap::new();
let mut api_value = String::new();
let mut api_value_json_result: Result<serde_json::Value, serde_json::Error>;

let debug_flag = log_enabled!(Debug);

Expand Down Expand Up @@ -1173,7 +1175,7 @@ fn get_response(
// debug!("{resp:?}");
api_respheader.clone_from(resp.headers());
api_status = resp.status();
let api_value: String = resp.text().unwrap_or_default();
api_value = resp.text().unwrap_or_default();

if api_status.is_client_error() || api_status.is_server_error() {
error_flag = true;
Expand Down Expand Up @@ -1213,16 +1215,29 @@ fn get_response(
error_flag = true;
},
}
} else if flag_pretty {
let api_value_json: serde_json::Value =
serde_json::from_str(&api_value).unwrap();
if let Ok(pretty_json) = serde_json::to_string_pretty(&api_value_json) {
final_value = pretty_json;
} else {
final_value = api_value;
}
} else {
final_value = api_value;
// validate the JSON response
api_value_json_result = serde_json::from_str::<serde_json::Value>(&api_value);
match api_value_json_result {
Ok(api_value_json) => {
if flag_pretty {
final_value = format!("{api_value_json:#?}");
} else {
// use serde_json CompactFormatter to minify the JSON
final_value = format!("{api_value_json}");
}
},
Err(e) => {
error!("json error. json: {api_value:?}, error: {e:?}");

if flag_store_error {
final_value = e.to_string();
} else {
final_value = String::new();
}
error_flag = true;
},
}
}
}
} else {
Expand Down

0 comments on commit ebc7287

Please sign in to comment.