From b9f2aff5969072c838efa5e2f68690b698e203a2 Mon Sep 17 00:00:00 2001 From: Alex Pozhylenkov Date: Thu, 16 Nov 2023 21:58:25 +0200 Subject: [PATCH] feat: Add blank implementation of the `/api/v0/vote/active/plans` endpoint (#134) * add new endpoint * add VotePlan object * update * fix fmt * fix spell check * remove unused fields --------- Co-authored-by: Steven Johnson --- .../bin/src/service/api/v0/mod.rs | 18 +++++++- .../bin/src/service/api/v0/plans_get.rs | 41 +++++++++++++++++++ .../src/service/api/v1/account_votes_get.rs | 2 +- .../bin/src/service/common/objects/mod.rs | 1 + .../src/service/common/objects/vote_plan.rs | 26 ++++++++++++ 5 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 catalyst-gateway/bin/src/service/api/v0/plans_get.rs create mode 100644 catalyst-gateway/bin/src/service/common/objects/vote_plan.rs diff --git a/catalyst-gateway/bin/src/service/api/v0/mod.rs b/catalyst-gateway/bin/src/service/api/v0/mod.rs index afed0f0f649..4c890347279 100644 --- a/catalyst-gateway/bin/src/service/api/v0/mod.rs +++ b/catalyst-gateway/bin/src/service/api/v0/mod.rs @@ -1,21 +1,35 @@ //! `v0` Endpoints +use std::sync::Arc; + +use poem::web::Data; use poem_openapi::{payload::Binary, OpenApi}; -use crate::service::common::tags::ApiTags; +use crate::{service::common::tags::ApiTags, state::State}; mod message_post; +mod plans_get; /// `v0` API Endpoints pub(crate) struct V0Api; #[OpenApi(prefix_path = "/v0", tag = "ApiTags::V0")] impl V0Api { - #[oai(path = "/message", method = "post", operation_id = "Message")] /// Posts a signed transaction. + #[oai(path = "/message", method = "post", operation_id = "Message")] async fn message_post(&self, message: Binary>) -> message_post::AllResponses { message_post::endpoint(message).await } + + /// Get all active vote plans endpoint. + #[oai( + path = "/vote/active/plans", + method = "get", + operation_id = "GetActivePlans" + )] + async fn plans_get(&self, state: Data<&Arc>) -> plans_get::AllResponses { + plans_get::endpoint(state).await + } } // Poem Tests cause license violation... diff --git a/catalyst-gateway/bin/src/service/api/v0/plans_get.rs b/catalyst-gateway/bin/src/service/api/v0/plans_get.rs new file mode 100644 index 00000000000..de58a7006f5 --- /dev/null +++ b/catalyst-gateway/bin/src/service/api/v0/plans_get.rs @@ -0,0 +1,41 @@ +//! Implementation of the GET /vote/active/plans endpoint + +use std::sync::Arc; + +use poem::web::Data; +use poem_extensions::{response, UniResponse::T200}; +use poem_openapi::payload::Json; + +use crate::{ + service::common::{ + objects::vote_plan::VotePlan, + responses::{ + resp_2xx::OK, + resp_5xx::{ServerError, ServiceUnavailable}, + }, + }, + state::State, +}; + +/// All responses +pub(crate) type AllResponses = response! { + 200: OK>>, + 500: ServerError, + 503: ServiceUnavailable, +}; + +/// GET /v0/vote/active/plans +/// +/// Get all active vote plans endpoint. +/// +/// ## Responses +/// +/// * 200 with a JSON array with the list of vote plans with their respective data. +/// * 500 Server Error - If anything within this function fails unexpectedly. (Possible +/// but unlikely) +/// * 503 Service Unavailable - Service has not started, do not send other requests. +#[allow(clippy::unused_async)] +pub(crate) async fn endpoint(_state: Data<&Arc>) -> AllResponses { + // otherwise everything seems to be A-OK + T200(OK(Json(Vec::new()))) +} diff --git a/catalyst-gateway/bin/src/service/api/v1/account_votes_get.rs b/catalyst-gateway/bin/src/service/api/v1/account_votes_get.rs index fc35df361e2..84e8202b817 100644 --- a/catalyst-gateway/bin/src/service/api/v1/account_votes_get.rs +++ b/catalyst-gateway/bin/src/service/api/v1/account_votes_get.rs @@ -24,7 +24,6 @@ pub(crate) type AllResponses = response! { 503: ServiceUnavailable, }; -#[allow(clippy::unused_async)] /// GET /v1/votes/plans/account-votes/:account_id /// /// Get votes for an `account_id` endpoint. @@ -38,6 +37,7 @@ pub(crate) type AllResponses = response! { /// * 500 Server Error - If anything within this function fails unexpectedly. (Possible /// but unlikely) /// * 503 Service Unavailable - Service has not started, do not send other requests. +#[allow(clippy::unused_async)] pub(crate) async fn endpoint( _state: Data<&Arc>, _account_id: Path, ) -> AllResponses { diff --git a/catalyst-gateway/bin/src/service/common/objects/mod.rs b/catalyst-gateway/bin/src/service/common/objects/mod.rs index 624d50012c6..e014ae234df 100644 --- a/catalyst-gateway/bin/src/service/common/objects/mod.rs +++ b/catalyst-gateway/bin/src/service/common/objects/mod.rs @@ -4,6 +4,7 @@ pub(crate) mod delegate_public_key; pub(crate) mod event_id; pub(crate) mod fragments_processing_summary; pub(crate) mod stake_public_key; +pub(crate) mod vote_plan; pub(crate) mod voter_group_id; pub(crate) mod voter_info; pub(crate) mod voter_registration; diff --git a/catalyst-gateway/bin/src/service/common/objects/vote_plan.rs b/catalyst-gateway/bin/src/service/common/objects/vote_plan.rs new file mode 100644 index 00000000000..8953b454cf7 --- /dev/null +++ b/catalyst-gateway/bin/src/service/common/objects/vote_plan.rs @@ -0,0 +1,26 @@ +//! Define the Vote Plan + +use poem_openapi::{types::Example, Object}; + +/// Vote Plan +#[derive(Object)] +#[oai(example = true)] +pub(crate) struct VotePlan { + /// Voting token identifier + #[oai(validator( + max_length = 121, + min_length = 59, + pattern = r"[0-9a-f]{56}\.[0-9a-f]{2,64}" + ))] + voting_token: String, +} + +impl Example for VotePlan { + fn example() -> Self { + Self { + voting_token: + "134c2d0a0b5761445d3f2d08492a5c193e3a19194453511426153630.0418401957301613" + .to_string(), + } + } +}