1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use serde::{Deserialize, Serialize};
use crate::client::Client;
use crate::errors::Result;
use crate::item::Item;
#[derive(Deserialize, Debug, Clone)]
pub struct Account {
pub account_id: String,
pub balances: AccountBalances,
pub mask: Option<String>,
pub name: String,
pub official_name: Option<String>,
pub r#type: String,
pub subtype: Option<String>,
pub verification_status: Option<String>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct AccountBalances {
pub available: Option<f64>,
pub current: f64,
pub limit: Option<f64>,
pub iso_currency_code: Option<String>,
pub unofficial_currency_code: Option<String>,
}
#[derive(Serialize)]
struct GetBalancesRequest<'a> {
client_id: &'a str,
secret: &'a str,
access_token: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<GetBalancesOptions<'a>>,
}
#[derive(Serialize, Debug, Clone)]
pub struct GetBalancesOptions<'a> {
pub account_ids: Option<&'a [&'a str]>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct GetBalancesResponse {
pub request_id: String,
pub accounts: Vec<Account>,
}
#[derive(Serialize)]
struct GetAccountsRequest<'a> {
client_id: &'a str,
secret: &'a str,
access_token: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
options: Option<GetAccountsOptions<'a>>,
}
#[derive(Serialize, Debug, Clone)]
pub struct GetAccountsOptions<'a> {
pub account_ids: Option<&'a [&'a str]>,
}
#[derive(Deserialize, Debug, Clone)]
pub struct GetAccountsResponse {
pub request_id: String,
pub accounts: Vec<Account>,
pub item: Item,
}
impl Client {
pub async fn get_balances<'a>(
&self,
access_token: &str,
options: Option<GetBalancesOptions<'a>>,
) -> Result<GetBalancesResponse> {
self.send_request(
"accounts/balance/get",
&GetBalancesRequest {
client_id: &self.client_id,
secret: &self.secret,
access_token,
options,
},
)
.await
}
pub async fn get_accounts<'a>(
&self,
access_token: &str,
options: Option<GetAccountsOptions<'a>>,
) -> Result<GetAccountsResponse> {
self.send_request(
"accounts/get",
&GetAccountsRequest {
client_id: &self.client_id,
secret: &self.secret,
access_token,
options,
},
)
.await
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::client::tests::{get_test_client, SANDBOX_INSTITUTION, TEST_PRODUCTS};
#[tokio::test]
async fn test_get_accounts() {
let client = get_test_client();
let sandbox_resp = client
.create_sandbox_public_token(SANDBOX_INSTITUTION, TEST_PRODUCTS)
.await
.unwrap();
let token_resp = client
.exchange_public_token(&sandbox_resp.public_token)
.await
.unwrap();
let resp = client
.get_accounts(&token_resp.access_token, None)
.await
.unwrap();
assert_ne!(resp.accounts.len(), 0);
assert_eq!(resp.item.item_id.is_empty(), false);
let resp = client
.get_accounts(
&token_resp.access_token,
Some(GetAccountsOptions {
account_ids: Some(&[&resp.accounts[0].account_id]),
}),
)
.await
.unwrap();
assert_ne!(resp.accounts.len(), 0);
assert_eq!(resp.item.item_id.is_empty(), false);
}
#[tokio::test]
async fn test_get_balances() {
let client = get_test_client();
let sandbox_resp = client
.create_sandbox_public_token(SANDBOX_INSTITUTION, TEST_PRODUCTS)
.await
.unwrap();
let token_resp = client
.exchange_public_token(&sandbox_resp.public_token)
.await
.unwrap();
let resp = client
.get_balances(&token_resp.access_token, None)
.await
.unwrap();
assert_ne!(resp.accounts.len(), 0);
let resp = client
.get_balances(
&token_resp.access_token,
Some(GetBalancesOptions {
account_ids: Some(&[&resp.accounts[0].account_id]),
}),
)
.await
.unwrap();
assert_ne!(resp.accounts.len(), 0);
}
}