cat_gateway/service/api/documents/
get_document.rs

1//! Implementation of the GET `/document` endpoint
2
3use poem_openapi::ApiResponse;
4
5use super::common;
6use crate::{
7    db::event::error::NotFoundError,
8    service::common::{responses::WithErrorResponses, types::payload::cbor::Cbor},
9};
10
11/// Endpoint responses.
12#[derive(ApiResponse)]
13pub(crate) enum Responses {
14    /// ## OK
15    ///
16    /// The Document that was requested.
17    #[oai(status = 200)]
18    Ok(Cbor<Vec<u8>>),
19    /// ## Not Found
20    ///
21    /// The document could not be found.
22    #[oai(status = 404)]
23    NotFound,
24}
25
26/// All responses.
27pub(crate) type AllResponses = WithErrorResponses<Responses>;
28
29/// # GET `/document`
30pub(crate) async fn endpoint(
31    document_id: uuid::Uuid,
32    version: Option<uuid::Uuid>,
33) -> AllResponses {
34    match common::get_document_cbor_bytes(&document_id, version.as_ref()).await {
35        Ok(doc_cbor_bytes) => Responses::Ok(Cbor(doc_cbor_bytes)).into(),
36        Err(err) if err.is::<NotFoundError>() => Responses::NotFound.into(),
37        Err(err) => AllResponses::handle_error(&err),
38    }
39}