Skip to content

Commit

Permalink
[0.2.0-rc.1] - Add Builder Pattern, Clean Files
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkertTheDeveloper committed May 16, 2023
1 parent cf984ae commit 32cdd40
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 123 deletions.
10 changes: 4 additions & 6 deletions Cargo.toml
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
65 changes: 65 additions & 0 deletions src/endpoint.rs
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
}
}
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)
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
mod objects;
mod tests;
pub mod endpoint;
pub mod http;
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.

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.

8 changes: 0 additions & 8 deletions src/tests/mod.rs

This file was deleted.

0 comments on commit 32cdd40

Please sign in to comment.