Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jan 15, 2025
1 parent ad80f63 commit b64ac3d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) enum Responses {
///
/// The Document that was requested.
#[oai(status = 200)]
Ok(Cbor),
Ok(Cbor<Vec<u8>>),
/// ## Not Found
///
/// The document could not be found.
Expand All @@ -30,7 +30,7 @@ pub(crate) type AllResponses = WithErrorResponses<Responses>;
#[allow(clippy::unused_async, clippy::no_effect_underscore_binding)]
pub(crate) async fn endpoint(document_id: uuid::Uuid, version: Option<uuid::Uuid>) -> 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::<NotFoundError>() => Responses::NotFound.into(),
Err(err) => AllResponses::handle_error(&err),
}
Expand Down
8 changes: 4 additions & 4 deletions catalyst-gateway/bin/src/service/api/documents/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -76,12 +76,12 @@ impl DocumentApi {
)]
async fn put_document(
&self, /// The document to PUT
document: Cbor,
document: Cbor<Body>,
/// 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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) type AllResponses = WithErrorResponses<Responses>;
/// # PUT `/document`
#[allow(clippy::no_effect_underscore_binding)]
pub(crate) async fn endpoint(doc_bytes: Vec<u8>) -> 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()
Expand Down
60 changes: 43 additions & 17 deletions catalyst-gateway/bin/src/service/common/types/payload/cbor.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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<T>(pub T);

impl From<Vec<u8>> for Cbor {
fn from(value: Vec<u8>) -> Self {
Self(value.into())
impl<T> Deref for Cbor<T> {
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<Vec<u8>, poem::error::ReadBodyError> {
Ok(self.0.into_bytes_limit(limit).await?.to_vec())
impl<T> DerefMut for Cbor<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl Payload for Cbor {
impl<T: Send> Payload for Cbor<T> {
const CONTENT_TYPE: &'static str = "application/cbor";

fn check_content_type(content_type: &str) -> bool {
Expand All @@ -101,23 +109,39 @@ impl Payload for Cbor {
}
}

impl ParsePayload for Cbor {
impl ParsePayload for Cbor<Vec<u8>> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
Ok(Self(<Vec<u8>>::from_request(request, body).await?))
}
}

impl ParsePayload for Cbor<Bytes> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
Ok(Self(Bytes::from_request(request, body).await?))
}
}

impl ParsePayload for Cbor<Body> {
const IS_REQUIRED: bool = true;

async fn from_request(request: &Request, body: &mut RequestBody) -> Result<Self> {
Ok(Self(Body::from_request(request, body).await?))
}
}

impl IntoResponse for Cbor {
impl<T: Into<Body> + Send> IntoResponse for Cbor<T> {
fn into_response(self) -> Response {
Response::builder()
.content_type(Self::CONTENT_TYPE)
.body(self.0)
.body(self.0.into())
}
}

impl ApiResponse for Cbor {
impl<T: Into<Body> + Send> ApiResponse for Cbor<T> {
fn meta() -> MetaResponses {
MetaResponses {
responses: vec![MetaResponse {
Expand All @@ -135,4 +159,6 @@ impl ApiResponse for Cbor {
fn register(_registry: &mut Registry) {}
}

impl_apirequest_for_payload!(Cbor);
impl_apirequest_for_payload!(Cbor<Vec<u8>>);
impl_apirequest_for_payload!(Cbor<Bytes>);
impl_apirequest_for_payload!(Cbor<Body>);

0 comments on commit b64ac3d

Please sign in to comment.