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
//! Accounts API endpoints.
use crate::client::RestClient;
use crate::error::Result;
use crate::models::{Account, GetAccountResponse, ListAccountsParams, ListAccountsResponse};
/// API for managing accounts.
///
/// Accounts represent wallets for holding different currencies.
/// Each account holds a single currency.
pub struct AccountsApi<'a> {
client: &'a RestClient,
}
impl<'a> AccountsApi<'a> {
/// Create a new Accounts API instance.
pub(crate) fn new(client: &'a RestClient) -> Self {
Self { client }
}
/// List all accounts.
///
/// Returns a paginated list of accounts. Use the `cursor` from the response
/// to fetch the next page.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials, models::ListAccountsParams};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// // List first 10 accounts
/// let response = client.accounts()
/// .list(ListAccountsParams::new().limit(10))
/// .await?;
///
/// for account in response.accounts {
/// println!("{}: {} {}", account.name, account.available_balance.value, account.currency);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn list(&self, params: ListAccountsParams) -> Result<ListAccountsResponse> {
self.client.get_with_query("/accounts", ¶ms).await
}
/// List all accounts with default parameters.
pub async fn list_all(&self) -> Result<ListAccountsResponse> {
self.list(ListAccountsParams::default()).await
}
/// Get a single account by UUID.
///
/// # Arguments
///
/// * `account_uuid` - The unique identifier of the account.
///
/// # Example
///
/// ```no_run
/// # use coinbase_advanced::{RestClient, Credentials};
/// # async fn example() -> coinbase_advanced::Result<()> {
/// let client = RestClient::builder()
/// .credentials(Credentials::from_env()?)
/// .build()?;
///
/// let account = client.accounts()
/// .get("account-uuid-here")
/// .await?;
///
/// println!("Balance: {} {}", account.available_balance.value, account.currency);
/// # Ok(())
/// # }
/// ```
pub async fn get(&self, account_uuid: &str) -> Result<Account> {
let endpoint = format!("/accounts/{}", account_uuid);
let response: GetAccountResponse = self.client.get(&endpoint).await?;
Ok(response.account)
}
}