Skip to content

Commit

Permalink
Integrate 0.2.4-rc.1
Browse files Browse the repository at this point in the history
For a full changelog, please review the following pull request:
#2
  • Loading branch information
RikkertTheDeveloper committed May 16, 2023
2 parents 701e636 + d1a5281 commit 2413ae0
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multisafepay-rust-sdk"
version = "0.2.1"
name = "multisafepay"
version = "0.2.4"
description="A MultiSafePay SDK for the Rust Programming Language"
edition = "2021"
authors = ["Rick Arendsen"]
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ 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.

## Please Note
As of **16-05-2023**, this repository is still experiencing heavy development. Please expect severe API changes in the future.
65 changes: 65 additions & 0 deletions src/client.rs
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)
}
}
}
18 changes: 15 additions & 3 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::http::HttpMethod;

pub enum EndpointAction {
CreateTransaction,
GetTransaction
}

impl EndpointAction {
Expand All @@ -12,7 +13,11 @@ impl EndpointAction {
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)
Endpoint::new("v1/json/orders".to_owned(), HttpMethod::POST, api_key)
},

Self::GetTransaction => {
Endpoint::new("v1/json/orders".to_owned(), HttpMethod::GET, api_key)
}
}
}
Expand Down Expand Up @@ -57,7 +62,14 @@ impl Endpoint {
&self.method
}

pub fn get_url(&self) -> &str {
&self.url
pub fn get_url(&self, is_debug: bool) -> String {
match is_debug {
true => format!("https://testapi.multisafepay.com/{}", &self.url),
false => format!("https://multisafepay.com/{}", &self.url)
}
}

pub fn get_api_key(&self) -> &str {
&self.api_key
}
}
2 changes: 1 addition & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<'a> HttpRequestBuilder<'a> {
let mut headers = List::new();
headers.append(&format!("api_key: {}", api_key))?;
headers.append("accept: application/json")?;
headers.append("content-type: application/json")?;
headers.append("Content-Type: application/json")?;
self.easy.http_headers(headers)?;
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
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;
2 changes: 1 addition & 1 deletion src/tests/endpoint_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ fn test_endpoint_url() {
.api_key(api_key.to_owned())
.build(EndpointAction::CreateTransaction);

assert_eq!(my_endpoint.get_url(), "https://testapi.multisafepay.com/v1/json/orders")
assert_eq!(my_endpoint.get_url(true), "https://testapi.multisafepay.com/v1/json/orders")
}
6 changes: 4 additions & 2 deletions src/tests/mod.rs
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;
14 changes: 14 additions & 0 deletions src/tests/utils_test.rs
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");
}
27 changes: 27 additions & 0 deletions src/utils.rs
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())
}
}

0 comments on commit 2413ae0

Please sign in to comment.