Skip to content

Commit

Permalink
provide a common ErrorResponses type and convinient other abstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed May 2, 2024
1 parent bab8153 commit f682bc1
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 157 deletions.
18 changes: 11 additions & 7 deletions catalyst-gateway/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions catalyst-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ tracing = "0.1.37"
tracing-subscriber = "0.3.16"
serde = "1.0"
serde_json = "1.0"
poem = { git = "https://github.com/input-output-hk/catalyst-poem.git", rev = "0c28d4351e9a7a1081f85ad62cc7bcb8986ba4dc" }
poem-openapi = { git = "https://github.com/input-output-hk/catalyst-poem.git", rev = "0c28d4351e9a7a1081f85ad62cc7bcb8986ba4dc" }
poem = "3.0.0"
poem-openapi = "5.0.0"
prometheus = "0.13.0"
cryptoxide = "0.4.4"
uuid = "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,26 @@ use crate::{
error::NotFoundError,
},
service::common::{
objects::{
cardano::{
network::Network,
slot_info::{Slot, SlotInfo},
},
server_error::ServerError,
objects::cardano::{
network::Network,
slot_info::{Slot, SlotInfo},
},
responses::handle_5xx_response,
responses::WithErrorResponses,
},
state::State,
};

/// All Responses
/// Endpoint responses.
#[derive(ApiResponse)]
pub(crate) enum AllResponses {
pub(crate) enum Responses {
/// Returns the slot info.
#[oai(status = 200)]
Ok(Json<SlotInfo>),
/// Internal Server Error.
///
/// *The contents of this response should be reported to the projects issue tracker.*
#[oai(status = 500)]
ServerError(Json<ServerError>),
/// Service is not ready, do not send other requests.
#[oai(status = 503)]
ServiceUnavailable,
}

/// All responses.
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// # GET `/date_time_to_slot_number`
#[allow(clippy::unused_async)]
pub(crate) async fn endpoint(
Expand Down Expand Up @@ -77,20 +69,21 @@ pub(crate) async fn endpoint(

let current = match process_slot_info_result(current) {
Ok(current) => current,
Err(err) => return handle_5xx_response!(err),
Err(err) => return AllResponses::handle_5xx_response(&err),
};
let previous = match process_slot_info_result(previous) {
Ok(current) => current,
Err(err) => return handle_5xx_response!(err),
Err(err) => return AllResponses::handle_5xx_response(&err),
};
let next = match process_slot_info_result(next) {
Ok(current) => current,
Err(err) => return handle_5xx_response!(err),
Err(err) => return AllResponses::handle_5xx_response(&err),
};

AllResponses::Ok(Json(SlotInfo {
Responses::Ok(Json(SlotInfo {
previous,
current,
next,
}))
.into()
}
41 changes: 15 additions & 26 deletions catalyst-gateway/bin/src/service/api/cardano/registration_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,31 @@ use crate::{
event_db::{cardano::chain_state::SlotNumber, error::NotFoundError},
service::{
common::{
objects::{
cardano::{
network::Network, registration_info::RegistrationInfo,
stake_address::StakeAddress,
},
server_error::ServerError,
validation_error::ValidationError,
objects::cardano::{
network::Network, registration_info::RegistrationInfo, stake_address::StakeAddress,
},
responses::handle_5xx_response,
responses::WithErrorResponses,
},
utilities::check_network,
},
state::State,
};

/// All Responses
/// Endpoint responses
#[derive(ApiResponse)]
pub(crate) enum AllResponses {
pub(crate) enum Responses {
/// Returns the staked ada amount.
#[oai(status = 200)]
Ok(Json<RegistrationInfo>),
/// Content validation error.
#[oai(status = 400)]
ValidationError(Json<ValidationError>),
/// Content not found.
/// Nothing found or no any valid registration found for the provided stake address
/// and provided slot number.
#[oai(status = 404)]
NotFound,
/// Internal Server Error.
///
/// *The contents of this response should be reported to the projects issue tracker.*
#[oai(status = 500)]
ServerError(Json<ServerError>),
/// Service is not ready, do not send other requests.
#[oai(status = 503)]
ServiceUnavailable,
}

/// All responses
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// # GET `/registration`
pub(crate) async fn endpoint(
state: &State, stake_address: StakeAddress, provided_network: Option<Network>,
Expand All @@ -54,7 +42,7 @@ pub(crate) async fn endpoint(
let stake_credential = stake_address.payload().as_hash().to_vec();
let network = match check_network(stake_address.network(), provided_network) {
Ok(network) => network,
Err(err) => return AllResponses::ValidationError(Json(err)),
Err(err) => return err.into(),
};

// get the total utxo amount from the database
Expand All @@ -63,14 +51,15 @@ pub(crate) async fn endpoint(
.await
{
Ok((tx_id, payment_address, voting_info, nonce)) => {
AllResponses::Ok(Json(RegistrationInfo::new(
Responses::Ok(Json(RegistrationInfo::new(
tx_id,
&payment_address,
voting_info,
nonce,
)))
.into()
},
Err(err) if err.is::<NotFoundError>() => AllResponses::NotFound,
Err(err) => handle_5xx_response!(err),
Err(err) if err.is::<NotFoundError>() => Responses::NotFound.into(),
Err(err) => AllResponses::handle_5xx_response(&err),
}
}
51 changes: 14 additions & 37 deletions catalyst-gateway/bin/src/service/api/cardano/staked_ada_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,30 @@ use crate::{
event_db::{cardano::chain_state::SlotNumber, error::NotFoundError},
service::{
common::{
objects::{
cardano::{network::Network, stake_address::StakeAddress, stake_info::StakeInfo},
server_error::ServerError,
validation_error::ValidationError,
objects::cardano::{
network::Network, stake_address::StakeAddress, stake_info::StakeInfo,
},
responses::handle_5xx_response,
responses::WithErrorResponses,
},
utilities::check_network,
},
state::State,
};

/// All Responses
/// Endpoint responses.
#[derive(ApiResponse)]
pub(crate) enum AllResponses {
pub(crate) enum Responses {
/// Returns the staked ada amount.
#[oai(status = 200)]
Ok(Json<StakeInfo>),
/// Content validation error.
#[oai(status = 400)]
ValidationError(Json<ValidationError>),
/// Content not found.
/// Nothing found for the provided stake address and provided slot number.
#[oai(status = 404)]
NotFound,
/// Internal Server Error.
///
/// *The contents of this response should be reported to the projects issue tracker.*
#[oai(status = 500)]
ServerError(Json<ServerError>),
/// Service is not ready, do not send other requests.
#[oai(status = 503)]
ServiceUnavailable,
}

/// All responses.
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// # GET `/staked_ada`
pub(crate) async fn endpoint(
state: &State, stake_address: StakeAddress, provided_network: Option<Network>,
Expand All @@ -52,7 +42,7 @@ pub(crate) async fn endpoint(

let network = match check_network(stake_address.network(), provided_network) {
Ok(network) => network,
Err(err) => return AllResponses::ValidationError(Json(err)),
Err(err) => return err.into(),
};

// get the total utxo amount from the database
Expand All @@ -61,26 +51,13 @@ pub(crate) async fn endpoint(
.await
{
Ok((amount, slot_number)) => {
AllResponses::Ok(Json(StakeInfo {
Responses::Ok(Json(StakeInfo {
amount,
slot_number,
}))
.into()
},
Err(err) if err.is::<NotFoundError>() => AllResponses::NotFound,
Err(err) => handle_5xx_response!(err),
Err(err) if err.is::<NotFoundError>() => Responses::NotFound.into(),
Err(err) => AllResponses::handle_5xx_response(&err),
}
}

#[test]
fn test() {
use poem::IntoResponse;
let meta = AllResponses::meta();
println!("meta: {meta:?}",);
let resp = AllResponses::ValidationError(Json(ValidationError {
message: "some".to_string(),
}))
.into_response();
println!("resp: {resp:?}");
let resp = "some".to_string().into_response();
println!("resp: {resp:?}");
}
31 changes: 12 additions & 19 deletions catalyst-gateway/bin/src/service/api/cardano/sync_state_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,26 @@ use poem_openapi::{payload::Json, ApiResponse};
use crate::{
event_db::error::NotFoundError,
service::common::{
objects::{
cardano::{network::Network, sync_state::SyncState},
server_error::ServerError,
},
responses::handle_5xx_response,
objects::cardano::{network::Network, sync_state::SyncState},
responses::WithErrorResponses,
},
state::State,
};

/// # All Responses
/// Endpoint responses.
#[derive(ApiResponse)]
pub(crate) enum AllResponses {
pub(crate) enum Responses {
/// Returns the sync state.
#[oai(status = 200)]
Ok(Json<SyncState>),
/// Content not found.
/// Cardano chain follower was not syncing for the provided network.
#[oai(status = 404)]
NotFound,
/// Internal Server Error.
///
/// *The contents of this response should be reported to the projects issue tracker.*
#[oai(status = 500)]
ServerError(Json<ServerError>),
/// Service is not ready, do not send other requests.
#[oai(status = 503)]
ServiceUnavailable,
}

/// All responses.
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// # GET `/sync_state`
#[allow(clippy::unused_async)]
pub(crate) async fn endpoint(state: &State, network: Option<Network>) -> AllResponses {
Expand All @@ -42,13 +34,14 @@ pub(crate) async fn endpoint(state: &State, network: Option<Network>) -> AllResp

match event_db.last_updated_state(network.into()).await {
Ok((slot_number, block_hash, last_updated)) => {
AllResponses::Ok(Json(SyncState {
Responses::Ok(Json(SyncState {
slot_number,
block_hash: block_hash.into(),
last_updated,
}))
.into()
},
Err(err) if err.is::<NotFoundError>() => AllResponses::NotFound,
Err(err) => handle_5xx_response!(err),
Err(err) if err.is::<NotFoundError>() => Responses::NotFound.into(),
Err(err) => AllResponses::handle_5xx_response(&err),
}
}
11 changes: 8 additions & 3 deletions catalyst-gateway/bin/src/service/api/health/live_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

use poem_openapi::ApiResponse;

/// All responses
use crate::service::common::responses::WithErrorResponses;

/// Endpoint responses.
#[derive(ApiResponse)]
pub(crate) enum AllResponses {
pub(crate) enum Responses {
/// Service is Started and can serve requests.
#[oai(status = 204)]
NoContent,
}

/// All responses.
pub(crate) type AllResponses = WithErrorResponses<Responses>;

/// # GET /health/live
///
/// Liveness endpoint.
Expand All @@ -21,5 +26,5 @@ pub(crate) enum AllResponses {
/// by an endpoint in a short window.
#[allow(clippy::unused_async)]
pub(crate) async fn endpoint() -> AllResponses {
AllResponses::NoContent
Responses::NoContent.into()
}
Loading

0 comments on commit f682bc1

Please sign in to comment.