Skip to content

Commit

Permalink
update put_document impl and get_document impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Jan 13, 2025
1 parent 54f0075 commit 6676eaf
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions catalyst-gateway/bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ workspace = true
cardano-chain-follower = {version = "0.0.6", git = "https://github.com/input-output-hk/catalyst-libs.git", tag="v0.0.10" }
c509-certificate = { version = "0.0.3", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.3" }
rbac-registration = { version = "0.0.2", git = "https://github.com/input-output-hk/catalyst-libs.git", tag = "v0.0.8" }
catalyst-signed-doc = { git = "https://github.com/input-output-hk/catalyst-libs.git", branch = "fix/replace-ulid-with-uuidv7" }


pallas = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
pallas-traverse = { version = "0.30.1", git = "https://github.com/input-output-hk/catalyst-pallas.git", rev = "9b5183c8b90b90fe2cc319d986e933e9518957b3" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ impl FullSignedDoc {
&self.body
}

/// Returns the document raw bytes.
#[allow(dead_code)]
pub(crate) fn raw(&self) -> &Vec<u8> {
&self.raw
}

/// Uploads a `FullSignedDoc` to the event db.
/// Returns `true` if document was added into the db, `false` if it was already added
/// previously.
Expand Down
17 changes: 10 additions & 7 deletions catalyst-gateway/bin/src/service/api/documents/get_document.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
//! Implementation of the GET `/document` endpoint
use poem::Body;
use poem_openapi::ApiResponse;

use crate::service::common::{responses::WithErrorResponses, types::payload::cbor::Cbor};
use crate::{
db::event::{error::NotFoundError, signed_docs::FullSignedDoc},
service::common::{responses::WithErrorResponses, types::payload::cbor::Cbor},
};

/// Endpoint responses.
#[derive(ApiResponse)]
#[allow(dead_code)]
pub(crate) enum Responses {
/// The Document that was requested.
#[oai(status = 200)]
Ok(Cbor<Body>),
Ok(Cbor),
/// The document could not be found.
#[oai(status = 404)]
NotFound,
Expand All @@ -23,8 +25,9 @@ pub(crate) type AllResponses = WithErrorResponses<Responses>;
/// # GET `/document`
#[allow(clippy::unused_async, clippy::no_effect_underscore_binding)]
pub(crate) async fn endpoint(document_id: uuid::Uuid, version: Option<uuid::Uuid>) -> AllResponses {
let _doc = document_id;
let _ver = version;

Responses::NotFound.into()
match FullSignedDoc::retrieve(&document_id, version.as_ref()).await {
Ok(doc) => Responses::Ok(doc.raw().clone().into()).into(),
Err(err) if err.is::<NotFoundError>() => Responses::NotFound.into(),
Err(err) => AllResponses::handle_error(&err),
}
}
19 changes: 18 additions & 1 deletion catalyst-gateway/bin/src/service/api/documents/put_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,24 @@ pub(crate) async fn endpoint(doc_bytes: Vec<u8>) -> AllResponses {
None,
);

match FullSignedDoc::new(doc_body, None, doc_bytes).store().await {
let payload = if doc.doc_content().is_json() {
match serde_json::from_slice(doc.doc_content().bytes()) {
Ok(payload) => Some(payload),
Err(e) => {
return Responses::BadRequest(Json(PutDocumentBadRequest::new(format!(
"Ivalid Document Content, not Json encoded: {e}"
))))
.into()
},
}
} else {
None
};

match FullSignedDoc::new(doc_body, payload, doc_bytes)
.store()
.await
{
Ok(true) => Responses::Created.into(),
Ok(false) => Responses::NoContent.into(),
Err(err) => AllResponses::handle_error(&err),
Expand Down
6 changes: 6 additions & 0 deletions catalyst-gateway/bin/src/service/common/types/payload/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ use poem_openapi::{
#[derive(Debug)]
pub struct Cbor(Body);

impl From<Vec<u8>> for Cbor {
fn from(value: Vec<u8>) -> Self {
Self(value.into())
}
}

impl Cbor {
/// Returns an inner bytes with the limit
pub(crate) async fn into_bytes_with_limit(
Expand Down

0 comments on commit 6676eaf

Please sign in to comment.