iota_rest_kv/routes/
kv_store.rs

1// Copyright (c) 2025 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use axum::{body::Body, extract::State, http::StatusCode, response::IntoResponse};
5
6use crate::{errors::ApiError, extractors::ExtractPath, types::SharedKvStoreClient};
7
8/// Retrieves data associated with a given key from the KV store as raw
9/// [`Bytes`](bytes::Bytes).
10///
11/// # Returns
12///
13/// * If the key exists, the data is returned as a [`Bytes`](bytes::Bytes)
14///   stream with a `200 OK` status code.
15/// * If the key does not exist, a `204 No Content` status code is returned with
16///   an empty body.
17/// * If an error occurs while interacting with the KV store, an `500 internal
18///   server error` is returned.
19pub async fn data_as_bytes(
20    ExtractPath(key): ExtractPath,
21    State(kv_store_client): State<SharedKvStoreClient>,
22) -> Result<impl IntoResponse, ApiError> {
23    match kv_store_client.get(key).await {
24        Ok(Some(bytes)) => Ok(bytes.into_response()),
25        Ok(None) => Ok((StatusCode::NO_CONTENT, Body::empty()).into_response()),
26        Err(err) => {
27            tracing::error!("cannot fetch data from kv store: {err}");
28            Err(ApiError::InternalServerError)
29        }
30    }
31}