Skip to content

Commit

Permalink
refactor(router): add openapi spec support for gsm apis (#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
sai-harsha-vardhan authored Nov 16, 2023
1 parent f248fe2 commit 62c9cca
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 18 deletions.
6 changes: 6 additions & 0 deletions crates/api_models/src/events/gsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ impl ApiEventMetric for gsm::GsmDeleteResponse {
Some(ApiEventsType::Gsm)
}
}

impl ApiEventMetric for gsm::GsmResponse {
fn get_api_event_type(&self) -> Option<ApiEventsType> {
Some(ApiEventsType::Gsm)
}
}
32 changes: 24 additions & 8 deletions crates/api_models/src/gsm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::enums;
use utoipa::ToSchema;

#[derive(Debug, serde::Deserialize, serde::Serialize)]
use crate::enums::Connector;

#[derive(Debug, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct GsmCreateRequest {
pub connector: enums::Connector,
pub connector: Connector,
pub flow: String,
pub sub_flow: String,
pub code: String,
Expand All @@ -13,9 +15,9 @@ pub struct GsmCreateRequest {
pub step_up_possible: bool,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct GsmRetrieveRequest {
pub connector: enums::Connector,
pub connector: Connector,
pub flow: String,
pub sub_flow: String,
pub code: String,
Expand All @@ -33,6 +35,7 @@ pub struct GsmRetrieveRequest {
serde::Serialize,
serde::Deserialize,
strum::EnumString,
ToSchema,
)]
#[serde(rename_all = "snake_case")]
#[strum(serialize_all = "snake_case")]
Expand All @@ -43,7 +46,7 @@ pub enum GsmDecision {
DoDefault,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct GsmUpdateRequest {
pub connector: String,
pub flow: String,
Expand All @@ -56,7 +59,7 @@ pub struct GsmUpdateRequest {
pub step_up_possible: Option<bool>,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
#[derive(Debug, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct GsmDeleteRequest {
pub connector: String,
pub flow: String,
Expand All @@ -65,11 +68,24 @@ pub struct GsmDeleteRequest {
pub message: String,
}

#[derive(Debug, serde::Serialize)]
#[derive(Debug, serde::Serialize, ToSchema)]
pub struct GsmDeleteResponse {
pub gsm_rule_delete: bool,
pub connector: String,
pub flow: String,
pub sub_flow: String,
pub code: String,
}

#[derive(serde::Serialize, Debug, ToSchema)]
pub struct GsmResponse {
pub connector: String,
pub flow: String,
pub sub_flow: String,
pub code: String,
pub message: String,
pub status: String,
pub router_error: Option<String>,
pub decision: String,
pub step_up_possible: bool,
}
14 changes: 7 additions & 7 deletions crates/router/src/core/gsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ use crate::{
},
db::gsm::GsmInterface,
services,
types::{self, transformers::ForeignInto},
types::transformers::ForeignInto,
AppState,
};

#[instrument(skip_all)]
pub async fn create_gsm_rule(
state: AppState,
gsm_rule: gsm_api_types::GsmCreateRequest,
) -> RouterResponse<types::GsmResponse> {
) -> RouterResponse<gsm_api_types::GsmResponse> {
let db = state.store.as_ref();
GsmInterface::add_gsm_rule(db, gsm_rule.foreign_into())
.await
.to_duplicate_response(errors::ApiErrorResponse::GenericDuplicateError {
message: "GSM with given key already exists in our records".to_string(),
})
.map(services::ApplicationResponse::Json)
.map(|gsm| services::ApplicationResponse::Json(gsm.foreign_into()))
}

#[instrument(skip_all)]
pub async fn retrieve_gsm_rule(
state: AppState,
gsm_request: gsm_api_types::GsmRetrieveRequest,
) -> RouterResponse<types::GsmResponse> {
) -> RouterResponse<gsm_api_types::GsmResponse> {
let db = state.store.as_ref();
let gsm_api_types::GsmRetrieveRequest {
connector,
Expand All @@ -46,14 +46,14 @@ pub async fn retrieve_gsm_rule(
.to_not_found_response(errors::ApiErrorResponse::GenericNotFoundError {
message: "GSM with given key does not exist in our records".to_string(),
})
.map(services::ApplicationResponse::Json)
.map(|gsm| services::ApplicationResponse::Json(gsm.foreign_into()))
}

#[instrument(skip_all)]
pub async fn update_gsm_rule(
state: AppState,
gsm_request: gsm_api_types::GsmUpdateRequest,
) -> RouterResponse<types::GsmResponse> {
) -> RouterResponse<gsm_api_types::GsmResponse> {
let db = state.store.as_ref();
let gsm_api_types::GsmUpdateRequest {
connector,
Expand Down Expand Up @@ -85,7 +85,7 @@ pub async fn update_gsm_rule(
message: "GSM with given key does not exist in our records".to_string(),
})
.attach_printable("Failed while updating Gsm rule")
.map(services::ApplicationResponse::Json)
.map(|gsm| services::ApplicationResponse::Json(gsm.foreign_into()))
}

#[instrument(skip_all)]
Expand Down
13 changes: 12 additions & 1 deletion crates/router/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ Never share your secret api keys. Keep them guarded and secure.
crate::routes::payouts::payouts_fulfill,
crate::routes::payouts::payouts_retrieve,
crate::routes::payouts::payouts_update,
crate::routes::payment_link::payment_link_retrieve
crate::routes::payment_link::payment_link_retrieve,
crate::routes::gsm::create_gsm_rule,
crate::routes::gsm::get_gsm_rule,
crate::routes::gsm::update_gsm_rule,
crate::routes::gsm::delete_gsm_rule,
),
components(schemas(
crate::types::api::refunds::RefundRequest,
Expand Down Expand Up @@ -184,6 +188,13 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::admin::PaymentLinkColorSchema,
api_models::disputes::DisputeResponse,
api_models::disputes::DisputeResponsePaymentsRetrieve,
api_models::gsm::GsmCreateRequest,
api_models::gsm::GsmRetrieveRequest,
api_models::gsm::GsmUpdateRequest,
api_models::gsm::GsmDeleteRequest,
api_models::gsm::GsmDeleteResponse,
api_models::gsm::GsmResponse,
api_models::gsm::GsmDecision,
api_models::payments::AddressDetails,
api_models::payments::BankDebitData,
api_models::payments::AliPayQr,
Expand Down
68 changes: 68 additions & 0 deletions crates/router/src/routes/gsm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ use crate::{
services::{api, authentication as auth},
};

/// Gsm - Create
///
/// To create a Gsm Rule
#[utoipa::path(
post,
path = "/gsm",
request_body(
content = GsmCreateRequest,
),
responses(
(status = 200, description = "Gsm created", body = GsmResponse),
(status = 400, description = "Missing Mandatory fields")
),
tag = "Gsm",
operation_id = "Create Gsm Rule",
security(("admin_api_key" = [])),
)]
#[instrument(skip_all, fields(flow = ?Flow::GsmRuleCreate))]
pub async fn create_gsm_rule(
state: web::Data<AppState>,
Expand All @@ -29,6 +46,23 @@ pub async fn create_gsm_rule(
.await
}

/// Gsm - Get
///
/// To get a Gsm Rule
#[utoipa::path(
post,
path = "/gsm/get",
request_body(
content = GsmRetrieveRequest,
),
responses(
(status = 200, description = "Gsm retrieved", body = GsmResponse),
(status = 400, description = "Missing Mandatory fields")
),
tag = "Gsm",
operation_id = "Retrieve Gsm Rule",
security(("admin_api_key" = [])),
)]
#[instrument(skip_all, fields(flow = ?Flow::GsmRuleRetrieve))]
pub async fn get_gsm_rule(
state: web::Data<AppState>,
Expand All @@ -49,6 +83,23 @@ pub async fn get_gsm_rule(
.await
}

/// Gsm - Update
///
/// To update a Gsm Rule
#[utoipa::path(
post,
path = "/gsm/update",
request_body(
content = GsmUpdateRequest,
),
responses(
(status = 200, description = "Gsm updated", body = GsmResponse),
(status = 400, description = "Missing Mandatory fields")
),
tag = "Gsm",
operation_id = "Update Gsm Rule",
security(("admin_api_key" = [])),
)]
#[instrument(skip_all, fields(flow = ?Flow::GsmRuleUpdate))]
pub async fn update_gsm_rule(
state: web::Data<AppState>,
Expand All @@ -70,6 +121,23 @@ pub async fn update_gsm_rule(
.await
}

/// Gsm - Delete
///
/// To delete a Gsm Rule
#[utoipa::path(
post,
path = "/gsm/delete",
request_body(
content = GsmDeleteRequest,
),
responses(
(status = 200, description = "Gsm deleted", body = GsmDeleteResponse),
(status = 400, description = "Missing Mandatory fields")
),
tag = "Gsm",
operation_id = "Delete Gsm Rule",
security(("admin_api_key" = [])),
)]
#[instrument(skip_all, fields(flow = ?Flow::GsmRuleDelete))]
pub async fn delete_gsm_rule(
state: web::Data<AppState>,
Expand Down
2 changes: 0 additions & 2 deletions crates/router/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,5 +1213,3 @@ impl<F1, F2>
}
}
}

pub type GsmResponse = storage::GatewayStatusMap;
16 changes: 16 additions & 0 deletions crates/router/src/types/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,19 @@ impl ForeignFrom<gsm_api_types::GsmCreateRequest> for storage::GatewayStatusMapp
}
}
}

impl ForeignFrom<storage::GatewayStatusMap> for gsm_api_types::GsmResponse {
fn foreign_from(value: storage::GatewayStatusMap) -> Self {
Self {
connector: value.connector.to_string(),
flow: value.flow,
sub_flow: value.sub_flow,
code: value.code,
message: value.message,
decision: value.decision.to_string(),
status: value.status,
router_error: value.router_error,
step_up_possible: value.step_up_possible,
}
}
}
Loading

0 comments on commit 62c9cca

Please sign in to comment.