aboutsummaryrefslogtreecommitdiff
path: root/src/api/helpers.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/helpers.rs')
-rw-r--r--src/api/helpers.rs10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/api/helpers.rs b/src/api/helpers.rs
index 9fb12dbe..642dbc42 100644
--- a/src/api/helpers.rs
+++ b/src/api/helpers.rs
@@ -1,4 +1,4 @@
-use hyper::{Body, Request};
+use hyper::{Body, Request, Response};
use idna::domain_to_unicode;
use serde::{Deserialize, Serialize};
@@ -144,6 +144,14 @@ pub async fn parse_json_body<T: for<'de> Deserialize<'de>>(req: Request<Body>) -
Ok(resp)
}
+pub fn json_ok_response<T: Serialize>(res: &T) -> Result<Response<Body>, Error> {
+ let resp_json = serde_json::to_string_pretty(res).map_err(garage_util::error::Error::from)?;
+ Ok(Response::builder()
+ .status(hyper::StatusCode::OK)
+ .header(http::header::CONTENT_TYPE, "application/json")
+ .body(Body::from(resp_json))?)
+}
+
#[cfg(test)]
mod tests {
use super::*;