-
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.
[0.2.0-rc.1] - Add Builder Pattern, Clean Files
- Loading branch information
1 parent
cf984ae
commit 32cdd40
Showing
12 changed files
with
169 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
[package] | ||
name = "multisafepay-rust-sdk" | ||
version = "0.1.0" | ||
version = "0.2.0" | ||
edition = "2021" | ||
authors = ["Rick Arendsen"] | ||
readme = "README.md" | ||
repository = "https://github.com/rikkertthedeveloper/multisafepay-rust-sdk" | ||
keywords = ["multisafepay", "ecommerce"] | ||
|
||
[dependencies] | ||
minreq = { version = "2.8.0", features = ["https"] } | ||
|
||
[dev-dependencies] | ||
pretty_assertions = "1" | ||
curl = "0.4.44" | ||
|
||
[profile.release] | ||
panic = "unwind" | ||
panic = "abort" | ||
opt-level = 3 | ||
|
||
[profile.production] | ||
panic = "abort" | ||
inherits = "release" | ||
lto = true | ||
codegen-units = 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,65 @@ | ||
use crate::http::HttpMethod; | ||
|
||
pub enum EndpointAction { | ||
CreateTransaction, | ||
} | ||
|
||
impl EndpointAction { | ||
pub fn builder() -> EndpointActionBuilder { | ||
EndpointActionBuilder::new() | ||
} | ||
|
||
pub fn endpoint(&self, api_key: String) -> Endpoint { | ||
match self { | ||
Self::CreateTransaction => Endpoint::new( | ||
"https://testapi.multisafepay.com/v1/json/orders".to_owned(), | ||
HttpMethod::POST, | ||
api_key, | ||
), | ||
} | ||
} | ||
} | ||
|
||
pub struct EndpointActionBuilder { | ||
api_key: Option<String>, | ||
} | ||
|
||
impl EndpointActionBuilder { | ||
pub fn new() -> Self { | ||
Self { api_key: None } | ||
} | ||
|
||
pub fn api_key(mut self, api_key: String) -> Self { | ||
self.api_key = Some(api_key); | ||
self | ||
} | ||
|
||
pub fn build(self, action: EndpointAction) -> Endpoint { | ||
let api_key = self.api_key.expect("API key not specified"); | ||
action.endpoint(api_key) | ||
} | ||
} | ||
|
||
pub struct Endpoint { | ||
url: String, | ||
method: HttpMethod, | ||
api_key: String, | ||
} | ||
|
||
impl Endpoint { | ||
pub fn new(url: String, method: HttpMethod, api_key: String) -> Self { | ||
Self { | ||
url, | ||
method, | ||
api_key, | ||
} | ||
} | ||
|
||
pub fn get_method(&self) -> &HttpMethod { | ||
&self.method | ||
} | ||
|
||
pub fn get_url(&self) -> &str { | ||
&self.url | ||
} | ||
} |
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,98 @@ | ||
use curl::easy::{Easy, List}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum HttpMethod { | ||
GET, | ||
POST, | ||
PUT, | ||
PATCH, | ||
} | ||
|
||
impl HttpMethod { | ||
fn as_str(&self) -> &'static str { | ||
match self { | ||
HttpMethod::GET => "GET", | ||
HttpMethod::POST => "POST", | ||
HttpMethod::PUT => "PUT", | ||
HttpMethod::PATCH => "PATCH", | ||
} | ||
} | ||
} | ||
|
||
pub struct HttpRequestBuilder<'a> { | ||
easy: Easy, | ||
method: Option<HttpMethod>, | ||
url: Option<&'a str>, | ||
payload: Option<&'a str>, | ||
api_key: Option<&'a str>, | ||
} | ||
|
||
impl<'a> HttpRequestBuilder<'a> { | ||
pub fn new() -> Self { | ||
Self { | ||
easy: Easy::new(), | ||
method: None, | ||
url: None, | ||
payload: None, | ||
api_key: None, | ||
} | ||
} | ||
|
||
pub fn method(mut self, method: HttpMethod) -> Self { | ||
self.method = Some(method); | ||
self | ||
} | ||
|
||
pub fn url(mut self, url: &'a str) -> Self { | ||
self.url = Some(url); | ||
self | ||
} | ||
|
||
pub fn payload(mut self, payload: &'a str) -> Self { | ||
self.payload = Some(payload); | ||
self | ||
} | ||
|
||
pub fn api_key(mut self, api_key: &'a str) -> Self { | ||
self.api_key = Some(api_key); | ||
self | ||
} | ||
|
||
pub fn execute(&mut self) -> Result<String, curl::Error> { | ||
let method = self | ||
.method | ||
.as_ref() | ||
.expect("HTTP method not specified") | ||
.as_str(); | ||
let url = self.url.expect("URL not specified"); | ||
|
||
self.easy.url(url)?; | ||
self.easy.custom_request(method)?; | ||
|
||
if let Some(payload_data) = self.payload { | ||
self.easy.post(true)?; | ||
self.easy.post_fields_copy(payload_data.as_bytes())?; | ||
} | ||
|
||
if let Some(api_key) = self.api_key { | ||
let mut headers = List::new(); | ||
headers.append(&format!("api_key: {}", api_key))?; | ||
headers.append("accept: application/json")?; | ||
headers.append("content-type: application/json")?; | ||
self.easy.http_headers(headers)?; | ||
} | ||
|
||
let mut response_data = Vec::new(); | ||
{ | ||
let mut transfer = self.easy.transfer(); | ||
transfer.write_function(|data| { | ||
response_data.extend_from_slice(data); | ||
Ok(data.len()) | ||
})?; | ||
transfer.perform()?; | ||
} | ||
|
||
let response_string = String::from_utf8_lossy(&response_data).to_string(); | ||
Ok(response_string) | ||
} | ||
} |
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,2 +1,2 @@ | ||
mod objects; | ||
mod tests; | ||
pub mod endpoint; | ||
pub mod http; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.