akahu_client/client/
refresh.rs

1//! Data Refresh Endpoints
2//!
3//! This module contains methods for refreshing account data.
4
5use crate::UserToken;
6
7use super::AkahuClient;
8use reqwest::Method;
9
10impl AkahuClient {
11    /// Refresh all accounts connected to your application.
12    ///
13    /// This endpoint initiates an on-demand data refresh across all accounts. Account data
14    /// such as balance and transactions are periodically refreshed by Akahu and enriched
15    /// asynchronously, providing clean and consistent data across financial institutions.
16    ///
17    /// **Note:** Data enrichment occurs asynchronously after the refresh request.
18    ///
19    /// # Arguments
20    ///
21    /// * `user_token` - The user's access token obtained through OAuth
22    ///
23    /// # Returns
24    ///
25    /// Returns `Ok(())` on successful refresh initiation.
26    ///
27    /// [<https://developers.akahu.nz/reference/post_refresh>]
28    pub async fn refresh_all_accounts(
29        &self,
30        user_token: &UserToken,
31    ) -> crate::error::AkahuResult<()> {
32        const URI: &str = "refresh";
33
34        let headers = self.build_user_headers(user_token)?;
35
36        let req = self
37            .client
38            .request(Method::POST, format!("{}/{}", self.base_url, URI))
39            .headers(headers)
40            .build()?;
41
42        let res = self.client.execute(req).await?;
43
44        if res.status().is_success() {
45            Ok(())
46        } else {
47            self.handle_error_response(res).await
48        }
49    }
50
51    /// Refresh a specific account or connection.
52    ///
53    /// This endpoint initiates a data refresh for either a specific financial institution
54    /// connection or individual account.
55    ///
56    /// **ID Type Behavior:**
57    /// - **Connection ID**: Triggers a refresh for all accounts held at that financial institution
58    /// - **Account ID**: Refreshes that specific account plus any other accounts sharing the
59    ///   same login credentials. For example, if the user has three ASB accounts from a single
60    ///   set of login credentials and you request a refresh for one, the other two accounts
61    ///   will also be refreshed.
62    ///
63    /// **Note:** Data enrichment occurs asynchronously after the refresh request.
64    ///
65    /// # Arguments
66    ///
67    /// * `user_token` - The user's access token obtained through OAuth
68    /// * `id` - Either a Connection ID or Account ID
69    ///
70    /// # Returns
71    ///
72    /// Returns `Ok(())` on successful refresh initiation.
73    ///
74    /// [<https://developers.akahu.nz/reference/post_refresh-id>]
75    pub async fn refresh_account_or_connection<Id: AsRef<str>>(
76        &self,
77        user_token: &UserToken,
78        id: Id,
79    ) -> crate::error::AkahuResult<()> {
80        let uri = format!("refresh/{}", id.as_ref());
81
82        let headers = self.build_user_headers(user_token)?;
83
84        let req = self
85            .client
86            .request(Method::POST, format!("{}/{}", self.base_url, uri))
87            .headers(headers)
88            .build()?;
89
90        let res = self.client.execute(req).await?;
91
92        if res.status().is_success() {
93            Ok(())
94        } else {
95            self.handle_error_response(res).await
96        }
97    }
98}