From 4ea5fd50242acb7764e1a8b92770b0aaa72bc9fc Mon Sep 17 00:00:00 2001 From: Allan Calix Date: Wed, 31 Aug 2022 11:21:36 -0700 Subject: [PATCH] Add feature to parse amounts as `Decimal` types --- Cargo.toml | 5 ++-- src/client.rs | 26 +++++++++---------- src/lib.rs | 17 ++++++++++++ src/model/account.rs | 12 +++++++++ src/model/transactions.rs | 6 +++++ ..._client__tests__can_read_transactions.snap | 2 +- 6 files changed, 52 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 726f1c4..3b68dc9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rplaid" -version = "0.3.0" +version = "0.4.0" authors = ["Allan Calix "] edition = "2021" description = """ @@ -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" diff --git a/src/client.rs b/src/client.rs index c6bf599..5f83167 100644 --- a/src/client.rs +++ b/src/client.rs @@ -250,9 +250,9 @@ impl Plaid { &self, public_token: P, ) -> Result { - Ok(self + self .request(&ExchangePublicTokenRequest { public_token }) - .await?) + .await } /// Creates a `link_token` that is required as a parameter when initializing @@ -263,7 +263,7 @@ impl Plaid { &self, req: &CreateLinkTokenRequest<'_, P>, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Retrieves information for any linked item, only active accounts are @@ -352,7 +352,7 @@ impl Plaid { &self, req: &GetAuthRequest<'_, P>, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Verify the name, address, phone number, and email address of a user @@ -363,7 +363,7 @@ impl Plaid { &self, req: &GetIdentityRequest<'_, P>, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Triggers a Transactions `DEFAULT_UPDATE` webhook for a given Sandbox @@ -375,7 +375,7 @@ impl Plaid { &self, req: &FireWebhookRequest

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

, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Searches Plaid's database for known employers to use with Deposit @@ -397,7 +397,7 @@ impl Plaid { &self, req: &SearchEmployerRequest<'_, P>, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Provides a JSON Web Key (JWK) that can be used to verify a JWT. @@ -407,7 +407,7 @@ impl Plaid { &self, req: &GetWebhookVerificationKeyRequest

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

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

, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Get detailed information on categories returned by Plaid. This endpoint @@ -439,7 +439,7 @@ impl Plaid { &self, req: &GetCategoriesRequest, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Initiates on-demand extraction to fetch the newest transactions for an @@ -465,7 +465,7 @@ impl Plaid { &self, req: &GetTransactionsRequest

, ) -> Result { - Ok(self.request(req).await?) + self.request(req).await } /// Returns a Stream of transactions that can be used to iterative fetch diff --git a/src/lib.rs b/src/lib.rs index 8fddd62..bdc7c57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. @@ -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; diff --git a/src/model/account.rs b/src/model/account.rs index 99a58b0..11c8c3a 100644 --- a/src/model/account.rs +++ b/src/model/account.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "decimal")] +use rust_decimal::Decimal; + use super::*; #[derive(Debug, Serialize)] @@ -62,9 +65,18 @@ pub enum AccountType { #[derive(Debug, Deserialize, Serialize)] pub struct Balance { + #[cfg(not(feature = "decimal"))] pub available: Option, + #[cfg(not(feature = "decimal"))] pub current: Option, + #[cfg(feature = "decimal")] + pub available: Option, + #[cfg(feature = "decimal")] + pub current: Option, pub iso_currency_code: Option, + #[cfg(feature = "decimal")] + pub limit: Option, + #[cfg(not(feature = "decimal"))] pub limit: Option, pub unofficial_currency_code: Option, } diff --git a/src/model/transactions.rs b/src/model/transactions.rs index feee09f..1d53dfe 100644 --- a/src/model/transactions.rs +++ b/src/model/transactions.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "decimal")] +use rust_decimal::Decimal; + use super::*; #[derive(Debug, Serialize, Copy, Clone)] @@ -97,7 +100,10 @@ pub struct Transaction { #[serde(skip_serializing_if = "Option::is_none")] pub original_description: Option, 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, #[serde(skip_serializing_if = "Option::is_none")] diff --git a/src/snapshots/rplaid__client__tests__can_read_transactions.snap b/src/snapshots/rplaid__client__tests__can_read_transactions.snap index cbd0eca..d86a258 100644 --- a/src/snapshots/rplaid__client__tests__can_read_transactions.snap +++ b/src/snapshots/rplaid__client__tests__can_read_transactions.snap @@ -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" },