Formatting fixes
The result of `cargo fmt`.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
use crate::models::{AccountArray, TransactionArray, TransactionStore, TransactionUpdate};
|
||||
use reqwest::Url;
|
||||
use reqwest_middleware::ClientWithMiddleware;
|
||||
use serde::de::DeserializeOwned;
|
||||
use thiserror::Error;
|
||||
use tracing::instrument;
|
||||
use crate::models::{AccountArray, TransactionStore, TransactionArray, TransactionUpdate};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FireflyError {
|
||||
@@ -28,10 +28,16 @@ impl FireflyClient {
|
||||
Self::with_client(base_url, access_token, None)
|
||||
}
|
||||
|
||||
pub fn with_client(base_url: &str, access_token: &str, client: Option<ClientWithMiddleware>) -> Result<Self, FireflyError> {
|
||||
pub fn with_client(
|
||||
base_url: &str,
|
||||
access_token: &str,
|
||||
client: Option<ClientWithMiddleware>,
|
||||
) -> Result<Self, FireflyError> {
|
||||
Ok(Self {
|
||||
base_url: Url::parse(base_url)?,
|
||||
client: client.unwrap_or_else(|| reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build()),
|
||||
client: client.unwrap_or_else(|| {
|
||||
reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build()
|
||||
}),
|
||||
access_token: access_token.to_string(),
|
||||
})
|
||||
}
|
||||
@@ -39,12 +45,11 @@ impl FireflyClient {
|
||||
#[instrument(skip(self))]
|
||||
pub async fn get_accounts(&self, _iban: &str) -> Result<AccountArray, FireflyError> {
|
||||
let mut url = self.base_url.join("/api/v1/accounts")?;
|
||||
url.query_pairs_mut()
|
||||
.append_pair("type", "asset");
|
||||
|
||||
url.query_pairs_mut().append_pair("type", "asset");
|
||||
|
||||
self.get_authenticated(url).await
|
||||
}
|
||||
|
||||
|
||||
#[instrument(skip(self))]
|
||||
pub async fn search_accounts(&self, query: &str) -> Result<AccountArray, FireflyError> {
|
||||
let mut url = self.base_url.join("/api/v1/search/accounts")?;
|
||||
@@ -52,15 +57,20 @@ impl FireflyClient {
|
||||
.append_pair("query", query)
|
||||
.append_pair("type", "asset")
|
||||
.append_pair("field", "all");
|
||||
|
||||
|
||||
self.get_authenticated(url).await
|
||||
}
|
||||
|
||||
#[instrument(skip(self, transaction))]
|
||||
pub async fn store_transaction(&self, transaction: TransactionStore) -> Result<(), FireflyError> {
|
||||
pub async fn store_transaction(
|
||||
&self,
|
||||
transaction: TransactionStore,
|
||||
) -> Result<(), FireflyError> {
|
||||
let url = self.base_url.join("/api/v1/transactions")?;
|
||||
|
||||
let response = self.client.post(url)
|
||||
|
||||
let response = self
|
||||
.client
|
||||
.post(url)
|
||||
.bearer_auth(&self.access_token)
|
||||
.header("accept", "application/json")
|
||||
.json(&transaction)
|
||||
@@ -70,15 +80,25 @@ impl FireflyClient {
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await?;
|
||||
return Err(FireflyError::ApiError(format!("Store Transaction Failed {}: {}", status, text)));
|
||||
return Err(FireflyError::ApiError(format!(
|
||||
"Store Transaction Failed {}: {}",
|
||||
status, text
|
||||
)));
|
||||
}
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
#[instrument(skip(self))]
|
||||
pub async fn list_account_transactions(&self, account_id: &str, start: Option<&str>, end: Option<&str>) -> Result<TransactionArray, FireflyError> {
|
||||
let mut url = self.base_url.join(&format!("/api/v1/accounts/{}/transactions", account_id))?;
|
||||
pub async fn list_account_transactions(
|
||||
&self,
|
||||
account_id: &str,
|
||||
start: Option<&str>,
|
||||
end: Option<&str>,
|
||||
) -> Result<TransactionArray, FireflyError> {
|
||||
let mut url = self
|
||||
.base_url
|
||||
.join(&format!("/api/v1/accounts/{}/transactions", account_id))?;
|
||||
{
|
||||
let mut pairs = url.query_pairs_mut();
|
||||
if let Some(s) = start {
|
||||
@@ -88,17 +108,25 @@ impl FireflyClient {
|
||||
pairs.append_pair("end", e);
|
||||
}
|
||||
// Limit to 50, could be higher but safer to page if needed. For heuristic checks 50 is usually plenty per day range.
|
||||
pairs.append_pair("limit", "50");
|
||||
pairs.append_pair("limit", "50");
|
||||
}
|
||||
|
||||
self.get_authenticated(url).await
|
||||
}
|
||||
|
||||
#[instrument(skip(self, update))]
|
||||
pub async fn update_transaction(&self, id: &str, update: TransactionUpdate) -> Result<(), FireflyError> {
|
||||
let url = self.base_url.join(&format!("/api/v1/transactions/{}", id))?;
|
||||
pub async fn update_transaction(
|
||||
&self,
|
||||
id: &str,
|
||||
update: TransactionUpdate,
|
||||
) -> Result<(), FireflyError> {
|
||||
let url = self
|
||||
.base_url
|
||||
.join(&format!("/api/v1/transactions/{}", id))?;
|
||||
|
||||
let response = self.client.put(url)
|
||||
let response = self
|
||||
.client
|
||||
.put(url)
|
||||
.bearer_auth(&self.access_token)
|
||||
.header("accept", "application/json")
|
||||
.json(&update)
|
||||
@@ -106,25 +134,33 @@ impl FireflyClient {
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await?;
|
||||
return Err(FireflyError::ApiError(format!("Update Transaction Failed {}: {}", status, text)));
|
||||
let status = response.status();
|
||||
let text = response.text().await?;
|
||||
return Err(FireflyError::ApiError(format!(
|
||||
"Update Transaction Failed {}: {}",
|
||||
status, text
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_authenticated<T: DeserializeOwned>(&self, url: Url) -> Result<T, FireflyError> {
|
||||
let response = self.client.get(url)
|
||||
let response = self
|
||||
.client
|
||||
.get(url)
|
||||
.bearer_auth(&self.access_token)
|
||||
.header("accept", "application/json")
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let text = response.text().await?;
|
||||
return Err(FireflyError::ApiError(format!("API request failed {}: {}", status, text)));
|
||||
let status = response.status();
|
||||
let text = response.text().await?;
|
||||
return Err(FireflyError::ApiError(format!(
|
||||
"API request failed {}: {}",
|
||||
status, text
|
||||
)));
|
||||
}
|
||||
|
||||
let data = response.json().await?;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use firefly_client::client::FireflyClient;
|
||||
use firefly_client::models::{TransactionStore, TransactionSplitStore};
|
||||
use wiremock::matchers::{method, path, header};
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||
use firefly_client::models::{TransactionSplitStore, TransactionStore};
|
||||
use std::fs;
|
||||
use wiremock::matchers::{header, method, path};
|
||||
use wiremock::{Mock, MockServer, ResponseTemplate};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_search_accounts() {
|
||||
@@ -21,7 +21,10 @@ async fn test_search_accounts() {
|
||||
|
||||
assert_eq!(accounts.data.len(), 1);
|
||||
assert_eq!(accounts.data[0].attributes.name, "Checking Account");
|
||||
assert_eq!(accounts.data[0].attributes.iban.as_deref(), Some("NL01BANK0123456789"));
|
||||
assert_eq!(
|
||||
accounts.data[0].attributes.iban.as_deref(),
|
||||
Some("NL01BANK0123456789")
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -36,7 +39,7 @@ async fn test_store_transaction() {
|
||||
.await;
|
||||
|
||||
let client = FireflyClient::new(&mock_server.uri(), "my-token").unwrap();
|
||||
|
||||
|
||||
let tx = TransactionStore {
|
||||
transactions: vec![TransactionSplitStore {
|
||||
transaction_type: "withdrawal".to_string(),
|
||||
|
||||
Reference in New Issue
Block a user