avanza_rs/
lib.rs

1mod account;
2mod char_encode;
3mod client;
4mod config;
5mod error;
6mod fund;
7mod middleware;
8mod position;
9mod stock;
10mod totp;
11
12pub use account::list::AccountListResponse;
13pub use account::Account;
14pub use client::{Client, Credentials};
15pub use config::{Configuration, Urls};
16pub use error::Error;
17pub use fund::buy::{FundBuyRequest, FundBuyResponse};
18pub use position::total_values::{Aggegated, TotalValue, TotalValuesResponse};
19pub use stock::list::{
20    Instrument, StockListFilter, StockListRequest, StockListResponse, StockListResponsePagination,
21    StockListSortBy,
22};
23pub use stock::order::{
24    OrderRequestStatus, Side, StockOrder, StockOrderResponse, StockOrderUnknownResponse,
25};
26
27#[cfg(test)]
28mod tests {
29    use std::env;
30
31    use crate::{
32        stock::list::{StockListFilter, StockListRequest, StockListSortBy},
33        totp::generate_totp,
34    };
35
36    #[test]
37    fn totp() {
38        dotenvy::dotenv().ok();
39        let totp_secret =
40            env::var("AVANZA_TOTP_SECRET").expect("AVANZA_TOTP_SECRET is required in .env");
41        let code = generate_totp(&totp_secret);
42        println!("code: {}", code);
43    }
44
45    #[tokio::test]
46    async fn list_accounts() {
47        dotenvy::dotenv().ok();
48        let username = env::var("AVANZA_USERNAME").expect("AVANZA_USERNAME is required in .env");
49        let password = env::var("AVANZA_PASSWORD").expect("AVANZA_PASSWORD is required in .env");
50        let totp_secret =
51            env::var("AVANZA_TOTP_SECRET").expect("AVANZA_TOTP_SECRET is required in .env");
52
53        let credentials = super::client::Credentials {
54            username: String::from(username),
55            password: String::from(password),
56            totp_secret: String::from(totp_secret),
57        };
58
59        let client = super::client::Client::authenticate(&credentials)
60            .await
61            .expect("Auth failed");
62
63        let request = StockListRequest {
64            filter: StockListFilter {
65                country_codes: vec![String::from("SE")],
66            },
67            limit: 20,
68            offset: 0,
69            sort_by: StockListSortBy {
70                field: String::from("name"),
71                order: String::from("desc"),
72            },
73        };
74
75        let stocks = client
76            .get_stock_list(&request)
77            .await
78            .expect("could not get stocklist");
79
80        println!("{:#?}", stocks);
81    }
82}