diff --git a/catalyst-gateway/bin/src/service/api/documents/get_document.rs b/catalyst-gateway/bin/src/service/api/documents/get_document.rs index ebbf95e8f92..f654dfcd804 100644 --- a/catalyst-gateway/bin/src/service/api/documents/get_document.rs +++ b/catalyst-gateway/bin/src/service/api/documents/get_document.rs @@ -15,7 +15,7 @@ pub(crate) enum Responses { /// /// The Document that was requested. #[oai(status = 200)] - Ok(Cbor), + Ok(Cbor>), /// ## Not Found /// /// The document could not be found. @@ -30,7 +30,7 @@ pub(crate) type AllResponses = WithErrorResponses; #[allow(clippy::unused_async, clippy::no_effect_underscore_binding)] pub(crate) async fn endpoint(document_id: uuid::Uuid, version: Option) -> AllResponses { match FullSignedDoc::retrieve(&document_id, version.as_ref()).await { - Ok(doc) => Responses::Ok(doc.raw().clone().into()).into(), + Ok(doc) => Responses::Ok(Cbor(doc.raw().clone())).into(), Err(err) if err.is::() => Responses::NotFound.into(), Err(err) => AllResponses::handle_error(&err), } diff --git a/catalyst-gateway/bin/src/service/api/documents/mod.rs b/catalyst-gateway/bin/src/service/api/documents/mod.rs index bd15c373549..91006011ca3 100644 --- a/catalyst-gateway/bin/src/service/api/documents/mod.rs +++ b/catalyst-gateway/bin/src/service/api/documents/mod.rs @@ -1,7 +1,7 @@ //! Signed Documents API endpoints use anyhow::anyhow; -use poem::error::ReadBodyError; +use poem::{error::ReadBodyError, Body}; use poem_openapi::{ param::{Path, Query}, payload::Json, @@ -76,12 +76,12 @@ impl DocumentApi { )] async fn put_document( &self, /// The document to PUT - document: Cbor, + document: Cbor, /// Authorization required. _auth: NoneOrRBAC, ) -> put_document::AllResponses { - match document.into_bytes_with_limit(MAXIMUM_DOCUMENT_SIZE).await { - Ok(doc_bytes) => put_document::endpoint(doc_bytes).await, + match document.0.into_bytes_limit(MAXIMUM_DOCUMENT_SIZE).await { + Ok(doc_bytes) => put_document::endpoint(doc_bytes.to_vec()).await, Err(ReadBodyError::PayloadTooLarge) => put_document::Responses::PayloadTooLarge.into(), Err(e) => { put_document::Responses::BadRequest(Json(PutDocumentBadRequest::new(&format!( diff --git a/catalyst-gateway/bin/src/service/api/documents/put_document/bad_put_request.rs b/catalyst-gateway/bin/src/service/api/documents/put_document/bad_put_request.rs index fe0c4d219d4..99e0a5d79d5 100644 --- a/catalyst-gateway/bin/src/service/api/documents/put_document/bad_put_request.rs +++ b/catalyst-gateway/bin/src/service/api/documents/put_document/bad_put_request.rs @@ -14,7 +14,7 @@ pub(crate) struct PutDocumentBadRequest { impl PutDocumentBadRequest { /// Create a new instance of `ConfigBadRequest`. - pub(crate) fn new(error: &impl ToString) -> Self { + pub(crate) fn new(error: &(impl ToString + ?Sized)) -> Self { Self { error: error.to_string(), } diff --git a/catalyst-gateway/bin/src/service/api/documents/put_document/mod.rs b/catalyst-gateway/bin/src/service/api/documents/put_document/mod.rs index 0f5793e7719..7301ace0eaf 100644 --- a/catalyst-gateway/bin/src/service/api/documents/put_document/mod.rs +++ b/catalyst-gateway/bin/src/service/api/documents/put_document/mod.rs @@ -48,7 +48,7 @@ pub(crate) type AllResponses = WithErrorResponses; /// # PUT `/document` #[allow(clippy::no_effect_underscore_binding)] pub(crate) async fn endpoint(doc_bytes: Vec) -> AllResponses { - match CatalystSignedDocument::decode(&mut Decoder::new(doc_bytes.as_slice()), &mut ()) { + match CatalystSignedDocument::decode(&mut Decoder::new(&doc_bytes), &mut ()) { Ok(doc) => { let authors = doc .signatures() diff --git a/catalyst-gateway/bin/src/service/common/types/payload/cbor.rs b/catalyst-gateway/bin/src/service/common/types/payload/cbor.rs index f417c764569..67453a71e28 100644 --- a/catalyst-gateway/bin/src/service/common/types/payload/cbor.rs +++ b/catalyst-gateway/bin/src/service/common/types/payload/cbor.rs @@ -1,3 +1,6 @@ +use std::ops::{Deref, DerefMut}; + +use bytes::Bytes; use poem::{Body, FromRequest, IntoResponse, Request, RequestBody, Response, Result}; use poem_openapi::{ impl_apirequest_for_payload, @@ -6,6 +9,12 @@ use poem_openapi::{ ApiResponse, }; +// use crate::{ +// payload::{ParsePayload, Payload}, +// registry::{MetaMediaType, MetaResponse, MetaResponses, MetaSchema, MetaSchemaRef, +// Registry}, ApiResponse, +//}; + /// A cbor binary payload. /// /// # Examples @@ -64,25 +73,24 @@ use poem_openapi::{ /// resp.assert_text("6").await; /// # }); /// ``` -#[derive(Debug)] -pub struct Cbor(Body); +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Cbor(pub T); -impl From> for Cbor { - fn from(value: Vec) -> Self { - Self(value.into()) +impl Deref for Cbor { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 } } -impl Cbor { - /// Returns an inner bytes with the limit - pub(crate) async fn into_bytes_with_limit( - self, limit: usize, - ) -> Result, poem::error::ReadBodyError> { - Ok(self.0.into_bytes_limit(limit).await?.to_vec()) +impl DerefMut for Cbor { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 } } -impl Payload for Cbor { +impl Payload for Cbor { const CONTENT_TYPE: &'static str = "application/cbor"; fn check_content_type(content_type: &str) -> bool { @@ -101,7 +109,23 @@ impl Payload for Cbor { } } -impl ParsePayload for Cbor { +impl ParsePayload for Cbor> { + const IS_REQUIRED: bool = true; + + async fn from_request(request: &Request, body: &mut RequestBody) -> Result { + Ok(Self(>::from_request(request, body).await?)) + } +} + +impl ParsePayload for Cbor { + const IS_REQUIRED: bool = true; + + async fn from_request(request: &Request, body: &mut RequestBody) -> Result { + Ok(Self(Bytes::from_request(request, body).await?)) + } +} + +impl ParsePayload for Cbor { const IS_REQUIRED: bool = true; async fn from_request(request: &Request, body: &mut RequestBody) -> Result { @@ -109,15 +133,15 @@ impl ParsePayload for Cbor { } } -impl IntoResponse for Cbor { +impl + Send> IntoResponse for Cbor { fn into_response(self) -> Response { Response::builder() .content_type(Self::CONTENT_TYPE) - .body(self.0) + .body(self.0.into()) } } -impl ApiResponse for Cbor { +impl + Send> ApiResponse for Cbor { fn meta() -> MetaResponses { MetaResponses { responses: vec![MetaResponse { @@ -135,4 +159,6 @@ impl ApiResponse for Cbor { fn register(_registry: &mut Registry) {} } -impl_apirequest_for_payload!(Cbor); +impl_apirequest_for_payload!(Cbor>); +impl_apirequest_for_payload!(Cbor); +impl_apirequest_for_payload!(Cbor);