akahu_client/client/
accounts.rs

1//! Account endpoint implementations.
2//!
3//! This module contains methods for managing user accounts connected to your Akahu application.
4
5use crate::{AccountId, UserToken};
6
7use super::AkahuClient;
8use reqwest::Method;
9
10impl AkahuClient {
11    /// Get a list of all accounts that the user has connected to your application.
12    ///
13    /// # Arguments
14    ///
15    /// * `user_token` - The user's access token obtained through OAuth
16    ///
17    /// # Returns
18    ///
19    /// A response containing all accounts the user has connected to your application.
20    /// The returned account data depends on the permissions your application has been granted.
21    /// Access the accounts via the `.items` field.
22    ///
23    /// [<https://developers.akahu.nz/reference/get_accounts>]
24    pub async fn get_accounts(
25        &self,
26        user_token: &UserToken,
27    ) -> crate::error::AkahuResult<crate::models::ListResponse<crate::models::Account>> {
28        const URI: &str = "accounts";
29
30        let headers = self.build_user_headers(user_token)?;
31
32        let req = self
33            .client
34            .request(Method::GET, format!("{}/{}", self.base_url, URI))
35            .headers(headers)
36            .build()?;
37
38        self.execute_request(req).await
39    }
40
41    /// Get a specific account by its ID.
42    ///
43    /// # Arguments
44    ///
45    /// * `user_token` - The user's access token obtained through OAuth
46    /// * `account_id` - The unique identifier for the account (prefixed with `acc_`)
47    ///
48    /// # Returns
49    ///
50    /// A response containing the account details for the specified account.
51    /// The returned account data depends on the permissions your application has been granted.
52    /// Access the account via the `.item` field.
53    ///
54    /// [<https://developers.akahu.nz/reference/get_accounts-id>]
55    pub async fn get_account(
56        &self,
57        user_token: &UserToken,
58        account_id: &AccountId,
59    ) -> crate::error::AkahuResult<crate::models::ItemResponse<crate::models::Account>> {
60        let uri = format!("accounts/{}", account_id.as_str());
61
62        let headers = self.build_user_headers(user_token)?;
63
64        let req = self
65            .client
66            .request(Method::GET, format!("{}/{}", self.base_url, uri))
67            .headers(headers)
68            .build()?;
69
70        self.execute_request(req).await
71    }
72
73    /// Revoke your application's access to a specific account.
74    ///
75    /// **Note:** This endpoint is deprecated for accounts with official open banking connections.
76    /// Accounts connected via official open banking cannot be revoked on an individual basis.
77    /// Instead, you must either:
78    /// - Direct users through the OAuth flow to adjust permissions with their bank
79    /// - Use the Revoke Access To Authorisation endpoint to revoke the entire authorization
80    ///
81    /// # Arguments
82    ///
83    /// * `user_token` - The user's access token obtained through OAuth
84    /// * `account_id` - The unique identifier for the account to revoke access from
85    ///
86    /// # Returns
87    ///
88    /// Returns `Ok(())` on successful revocation.
89    /// Returns an error (400) when attempting to revoke accounts with `connection_type` of "official".
90    ///
91    /// [<https://developers.akahu.nz/reference/delete_accounts-id>]
92    #[deprecated(
93        note = "This endpoint is deprecated for accounts with official open banking connections. Use the Revoke Access To Authorisation endpoint instead."
94    )]
95    pub async fn revoke_account_access(
96        &self,
97        user_token: &UserToken,
98        account_id: &AccountId,
99    ) -> crate::error::AkahuResult<()> {
100        let uri = format!("accounts/{}", account_id.as_str());
101
102        let headers = self.build_user_headers(user_token)?;
103
104        let req = self
105            .client
106            .request(Method::DELETE, format!("{}/{}", self.base_url, uri))
107            .headers(headers)
108            .build()?;
109
110        // This endpoint returns empty response on success
111        let res = self.client.execute(req).await?;
112
113        if res.status().is_success() {
114            Ok(())
115        } else {
116            self.handle_error_response(res).await
117        }
118    }
119}