up-api/README.md

47 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2022-07-09 13:01:31 +00:00
# Up API
A convenient and easy to use wrapper for the [Up Bank API](https://developer.up.com.au).
2022-07-09 13:01:31 +00:00
## Example
2022-07-13 10:43:56 +00:00
The following example shows the calculation of the sum of all earnings (transactions with positive value) since a given date:
2022-07-09 13:01:31 +00:00
```
2022-07-10 08:39:53 +00:00
use up_api::v1::Client;
2022-07-13 10:43:56 +00:00
use up_api::v1::transactions::{ListTransactionsOptions, TransactionResource};
fn sum_earnings(transactions : &Vec<TransactionResource>) -> f32 {
transactions
.iter()
.map(|t| &t.attributes.amount.value)
.map(|v| v.parse::<f32>().unwrap())
.filter(|a| a > &0.0)
.sum()
}
2022-07-09 13:01:31 +00:00
#[tokio::main]
async fn main() {
let token = std::env::var("UP_ACCESS_TOKEN").unwrap();
2022-07-10 08:39:53 +00:00
2022-07-09 13:01:31 +00:00
let client = Client::new(token.to_string());
let mut options = ListTransactionsOptions::default();
2022-07-13 10:43:56 +00:00
options.filter_since("2022-01-01T00:00:00Z".to_string());
2022-07-10 08:39:53 +00:00
options.page_size(100);
2022-07-09 13:01:31 +00:00
2022-07-13 10:43:56 +00:00
let mut transactions = client.list_transactions(&options).await.unwrap();
let mut total = sum_earnings(&transactions.data);
while let Some(next_page) = transactions.next(&client).await {
let next_page = next_page.unwrap();
total = total + sum_earnings(&next_page.data);
2022-07-09 13:01:31 +00:00
2022-07-13 10:43:56 +00:00
transactions = next_page;
}
2022-07-09 13:01:31 +00:00
println!("{}", total);
}
```