-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
10 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::{http::{HttpRequestBuilder}, endpoint::{EndpointActionBuilder, EndpointAction}}; | ||
use crate::utils::RestfulUrlBuilder; | ||
|
||
|
||
pub struct ClientBuilder { | ||
api_key: Option<String>, | ||
is_debug: Option<bool>, | ||
} | ||
|
||
impl ClientBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
api_key: None, | ||
is_debug: None, | ||
} | ||
} | ||
|
||
pub fn api_key(mut self, api_key: String, is_debug: bool) -> Self { | ||
self.api_key = Some(api_key); | ||
self.is_debug = Some(is_debug); | ||
|
||
self | ||
} | ||
|
||
pub fn get_order(&self, order_id: String) -> Result<String, curl::Error> { | ||
let retrieval_endpoint = EndpointActionBuilder::new() | ||
.api_key(self.api_key.as_ref().unwrap().to_owned()) | ||
.build(EndpointAction::GetTransaction); | ||
|
||
let restful_url_info = RestfulUrlBuilder::new() | ||
.url(retrieval_endpoint.get_url(self.is_debug.unwrap()).to_owned()) | ||
.parameter(order_id.to_string()) | ||
.format(); | ||
|
||
let http_result = HttpRequestBuilder::new() | ||
.api_key(&self.api_key.as_ref().unwrap().to_owned()) | ||
.url(restful_url_info.as_ref()) | ||
.method(retrieval_endpoint.get_method().to_owned()) | ||
.execute(); | ||
|
||
match http_result { | ||
Ok(data) => Ok(data), | ||
Err(err) => Err(err) | ||
} | ||
} | ||
|
||
pub fn create_order(&self, body: String) -> Result<String, curl::Error> { | ||
let creation_endpoint = EndpointActionBuilder::new() | ||
.api_key(self.api_key.as_ref().unwrap().to_owned()) | ||
.build(EndpointAction::CreateTransaction); | ||
|
||
let formatted_url = creation_endpoint.get_url(self.is_debug.unwrap()); | ||
let http_result = HttpRequestBuilder::new() | ||
.api_key(&self.api_key.as_ref().unwrap().to_owned()) | ||
.payload(body.as_ref()) | ||
.url(formatted_url.as_ref()) | ||
.method(creation_endpoint.get_method().to_owned()) | ||
.execute(); | ||
|
||
match http_result { | ||
Ok(data) => Ok(data), | ||
Err(err) => Err(err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod endpoint; | ||
pub mod http; | ||
pub mod client; | ||
pub mod tests; | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
#[cfg(test)] | ||
|
||
mod endpoint_test; | ||
#[cfg(test)] | ||
|
||
#[cfg(test)] | ||
mod http_test; | ||
|
||
#[cfg(test)] | ||
mod utils_test; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::utils::RestfulUrlBuilder; | ||
|
||
#[test] | ||
fn test_restful_url() { | ||
let restful_base_url = "https://example.com"; | ||
let restful_url_parameter = "1"; | ||
|
||
let new_restful_url = RestfulUrlBuilder::new() | ||
.url(restful_base_url.to_owned()) | ||
.parameter(restful_url_parameter.to_owned()) | ||
.format(); | ||
|
||
assert_eq!(new_restful_url, "https://example.com/1"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pub struct RestfulUrlBuilder { | ||
url: Option<String>, | ||
parameter: Option<String> | ||
} | ||
|
||
impl RestfulUrlBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
url: None, | ||
parameter: None | ||
} | ||
} | ||
|
||
pub fn url(mut self, url: String) -> Self { | ||
self.url = Some(url); | ||
self | ||
} | ||
|
||
pub fn parameter(mut self, parameter: String) -> Self { | ||
self.parameter = Some(parameter); | ||
self | ||
} | ||
|
||
pub fn format(&self) -> String { | ||
format!("{}/{}", self.url.as_ref().unwrap(), self.parameter.as_ref().unwrap()) | ||
} | ||
} |