diff --git a/src/http/api/v1/credential.rs b/src/http/api/v1/credential.rs index d4af1d8f..81eb0888 100644 --- a/src/http/api/v1/credential.rs +++ b/src/http/api/v1/credential.rs @@ -3,6 +3,7 @@ use axum::{ Json, }; use http::StatusCode; +use serde_json::Value; use crate::{credhelper::CredentialHelper, http::api::APIState}; @@ -24,19 +25,16 @@ pub(crate) async fn update( Json(UpdateRequest { data: UpdateData { token: value }, }): Json, -) -> (StatusCode, String) { +) -> (StatusCode, Json) { match credentials.store(hostname, value).await { - Ok(_) => ( - StatusCode::OK, - serde_json::to_string(&serde_json::json!({ "data": {} })).unwrap(), - ), + Ok(_) => (StatusCode::OK, Json(serde_json::json!({ "data": {} }))), Err(e) => { tracing::error!(reason=?e, "Error occurred updating credential"); ( StatusCode::INTERNAL_SERVER_ERROR, - serde_json::to_string( - &serde_json::json!({ "error": { "msg": "Error occurred updating credential" } }), - ).unwrap(), + Json( + serde_json::json!({ "error": { "msg": "Error occurred updating credential" } }), + ), ) } } @@ -47,20 +45,16 @@ pub(crate) async fn delete( mut credentials, .. }): State>, Path(hostname): Path, -) -> (StatusCode, String) { +) -> (StatusCode, Json) { match credentials.forget(&hostname).await { - Ok(_) => ( - StatusCode::OK, - serde_json::to_string(&serde_json::json!({ "data": {} })).unwrap(), - ), + Ok(_) => (StatusCode::OK, Json(serde_json::json!({ "data": {} }))), Err(e) => { tracing::error!(reason=?e, "Error occurred deleting credential"); ( StatusCode::INTERNAL_SERVER_ERROR, - serde_json::to_string( - &serde_json::json!({ "error": { "msg": "Error occurred deleting credential" } }), - ) - .unwrap(), + Json( + serde_json::json!({ "error": { "msg": "Error occurred deleting credential" } }), + ), ) } } @@ -69,26 +63,25 @@ pub(crate) async fn delete( pub(crate) async fn exists( State(APIState { credentials, .. }): State>, Path(hostname): Path, -) -> (StatusCode, String) { +) -> (StatusCode, Json) { match credentials.get(&hostname).await { Ok(cred) => match cred { crate::credhelper::Credential::Entry(_) => ( StatusCode::OK, - serde_json::to_string(&serde_json::json!({ "data": { "exists": true } })).unwrap(), + Json(serde_json::json!({ "data": { "exists": true } })), ), crate::credhelper::Credential::NotFound => ( StatusCode::NOT_FOUND, - serde_json::to_string(&serde_json::json!({ "data": { "exists": false } })).unwrap(), + Json(serde_json::json!({ "data": { "exists": false } })), ), }, Err(e) => { tracing::error!(reason=?e, "Error occurred checking credential"); ( StatusCode::INTERNAL_SERVER_ERROR, - serde_json::to_string( - &serde_json::json!({ "error": { "msg": "Error occurred checking credential" } }), - ) - .unwrap(), + Json( + serde_json::json!({ "error": { "msg": "Error occurred checking credential" } }), + ), ) } }