LCOV - code coverage report
Current view: top level - libs/utils/src/http - json.rs (source / functions) Coverage Total Hit
Test: e402c46de0a007db6b48dddbde450ddbb92e6ceb.info Lines: 0.0 % 41 0
Test Date: 2024-06-25 10:31:23 Functions: 0.0 % 251 0

            Line data    Source code
       1              : use anyhow::Context;
       2              : use bytes::Buf;
       3              : use hyper::{header, Body, Request, Response, StatusCode};
       4              : use serde::{Deserialize, Serialize};
       5              : 
       6              : use super::error::ApiError;
       7              : 
       8            0 : pub async fn json_request<T: for<'de> Deserialize<'de>>(
       9            0 :     request: &mut Request<Body>,
      10            0 : ) -> Result<T, ApiError> {
      11            0 :     json_request_or_empty_body(request)
      12            0 :         .await?
      13            0 :         .context("missing request body")
      14            0 :         .map_err(ApiError::BadRequest)
      15            0 : }
      16              : 
      17              : /// Will be removed as part of <https://github.com/neondatabase/neon/issues/4282>
      18            0 : pub async fn json_request_or_empty_body<T: for<'de> Deserialize<'de>>(
      19            0 :     request: &mut Request<Body>,
      20            0 : ) -> Result<Option<T>, ApiError> {
      21            0 :     let body = hyper::body::aggregate(request.body_mut())
      22            0 :         .await
      23            0 :         .context("Failed to read request body")
      24            0 :         .map_err(ApiError::BadRequest)?;
      25            0 :     if body.remaining() == 0 {
      26            0 :         return Ok(None);
      27            0 :     }
      28            0 : 
      29            0 :     let mut deser = serde_json::de::Deserializer::from_reader(body.reader());
      30            0 : 
      31            0 :     serde_path_to_error::deserialize(&mut deser)
      32            0 :         // intentionally stringify because the debug version is not helpful in python logs
      33            0 :         .map_err(|e| anyhow::anyhow!("Failed to parse json request: {e}"))
      34            0 :         .map(Some)
      35            0 :         .map_err(ApiError::BadRequest)
      36            0 : }
      37              : 
      38            0 : pub fn json_response<T: Serialize>(
      39            0 :     status: StatusCode,
      40            0 :     data: T,
      41            0 : ) -> Result<Response<Body>, ApiError> {
      42            0 :     let json = serde_json::to_string(&data)
      43            0 :         .context("Failed to serialize JSON response")
      44            0 :         .map_err(ApiError::InternalServerError)?;
      45            0 :     let response = Response::builder()
      46            0 :         .status(status)
      47            0 :         .header(header::CONTENT_TYPE, "application/json")
      48            0 :         .body(Body::from(json))
      49            0 :         .map_err(|e| ApiError::InternalServerError(e.into()))?;
      50            0 :     Ok(response)
      51            0 : }
        

Generated by: LCOV version 2.1-beta