-
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
16 changed files
with
220 additions
and
122 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,27 @@ | ||
[package] | ||
name = "multisafepay-rust-sdk" | ||
version = "0.1.0" | ||
version = "0.2.1" | ||
description="A MultiSafePay SDK for the Rust Programming Language" | ||
edition = "2021" | ||
authors = ["Rick Arendsen"] | ||
readme = "README.md" | ||
repository = "https://github.com/rikkertthedeveloper/multisafepay-rust-sdk" | ||
keywords = ["multisafepay", "ecommerce"] | ||
license-file = "LICENSE.txt" | ||
categories = ["sdk", "multisafepay"] | ||
documentation = "https://docs.rs/multisafepay-rust-sdk" | ||
|
||
[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 | ||
lto = true | ||
codegen-units = 1 | ||
|
||
[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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Rick Arendsen | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,63 @@ | ||
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,3 @@ | ||
mod objects; | ||
mod tests; | ||
pub mod endpoint; | ||
pub mod http; | ||
pub mod tests; |
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 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,11 @@ | ||
use crate::endpoint::{EndpointActionBuilder, EndpointAction}; | ||
|
||
#[test] | ||
fn test_endpoint_url() { | ||
let api_key = "testing-api_key"; | ||
let my_endpoint = EndpointActionBuilder::new() | ||
.api_key(api_key.to_owned()) | ||
.build(EndpointAction::CreateTransaction); | ||
|
||
assert_eq!(my_endpoint.get_url(), "https://testapi.multisafepay.com/v1/json/orders") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
use crate::http::{HttpMethod, HttpRequestBuilder}; | ||
|
||
#[test] | ||
fn test_endpoint_url() { | ||
let http_result = HttpRequestBuilder::new() | ||
.api_key("my-api-key") | ||
.url("https://example.com") | ||
.method(HttpMethod::GET) | ||
.execute(); | ||
|
||
assert!(http_result.is_ok()); | ||
} |
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,8 +1,6 @@ | ||
#[cfg(test)] | ||
mod client_tests; | ||
|
||
mod endpoint_test; | ||
#[cfg(test)] | ||
mod endpoint_tests; | ||
|
||
#[cfg(test)] | ||
mod http_client_tests; | ||
mod http_test; |