Skip to content

Commit

Permalink
Merge pull request #20 from GordonHind96/master
Browse files Browse the repository at this point in the history
added Status filter to payments list
  • Loading branch information
pgoodjohn authored Dec 12, 2023
2 parents d939d01 + 57a32a1 commit 08cd73d
Show file tree
Hide file tree
Showing 16 changed files with 165 additions and 79 deletions.
2 changes: 1 addition & 1 deletion crates/mollie_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ reqwest = { version = "0.11", features = ["json"] }
lazy_static = { version = "1.4" }

[dev-dependencies]
tokio = { version = "1.35", features = ["full"] }
tokio = { version = "1.35", features = ["full"] }
15 changes: 6 additions & 9 deletions crates/mollie_api/src/api/balances.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Balances API module
//!
//! Used to retrieve information about your balances.
use std::collections::HashMap;
use crate::{
ApiClient,
};
use crate::models::balance::{BalanceResource, BalancesListResource};
use crate::ApiClient;
use std::collections::HashMap;

/// [Balances Api](https://docs.mollie.com/reference/v2/balances-api/overview)
/// Used to retrieve information about a balance.
Expand All @@ -19,11 +17,10 @@ impl<'client> BalancesApi<'client> {
Self { api_client }
}

pub async fn get_by_id(
&self,
balance_id: &String,
) -> crate::Result<BalanceResource> {
self.api_client.get(&format!("/balances/{}", balance_id), None).await
pub async fn get_by_id(&self, balance_id: &String) -> crate::Result<BalanceResource> {
self.api_client
.get(&format!("/balances/{}", balance_id), None)
.await
}

pub async fn list(
Expand Down
4 changes: 2 additions & 2 deletions crates/mollie_api/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod balances;
pub mod organizations;
pub mod payments;
pub mod permissions;
pub mod refunds;
pub mod balances;
pub mod permissions;
19 changes: 14 additions & 5 deletions crates/mollie_api/src/api/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//!
//! Used to retrieve information about current payments//! Payments API module
//!
//! Used to retrieve information about
//! Used to retrieve information about
use crate::{
models::{payment::{PaymentResource, PaymentsListResource, CreatePaymentRequest}},
models::payment::{CreatePaymentRequest, PaymentResource, PaymentsListResource},
ApiClient,
};

Expand All @@ -27,7 +27,13 @@ impl<'client> PaymentsApi<'client> {
}

// [List Payments](https://docs.mollie.com/reference/v2/payments-api/list-payments)
pub async fn list(&self, limit: &Option<i32>, from: &Option<String>, profile_id: &Option<String>, test_mode: &Option<bool>) -> crate::Result<PaymentsListResource> {
pub async fn list(
&self,
limit: &Option<i32>,
from: &Option<String>,
profile_id: &Option<String>,
test_mode: &Option<bool>,
) -> crate::Result<PaymentsListResource> {
let endpoint = "/payments";
let mut params = std::collections::HashMap::new();
if let Some(l) = limit {
Expand All @@ -50,9 +56,12 @@ impl<'client> PaymentsApi<'client> {
}

/// [Create Payment](https://docs.mollie.com/reference/v2/payments-api/create-payment)
pub async fn create_payment(&self, body: &CreatePaymentRequest) -> crate::Result<PaymentResource> {
pub async fn create_payment(
&self,
body: &CreatePaymentRequest,
) -> crate::Result<PaymentResource> {
let endpoint = "/payments";

self.api_client.post(&endpoint, body).await
}

Expand Down
7 changes: 2 additions & 5 deletions crates/mollie_api/src/api/permissions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! Permissions API module
//!
//! Used to retrieve information about current access token permissions
use crate::{
models::permission::PermissionsListResource,
ApiClient,
};
use crate::{models::permission::PermissionsListResource, ApiClient};

/// [Permissions Api](https://docs.mollie.com/reference/v2/permissions-api/overview)
/// Used to retrieve information about an organization.
Expand All @@ -22,4 +19,4 @@ impl<'client> PermissionsApi<'client> {
pub async fn list(&self) -> crate::Result<PermissionsListResource> {
self.api_client.get("/permissions", None).await
}
}
}
8 changes: 6 additions & 2 deletions crates/mollie_api/src/api/refunds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! Used to refund payments
use crate::{
models::{refund::{RefundResource, RefundPaymentRequest}},
models::refund::{RefundPaymentRequest, RefundResource},
ApiClient,
};

Expand All @@ -18,7 +18,11 @@ impl<'client> RefundsApi<'client> {
Self { api_client }
}

pub async fn refund(&self, id: &str, body: &RefundPaymentRequest) -> crate::Result<RefundResource>{
pub async fn refund(
&self,
id: &str,
body: &RefundPaymentRequest,
) -> crate::Result<RefundResource> {
let endpoint = format!("/payments/{}/refunds", id);
let x = self.api_client.post(&endpoint, body).await;
log::debug!("{:?}", x);
Expand Down
9 changes: 5 additions & 4 deletions crates/mollie_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::collections::HashMap;

use api::{organizations, payments, refunds, balances, permissions};
use api::{balances, organizations, payments, permissions, refunds};
use log::{debug, error};
use models::error_response::ErrorResponse;
use reqwest::{header::HeaderMap, Client};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<'a> ApiClient<'a> {
/// Performa a delete request using default headers and auth token
pub async fn delete<R>(&self, endpoint: &str, query: Option<HashMap<&str, String>>) -> Result<R>
where
R: for<'de> Deserialize<'de>
R: for<'de> Deserialize<'de>,
{
let url = self.build_url(endpoint);
let mut req = self.client.delete(url).bearer_auth(self.auth_token);
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'c> Mollie<'c> {
pub fn refunds(&self) -> refunds::RefundsApi {
refunds::RefundsApi::new(&self.api_client)
}

pub fn balances(&self) -> balances::BalancesApi {
balances::BalancesApi::new(&self.api_client)
}
Expand Down Expand Up @@ -224,7 +224,8 @@ mod client_tests {

#[tokio::test]
async fn test_balances_api_authorizes() {
let auth_token = std::env::var("MOLLIE_ACCESS_TOKEN").expect("Please set a valid access token");
let auth_token =
std::env::var("MOLLIE_ACCESS_TOKEN").expect("Please set a valid access token");
let client = Mollie::build(&auth_token);

let balances_response = client.balances().list(None, &None).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/mollie_api/src/models/amount.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use serde::{Serialize, Deserialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Amount {
Expand Down
6 changes: 3 additions & 3 deletions crates/mollie_api/src/models/balance.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};
use crate::models::amount::Amount;
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct BalancesListResource {
pub count: i32,
#[serde(rename(deserialize = "_embedded", serialize="_embedded"))]
#[serde(rename(deserialize = "_embedded", serialize = "_embedded"))]
pub embedded: EmbeddedBalanceResource,
}

Expand Down Expand Up @@ -36,4 +36,4 @@ pub struct TransferDestination {
pub destination_type: String,
pub beneficiary_name: String,
pub bank_account: String,
}
}
6 changes: 3 additions & 3 deletions crates/mollie_api/src/models/payment.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use serde::{Serialize, Deserialize};

use super::{amount::Amount, link::Link};

Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct PaymentsListResource {

#[derive(Debug, Deserialize, Serialize)]
pub struct EmbeddedPayments {
pub payments: Vec<PaymentResource>
pub payments: Vec<PaymentResource>,
}

#[derive(Debug, Serialize, Deserialize)]
Expand All @@ -58,5 +58,5 @@ pub struct CreatePaymentRequest {
pub amount: Amount,
pub description: String,
pub redirect_url: String,
pub profile_id: Option<String>,
pub profile_id: Option<String>,
}
10 changes: 5 additions & 5 deletions crates/mollie_api/src/models/permission.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize};
use super::link::Link;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PermissionResource {
Expand All @@ -9,7 +9,7 @@ pub struct PermissionResource {
pub granted: bool,

#[serde(rename = "_links")]
links: std::collections::HashMap<String, Link>
links: std::collections::HashMap<String, Link>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand All @@ -18,10 +18,10 @@ pub struct PermissionsListResource {
#[serde(rename = "_embedded")]
pub embedded: PermissionsEmbeddedResource,
#[serde(rename = "_links")]
links: std::collections::HashMap<String, Link>
links: std::collections::HashMap<String, Link>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct PermissionsEmbeddedResource {
pub permissions: Vec<PermissionResource>
}
pub permissions: Vec<PermissionResource>,
}
10 changes: 2 additions & 8 deletions crates/mollie_api/src/models/refund.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::Display;
use serde::{Serialize, Deserialize};

use super::{amount::Amount, link::Link};

Expand Down Expand Up @@ -29,13 +29,7 @@ impl Display for RefundResource {
write!(
f,
"[{}] {} | {} | {} | {} | {} |",
self.id,
self.status,
self.amount,
self.created_at,
self.description,
self.payment_id,
self.id, self.status, self.amount, self.created_at, self.description, self.payment_id,
)
}
}

1 change: 1 addition & 0 deletions crates/mollie_cli/src/balances/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub async fn command(
balance_id: &String,
with_response: bool,
) -> miette::Result<()> {

debug!("Running Get API Balance for balance: {}", balance_id);
let token = config.bearer_token()?;
let balance = Mollie::build(token.as_str())
Expand Down
13 changes: 3 additions & 10 deletions crates/mollie_cli/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use log::LevelFilter;
use std::io::Write;

pub fn init(cli_debug_mode: bool) {

// When developing, we want a thorough logger with as much info as possible.
if cfg!(debug_assertions) {
build_local_logger(cli_debug_mode);
Expand Down Expand Up @@ -40,13 +39,7 @@ fn build_local_logger(cli_debug_mode: bool) {

fn build_production_logger(cli_debug_mode: bool) {
Builder::new()
.format(|buf, record| {
writeln!(
buf,
"{}",
record.args()
)
})
.format(|buf, record| writeln!(buf, "{}", record.args()))
.filter(None, get_level_filter(cli_debug_mode))
.init();
}
.init();
}
55 changes: 41 additions & 14 deletions crates/mollie_cli/src/payments/list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::config::MollieConfig;
use crate::payments::Payment;
use crate::payments::Status;
use colored::Colorize;
use colored_json::ToColoredJson;
use log::{debug, info};
use mollie_api::Mollie;
use pad::{Alignment, PadStr};

pub async fn command(
config: &MollieConfig,
Expand All @@ -13,6 +13,7 @@ pub async fn command(
profile_id: &Option<String>,
test_mode: &Option<bool>,
with_response: bool,
status: &Option<Status>,
) -> miette::Result<()> {
debug!("Listing 10 Payments");
let token = config.bearer_token()?;
Expand All @@ -22,7 +23,7 @@ pub async fn command(
.await;
match response {
Ok(res) => {
list_payments_from_response(res, with_response);
list_payments_from_response(res, with_response, status);
}
Err(e) => info!("{}", e),
}
Expand All @@ -32,20 +33,46 @@ pub async fn command(
fn list_payments_from_response(
response: mollie_api::models::payment::PaymentsListResource,
with_response: bool,
status: &Option<Status>,
) {
info!(" {}", Colorize::bright_black(&*Payment::header()));
response
.embedded
.payments
.iter()
.enumerate()
.for_each(|(index, payment)| {
info!(
"{}. {}",
index + 1,
Payment::from(payment.clone()).to_string()
);
});
/*response.embedded.payments.iter().enumerate().for_each(|(index, payment)| {
info!("{}. {}", index + 1, Payment::from(payment.clone()).to_string());
});
*/
match status {
Some(s) => {
response
.embedded
.payments
.iter()
.enumerate()
.for_each(|(index, payment)| {
if payment.status == s.to_string().to_lowercase() {
info!(
"{}. {}",
index + 1,
Payment::from(payment.clone()).to_string()
);
}
});
}
None => {
response
.embedded
.payments
.iter()
.enumerate()
.for_each(|(index, payment)| {
info!(
"{}. {}",
index + 1,
Payment::from(payment.clone()).to_string()
);
});
}
}

if with_response {
let pretty_json = jsonxf::pretty_print(&serde_json::to_string(&response).unwrap()).unwrap();
info!("{}", pretty_json.to_colored_json_auto().unwrap());
Expand Down
Loading

0 comments on commit 08cd73d

Please sign in to comment.