Skip to content

Commit

Permalink
0.2.0-rc.1 Integration
Browse files Browse the repository at this point in the history
For a full changelog, please review the following link:
#1
  • Loading branch information
RikkertTheDeveloper committed May 16, 2023
2 parents cf984ae + 94e88d4 commit 701e636
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 122 deletions.
16 changes: 10 additions & 6 deletions Cargo.toml
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
21 changes: 21 additions & 0 deletions LICENSE.txt
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.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ The MultiSafePay Rust SDK makes use of the "Cargo" toolchain included within rus

## Tests
The tests within this project can be ran with the `cargo test` command. This project has built-in tests for all classes associated with the libary.

### Offline testing
This library makes use of mockup data sites such as **DummyJSON** in order to test it's internal parsing tools. If you'd like to fully test offline, you can add the `--offline` flag to your `cargo test` command.
63 changes: 63 additions & 0 deletions src/endpoint.rs
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
}
}
98 changes: 98 additions & 0 deletions src/http.rs
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)
}
}
5 changes: 3 additions & 2 deletions src/lib.rs
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;
13 changes: 0 additions & 13 deletions src/objects/client.rs

This file was deleted.

23 changes: 0 additions & 23 deletions src/objects/endpoint.rs

This file was deleted.

28 changes: 0 additions & 28 deletions src/objects/httpclient.rs

This file was deleted.

3 changes: 0 additions & 3 deletions src/objects/mod.rs

This file was deleted.

9 changes: 0 additions & 9 deletions src/tests/client_tests.rs

This file was deleted.

11 changes: 11 additions & 0 deletions src/tests/endpoint_test.rs
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")
}
19 changes: 0 additions & 19 deletions src/tests/endpoint_tests.rs

This file was deleted.

12 changes: 0 additions & 12 deletions src/tests/http_client_tests.rs

This file was deleted.

12 changes: 12 additions & 0 deletions src/tests/http_test.rs
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());
}
6 changes: 2 additions & 4 deletions src/tests/mod.rs
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;

0 comments on commit 701e636

Please sign in to comment.