Skip to content

Commit

Permalink
Merge branch 'main' into feature/poc-flutter-integration-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
minikin committed Nov 6, 2023
2 parents 2ce9f7a + 3e2efa7 commit 475aeac
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.recommended.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"Earthfile": "earthfile"
},
"rust-analyzer.linkedProjects": [
"./catalyst-gateway/bin/Cargo.toml"
"./catalyst-gateway/Cargo.toml"
],
"rust-analyzer.check.overrideCommand": [
"bash",
Expand Down
38 changes: 0 additions & 38 deletions catalyst-gateway/bin/Earthfile

This file was deleted.

6 changes: 4 additions & 2 deletions catalyst-gateway/bin/src/service/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ use poem_openapi::{ContactObject, LicenseObject, OpenApiService, ServerObject};
use registration::RegistrationApi;
use test_endpoints::TestApi;
use v0::V0Api;
use v1::V1Api;

use crate::settings::API_URL_PREFIX;

mod health;
mod registration;
mod test_endpoints;
mod v0;
mod v1;

/// The name of the API
const API_TITLE: &str = "Catalyst Data Service";
Expand Down Expand Up @@ -61,9 +63,9 @@ const TERMS_OF_SERVICE: &str =
/// Create the `OpenAPI` definition
pub(crate) fn mk_api(
hosts: Vec<String>,
) -> OpenApiService<(TestApi, HealthApi, V0Api, RegistrationApi), ()> {
) -> OpenApiService<(TestApi, HealthApi, RegistrationApi, V0Api, V1Api), ()> {
let mut service = OpenApiService::new(
(TestApi, HealthApi, V0Api, RegistrationApi),
(TestApi, HealthApi, RegistrationApi, V0Api, V1Api),
API_TITLE,
API_VERSION,
)
Expand Down
5 changes: 1 addition & 4 deletions catalyst-gateway/bin/src/service/api/v0/message_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ pub(crate) type AllResponses = response! {
///
/// Message post endpoint.
///
/// So, it will always just return 200.
///
/// In theory it can also return 503 is the service has some startup processing
/// to complete before it is ready to serve requests.
/// When successful, returns a summary of fragments accepted and rejected.
///
/// ## Responses
///
Expand Down
10 changes: 3 additions & 7 deletions catalyst-gateway/bin/src/service/api/v0/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! `v0` Endpoints
use poem_openapi::{payload::Binary, OpenApi};

use crate::service::common::tags::ApiTags;
Expand All @@ -8,14 +9,9 @@ mod message_post;
/// `v0` API Endpoints
pub(crate) struct V0Api;

#[OpenApi(prefix_path = "/v0")]
#[OpenApi(prefix_path = "/v0", tag = "ApiTags::V0")]
impl V0Api {
#[oai(
path = "/message",
method = "post",
operation_id = "Message",
tag = "ApiTags::Message"
)]
#[oai(path = "/message", method = "post", operation_id = "Message")]
/// Posts a signed transaction.
async fn message_post(&self, message: Binary<Vec<u8>>) -> message_post::AllResponses {
message_post::endpoint(message).await
Expand Down
46 changes: 46 additions & 0 deletions catalyst-gateway/bin/src/service/api/v1/account_votes_get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! Implementation of the `GET /v1/votes/plan/account-votes/:account_id` endpoint
use std::sync::Arc;

use poem::web::{Data, Path};
use poem_extensions::{response, UniResponse::T200};
use poem_openapi::payload::Json;

use crate::{
service::common::{
objects::account_votes::{AccountId, AccountVote},
responses::{
resp_2xx::OK,
resp_5xx::{ServerError, ServiceUnavailable},
},
},
state::State,
};

/// All responses
pub(crate) type AllResponses = response! {
200: OK<Json<Vec<AccountVote>>>,
500: ServerError,
503: ServiceUnavailable,
};

#[allow(clippy::unused_async)]
/// GET /v1/votes/plans/account-votes/:account_id
///
/// Get votes for an `account_id` endpoint.
///
/// For each active vote plan, this endpoint returns an array
/// with the proposal index number that the account voted for.
///
/// ## Responses
///
/// * 200 with a JSON array of the number of voted proposals in a plan.
/// * 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.
pub(crate) async fn endpoint(
_state: Data<&Arc<State>>, _account_id: Path<AccountId>,
) -> AllResponses {
// otherwise everything seems to be A-OK
T200(OK(Json(Vec::new())))
}
32 changes: 32 additions & 0 deletions catalyst-gateway/bin/src/service/api/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! `v1` Endpoints
use std::sync::Arc;

use poem::web::{Data, Path};
use poem_openapi::OpenApi;

use crate::{
service::common::{objects::account_votes::AccountId, tags::ApiTags},
state::State,
};

mod account_votes_get;

/// V1 API Endpoints
pub(crate) struct V1Api;

#[OpenApi(prefix_path = "/v1", tag = "ApiTags::V1")]
impl V1Api {
#[oai(
path = "/votes/plan/account-votes/:account_id",
method = "get",
operation_id = "AccountVotes"
)]
/// Get from all active vote plans, the index of the voted proposals
/// by th given account ID.
async fn get_account_votes(
&self, state: Data<&Arc<State>>, account_id: Path<AccountId>,
) -> account_votes_get::AllResponses {
account_votes_get::endpoint(state, account_id).await
}
}
48 changes: 48 additions & 0 deletions catalyst-gateway/bin/src/service/common/objects/account_votes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Define the Account Votes.
use poem_openapi::{types::Example, NewType, Object};
use serde::Deserialize;

#[derive(NewType, Deserialize)]
#[oai(example = true)]
/// Unique ID of a user account.
pub(crate) struct AccountId(
#[oai(validator(max_length = 64, min_length = 64, pattern = "[0-9a-f]{64}"))] String,
);

impl Example for AccountId {
fn example() -> Self {
Self("a6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d1663".into())
}
}

#[derive(NewType, Deserialize)]
#[oai(example = true)]
/// Unique ID of a vote plan.
pub(crate) struct VotePlanId(String);

impl Example for VotePlanId {
fn example() -> Self {
Self("a6a3c0447aeb9cc54cf6422ba32b294e5e1c3ef6d782f2acff4a70694c4d1663".into())
}
}

#[derive(Object, Deserialize)]
#[oai(example = true)]
/// Indexes of a proposal that the account has voted for across all active vote plans.
pub(crate) struct AccountVote {
#[oai(validator(max_length = 64, min_length = 64, pattern = "[0-9a-f]{64}"))]
/// The hex-encoded ID of the vote plan.
pub(crate) vote_plan_id: VotePlanId,
/// Array of the proposal numbers voted for by the account ID within the vote plan.
pub(crate) votes: Vec<u8>,
}

impl Example for AccountVote {
fn example() -> Self {
Self {
vote_plan_id: VotePlanId::example(),
votes: vec![1, 3, 9, 123],
}
}
}
1 change: 1 addition & 0 deletions catalyst-gateway/bin/src/service/common/objects/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This module contains common and re-usable objects.
pub(crate) mod account_votes;
pub(crate) mod delegate_public_key;
pub(crate) mod event_id;
pub(crate) mod fragments_processing_summary;
Expand Down
8 changes: 4 additions & 4 deletions catalyst-gateway/bin/src/service/common/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ pub(crate) enum ApiTags {
Health,
/// Information relating to Voter Registration, Delegations and Calculated Voting
/// Power.
/// Message Endpoints
Message,
/// Information relating to Voter Registration, Delegations and Calculated Voting
/// Power.
Registration,
/// Test Endpoints (Not part of the API)
Test,
/// Test Endpoints (Not part of the API)
TestTag2,
/// API Version 0 Endpoints
V0,
/// API Version 1 Endpoints
V1,
}
51 changes: 0 additions & 51 deletions catalyst-gateway/event-db/Earthfile

This file was deleted.

0 comments on commit 475aeac

Please sign in to comment.