Skip to content

Commit

Permalink
Add feature to parse amounts as Decimal types
Browse files Browse the repository at this point in the history
  • Loading branch information
allancalix committed Aug 31, 2022
1 parent 558f9d7 commit 4ea5fd5
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rplaid"
version = "0.3.0"
version = "0.4.0"
authors = ["Allan Calix <[email protected]>"]
edition = "2021"
description = """
Expand All @@ -17,13 +17,14 @@ license = "MIT"

[features]
default = ["streams"]
bare = []
streams = ["async-stream", "futures-core", "futures-util"]
decimal = ["rust_decimal"]

[dependencies]
http-client = "6.5.1"
tokio = { version = "1", features = ["macros"] }
http-types = "2.12.0"
rust_decimal = { version = "1.26", optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.28"
Expand Down
26 changes: 13 additions & 13 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl<T: HttpClient> Plaid<T> {
&self,
public_token: P,
) -> Result<ExchangePublicTokenResponse, ClientError> {
Ok(self
self
.request(&ExchangePublicTokenRequest { public_token })
.await?)
.await
}

/// Creates a `link_token` that is required as a parameter when initializing
Expand All @@ -263,7 +263,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &CreateLinkTokenRequest<'_, P>,
) -> Result<CreateLinkTokenResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Retrieves information for any linked item, only active accounts are
Expand Down Expand Up @@ -352,7 +352,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetAuthRequest<'_, P>,
) -> Result<GetAuthResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Verify the name, address, phone number, and email address of a user
Expand All @@ -363,7 +363,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetIdentityRequest<'_, P>,
) -> Result<GetIdentityResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Triggers a Transactions `DEFAULT_UPDATE` webhook for a given Sandbox
Expand All @@ -375,7 +375,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &FireWebhookRequest<P>,
) -> Result<FireWebhookResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Changes the verification status of an Item in the sandbox in order to
Expand All @@ -386,7 +386,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &SetVerificationStatusRequest<P>,
) -> Result<SetVerificationStatusResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Searches Plaid's database for known employers to use with Deposit
Expand All @@ -397,7 +397,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &SearchEmployerRequest<'_, P>,
) -> Result<SearchEmployerResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Provides a JSON Web Key (JWK) that can be used to verify a JWT.
Expand All @@ -407,7 +407,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetWebhookVerificationKeyRequest<P>,
) -> Result<GetWebhookVerificationKeyResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Gets information about a `link_token`, can be useful for debugging.
Expand All @@ -417,7 +417,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetLinkTokenRequest<P>,
) -> Result<GetLinkTokenResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Rotate the `access_token` associated with an Item. Call returns a new
Expand All @@ -428,7 +428,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &InvalidateAccessTokenRequest<P>,
) -> Result<InvalidateAccessTokenResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Get detailed information on categories returned by Plaid. This endpoint
Expand All @@ -439,7 +439,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetCategoriesRequest,
) -> Result<GetCategoriesResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Initiates on-demand extraction to fetch the newest transactions for an
Expand All @@ -465,7 +465,7 @@ impl<T: HttpClient> Plaid<T> {
&self,
req: &GetTransactionsRequest<P>,
) -> Result<GetTransactionsResponse, ClientError> {
Ok(self.request(req).await?)
self.request(req).await
}

/// Returns a Stream of transactions that can be used to iterative fetch
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ __These bindings are not official Plaid bindings.__
control over the HTTP client used.
* Rust types, including variant types, for most API return types.
# Create features
This crate provides a few features that make working with Plaid's APIs more
ergonomic. Without these features the crate works like a 1:1 translation of
Plaid's APIs.
* **streams** -
When enabled this will add a new method for consuming all transactions available
from a future stream without having to worry about pagination or cursors.
* **decimal** -
When enabled currency amounts in response payloads will be parsed as
`rust_decimal::Decimal` types for more correct use in computations.
# Limitations
Some endpoints are production specific or beta products and are not yet
supported by the client.
Expand All @@ -51,3 +64,7 @@ pub mod model;

/// Re-exports HttpClient trait for implementing a custom HTTP client.
pub use http_client::HttpClient;

/// Re-exports Decimal type used for currency amounts.
#[cfg(feature = "decimal")]
pub use rust_decimal::Decimal;
12 changes: 12 additions & 0 deletions src/model/account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;

use super::*;

#[derive(Debug, Serialize)]
Expand Down Expand Up @@ -62,9 +65,18 @@ pub enum AccountType {

#[derive(Debug, Deserialize, Serialize)]
pub struct Balance {
#[cfg(not(feature = "decimal"))]
pub available: Option<f64>,
#[cfg(not(feature = "decimal"))]
pub current: Option<f64>,
#[cfg(feature = "decimal")]
pub available: Option<Decimal>,
#[cfg(feature = "decimal")]
pub current: Option<Decimal>,
pub iso_currency_code: Option<String>,
#[cfg(feature = "decimal")]
pub limit: Option<Decimal>,
#[cfg(not(feature = "decimal"))]
pub limit: Option<f64>,
pub unofficial_currency_code: Option<String>,
}
6 changes: 6 additions & 0 deletions src/model/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;

use super::*;

#[derive(Debug, Serialize, Copy, Clone)]
Expand Down Expand Up @@ -97,7 +100,10 @@ pub struct Transaction {
#[serde(skip_serializing_if = "Option::is_none")]
pub original_description: Option<String>,
pub account_id: String,
#[cfg(not(feature = "decimal"))]
pub amount: f64,
#[cfg(feature = "decimal")]
pub amount: Decimal,
#[serde(skip_serializing_if = "Option::is_none")]
pub iso_currency_code: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ expression: res.transactions
"date": "2021-09-05",
"pending": false,
"transaction_id": "[transaction_id]",
"payment_channel": "other",
"payment_channel": "in store",
"merchant_name": "United Airlines",
"authorized_date": "2021-09-05"
},
Expand Down

0 comments on commit 4ea5fd5

Please sign in to comment.