Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(connector-template): raise errors instead of using todo!() #620

Merged
merged 8 commits into from
Feb 24, 2023
218 changes: 103 additions & 115 deletions connector-template/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
errors::{self, CustomResult},
payments,
},
headers, logger, services::{self, ConnectorIntegration},
headers, services::{self, ConnectorIntegration},
types::{
self,
api::{self, ConnectorCommon, ConnectorCommonExt},
Expand All @@ -24,6 +24,18 @@ use transformers as {{project-name | downcase}};
#[derive(Debug, Clone)]
pub struct {{project-name | downcase | pascal_case}};

impl api::Payment for {{project-name | downcase | pascal_case}} {}
impl api::PaymentSession for {{project-name | downcase | pascal_case}} {}
impl api::ConnectorAccessToken for {{project-name | downcase | pascal_case}} {}
impl api::PreVerify for {{project-name | downcase | pascal_case}} {}
impl api::PaymentAuthorize for {{project-name | downcase | pascal_case}} {}
impl api::PaymentSync for {{project-name | downcase | pascal_case}} {}
impl api::PaymentCapture for {{project-name | downcase | pascal_case}} {}
impl api::PaymentVoid for {{project-name | downcase | pascal_case}} {}
impl api::Refund for {{project-name | downcase | pascal_case}} {}
impl api::RefundExecute for {{project-name | downcase | pascal_case}} {}
impl api::RefundSync for {{project-name | downcase | pascal_case}} {}

impl<Flow, Request, Response> ConnectorCommonExt<Flow, Request, Response> for {{project-name | downcase | pascal_case}}
where
Self: ConnectorIntegration<Flow, Request, Response>,{
Expand All @@ -32,7 +44,7 @@ where
_req: &types::RouterData<Flow, Request, Response>,
_connectors: &settings::Connectors,
) -> CustomResult<Vec<(String, String)>, errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("build_headers method".to_string()).into())
}
}

Expand All @@ -42,8 +54,7 @@ impl ConnectorCommon for {{project-name | downcase | pascal_case}} {
}

fn common_get_content_type(&self) -> &'static str {
todo!()
// Ex: "application/x-www-form-urlencoded"
mime::APPLICATION_JSON.essence_str()
}

fn base_url<'a>(&self, connectors: &'a settings::Connectors) -> &'a str {
Expand All @@ -58,9 +69,21 @@ impl ConnectorCommon for {{project-name | downcase | pascal_case}} {
}
}

impl api::Payment for {{project-name | downcase | pascal_case}} {}
impl
ConnectorIntegration<
api::Session,
types::PaymentsSessionData,
types::PaymentsResponseData,
> for {{project-name | downcase | pascal_case}}
{
//TODO: implement sessions flow
}

impl ConnectorIntegration<api::AccessTokenAuth, types::AccessTokenRequestData, types::AccessToken>
for {{project-name | downcase | pascal_case}}
{
}

impl api::PreVerify for {{project-name | downcase | pascal_case}} {}
impl
ConnectorIntegration<
api::Verify,
Expand All @@ -70,24 +93,69 @@ impl
{
}

impl api::PaymentVoid for {{project-name | downcase | pascal_case}} {}

impl
ConnectorIntegration<
api::Void,
types::PaymentsCancelData,
api::Authorize,
types::PaymentsAuthorizeData,
types::PaymentsResponseData,
> for {{project-name | downcase | pascal_case}}
{}
> for {{project-name | downcase | pascal_case}} {
fn get_headers(&self, req: &types::PaymentsAuthorizeRouterData, connectors: &settings::Connectors,) -> CustomResult<Vec<(String, String)>,errors::ConnectorError> {
self.build_headers(req, connectors)
}

impl api::ConnectorAccessToken for {{project-name | downcase | pascal_case}} {}
fn get_content_type(&self) -> &'static str {
self.common_get_content_type()
}

impl ConnectorIntegration<api::AccessTokenAuth, types::AccessTokenRequestData, types::AccessToken>
for {{project-name | downcase | pascal_case}}
{
fn get_url(&self, _req: &types::PaymentsAuthorizeRouterData, _connectors: &settings::Connectors,) -> CustomResult<String,errors::ConnectorError> {
Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into())
}

fn get_request_body(&self, req: &types::PaymentsAuthorizeRouterData) -> CustomResult<Option<String>,errors::ConnectorError> {
let {{project-name | downcase}}_req =
utils::Encode::<{{project-name | downcase}}::{{project-name | downcase | pascal_case}}PaymentsRequest>::convert_and_encode(req).change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some({{project-name | downcase}}_req))
}

fn build_request(
&self,
req: &types::PaymentsAuthorizeRouterData,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
Ok(Some(
services::RequestBuilder::new()
.method(services::Method::Post)
.url(&types::PaymentsAuthorizeType::get_url(
self, req, connectors,
)?)
.headers(types::PaymentsAuthorizeType::get_headers(
self, req, connectors,
)?)
.body(types::PaymentsAuthorizeType::get_request_body(self, req)?)
.build(),
))
}

fn handle_response(
&self,
data: &types::PaymentsAuthorizeRouterData,
res: Response,
) -> CustomResult<types::PaymentsAuthorizeRouterData,errors::ConnectorError> {
let response: {{project-name | downcase}}::{{project-name | downcase | pascal_case}}PaymentsResponse = res.response.parse_struct("{{project-name | downcase | pascal_case}} PaymentsAuthorizeResponse").change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
}
.try_into()
.change_context(errors::ConnectorError::ResponseHandlingFailed)
}

fn get_error_response(&self, res: Response) -> CustomResult<ErrorResponse,errors::ConnectorError> {
self.build_error_response(res)
}
}

impl api::PaymentSync for {{project-name | downcase | pascal_case}} {}
impl
ConnectorIntegration<api::PSync, types::PaymentsSyncData, types::PaymentsResponseData>
for {{project-name | downcase | pascal_case}}
Expand All @@ -109,7 +177,7 @@ impl
_req: &types::PaymentsSyncRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into())
}

fn build_request(
Expand All @@ -126,22 +194,14 @@ impl
))
}

fn get_error_response(
&self,
res: Response,
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
self.build_error_response(res)
}

fn handle_response(
&self,
data: &types::PaymentsSyncRouterData,
res: Response,
) -> CustomResult<types::PaymentsSyncRouterData, errors::ConnectorError> {
logger::debug!(payment_sync_response=?res);
let response: {{project-name | downcase}}:: {{project-name | downcase | pascal_case}}PaymentsResponse = res
.response
.parse_struct("{{project-name | downcase}} PaymentsResponse")
.parse_struct("{{project-name | downcase}} PaymentsSyncResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::RouterData::try_from(types::ResponseRouterData {
response,
Expand All @@ -150,10 +210,15 @@ impl
})
.change_context(errors::ConnectorError::ResponseHandlingFailed)
}
}

fn get_error_response(
&self,
res: Response,
) -> CustomResult<ErrorResponse, errors::ConnectorError> {
self.build_error_response(res)
}
}

impl api::PaymentCapture for {{project-name | downcase | pascal_case}} {}
impl
ConnectorIntegration<
api::Capture,
Expand All @@ -178,14 +243,14 @@ impl
_req: &types::PaymentsCaptureRouterData,
_connectors: &settings::Connectors,
) -> CustomResult<String, errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into())
}

fn get_request_body(
&self,
_req: &types::PaymentsCaptureRouterData,
) -> CustomResult<Option<String>, errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("get_request_body method".to_string()).into())
}

fn build_request(
Expand All @@ -211,9 +276,8 @@ impl
) -> CustomResult<types::PaymentsCaptureRouterData, errors::ConnectorError> {
let response: {{project-name | downcase }}::{{project-name | downcase | pascal_case}}PaymentsResponse = res
.response
.parse_struct("{{project-name | downcase | pascal_case}} PaymentsResponse")
.parse_struct("{{project-name | downcase | pascal_case}} PaymentsCaptureResponse")
.change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
logger::debug!({{project-name | downcase}}payments_create_response=?response);
types::ResponseRouterData {
response,
data: data.clone(),
Expand All @@ -231,87 +295,13 @@ impl
}
}

impl api::PaymentSession for {{project-name | downcase | pascal_case}} {}

impl
ConnectorIntegration<
api::Session,
types::PaymentsSessionData,
api::Void,
types::PaymentsCancelData,
types::PaymentsResponseData,
> for {{project-name | downcase | pascal_case}}
{
//TODO: implement sessions flow
}

impl api::PaymentAuthorize for {{project-name | downcase | pascal_case}} {}

impl
ConnectorIntegration<
api::Authorize,
types::PaymentsAuthorizeData,
types::PaymentsResponseData,
> for {{project-name | downcase | pascal_case}} {
fn get_headers(&self, req: &types::PaymentsAuthorizeRouterData, connectors: &settings::Connectors,) -> CustomResult<Vec<(String, String)>,errors::ConnectorError> {
self.build_headers(req, connectors)
}

fn get_content_type(&self) -> &'static str {
self.common_get_content_type()
}

fn get_url(&self, _req: &types::PaymentsAuthorizeRouterData, _connectors: &settings::Connectors,) -> CustomResult<String,errors::ConnectorError> {
todo!()
}

fn get_request_body(&self, req: &types::PaymentsAuthorizeRouterData) -> CustomResult<Option<String>,errors::ConnectorError> {
let {{project-name | downcase}}_req =
utils::Encode::<{{project-name | downcase}}::{{project-name | downcase | pascal_case}}PaymentsRequest>::convert_and_encode(req).change_context(errors::ConnectorError::RequestEncodingFailed)?;
Ok(Some({{project-name | downcase}}_req))
}

fn build_request(
&self,
req: &types::PaymentsAuthorizeRouterData,
connectors: &settings::Connectors,
) -> CustomResult<Option<services::Request>, errors::ConnectorError> {
Ok(Some(
services::RequestBuilder::new()
.method(services::Method::Post)
.url(&types::PaymentsAuthorizeType::get_url(
self, req, connectors,
)?)
.headers(types::PaymentsAuthorizeType::get_headers(
self, req, connectors,
)?)
.body(types::PaymentsAuthorizeType::get_request_body(self, req)?)
.build(),
))
}

fn handle_response(
&self,
data: &types::PaymentsAuthorizeRouterData,
res: Response,
) -> CustomResult<types::PaymentsAuthorizeRouterData,errors::ConnectorError> {
let response: {{project-name | downcase}}::{{project-name | downcase | pascal_case}}PaymentsResponse = res.response.parse_struct("PaymentIntentResponse").change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
logger::debug!({{project-name | downcase}}payments_create_response=?response);
types::ResponseRouterData {
response,
data: data.clone(),
http_code: res.status_code,
}
.try_into()
.change_context(errors::ConnectorError::ResponseHandlingFailed)
}

fn get_error_response(&self, res: Response) -> CustomResult<ErrorResponse,errors::ConnectorError> {
self.build_error_response(res)
}
}

impl api::Refund for {{project-name | downcase | pascal_case}} {}
impl api::RefundExecute for {{project-name | downcase | pascal_case}} {}
impl api::RefundSync for {{project-name | downcase | pascal_case}} {}
{}

impl
ConnectorIntegration<
Expand All @@ -328,7 +318,7 @@ impl
}

fn get_url(&self, _req: &types::RefundsRouterData<api::Execute>, _connectors: &settings::Connectors,) -> CustomResult<String,errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into())
}

fn get_request_body(&self, req: &types::RefundsRouterData<api::Execute>) -> CustomResult<Option<String>,errors::ConnectorError> {
Expand All @@ -351,7 +341,6 @@ impl
data: &types::RefundsRouterData<api::Execute>,
res: Response,
) -> CustomResult<types::RefundsRouterData<api::Execute>,errors::ConnectorError> {
logger::debug!(target: "router::connector::{{project-name | downcase}}", response=?res);
let response: {{project-name| downcase}}::RefundResponse = res.response.parse_struct("{{project-name | downcase}} RefundResponse").change_context(errors::ConnectorError::RequestEncodingFailed)?;
types::ResponseRouterData {
response,
Expand All @@ -378,7 +367,7 @@ impl
}

fn get_url(&self, _req: &types::RefundSyncRouterData,_connectors: &settings::Connectors,) -> CustomResult<String,errors::ConnectorError> {
todo!()
Err(errors::ConnectorError::NotImplemented("get_url method".to_string()).into())
}

fn build_request(
Expand All @@ -401,8 +390,7 @@ impl
data: &types::RefundSyncRouterData,
res: Response,
) -> CustomResult<types::RefundSyncRouterData,errors::ConnectorError,> {
logger::debug!(target: "router::connector::{{project-name | downcase}}", response=?res);
let response: {{project-name | downcase}}::RefundResponse = res.response.parse_struct("{{project-name | downcase}} RefundResponse").change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
let response: {{project-name | downcase}}::RefundResponse = res.response.parse_struct("{{project-name | downcase}} RefundSyncResponse").change_context(errors::ConnectorError::ResponseDeserializationFailed)?;
types::ResponseRouterData {
response,
data: data.clone(),
Expand Down
Loading