ynab_api/apis/
accounts_api.rs1use reqwest;
13use serde::{Deserialize, Serialize};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateAccountError {
22 Status400(models::ErrorResponse),
23 UnknownValue(serde_json::Value),
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum GetAccountByIdError {
30 Status404(models::ErrorResponse),
31 DefaultResponse(models::ErrorResponse),
32 UnknownValue(serde_json::Value),
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum GetAccountsError {
39 Status404(models::ErrorResponse),
40 DefaultResponse(models::ErrorResponse),
41 UnknownValue(serde_json::Value),
42}
43
44
45pub async fn create_account(configuration: &configuration::Configuration, budget_id: &str, data: models::PostAccountWrapper) -> Result<models::AccountResponse, Error<CreateAccountError>> {
47 let local_var_configuration = configuration;
48
49 let local_var_client = &local_var_configuration.client;
50
51 let local_var_uri_str = format!("{}/budgets/{budget_id}/accounts", local_var_configuration.base_path, budget_id=crate::apis::urlencode(budget_id));
52 let mut local_var_req_builder = local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
53
54 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
55 local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
56 }
57 if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
58 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
59 };
60 local_var_req_builder = local_var_req_builder.json(&data);
61
62 let local_var_req = local_var_req_builder.build()?;
63 let local_var_resp = local_var_client.execute(local_var_req).await?;
64
65 let local_var_status = local_var_resp.status();
66 let local_var_content = local_var_resp.text().await?;
67
68 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
69 serde_json::from_str(&local_var_content).map_err(Error::from)
70 } else {
71 let local_var_entity: Option<CreateAccountError> = serde_json::from_str(&local_var_content).ok();
72 let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
73 Err(Error::ResponseError(local_var_error))
74 }
75}
76
77pub async fn get_account_by_id(configuration: &configuration::Configuration, budget_id: &str, account_id: &str) -> Result<models::AccountResponse, Error<GetAccountByIdError>> {
79 let local_var_configuration = configuration;
80
81 let local_var_client = &local_var_configuration.client;
82
83 let local_var_uri_str = format!("{}/budgets/{budget_id}/accounts/{account_id}", local_var_configuration.base_path, budget_id=crate::apis::urlencode(budget_id), account_id=crate::apis::urlencode(account_id));
84 let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
85
86 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
87 local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
88 }
89 if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
90 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
91 };
92
93 let local_var_req = local_var_req_builder.build()?;
94 let local_var_resp = local_var_client.execute(local_var_req).await?;
95
96 let local_var_status = local_var_resp.status();
97 let local_var_content = local_var_resp.text().await?;
98
99 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
100 serde_json::from_str(&local_var_content).map_err(Error::from)
101 } else {
102 let local_var_entity: Option<GetAccountByIdError> = serde_json::from_str(&local_var_content).ok();
103 let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
104 Err(Error::ResponseError(local_var_error))
105 }
106}
107
108pub async fn get_accounts(configuration: &configuration::Configuration, budget_id: &str, last_knowledge_of_server: Option<i64>) -> Result<models::AccountsResponse, Error<GetAccountsError>> {
110 let local_var_configuration = configuration;
111
112 let local_var_client = &local_var_configuration.client;
113
114 let local_var_uri_str = format!("{}/budgets/{budget_id}/accounts", local_var_configuration.base_path, budget_id=crate::apis::urlencode(budget_id));
115 let mut local_var_req_builder = local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());
116
117 if let Some(ref local_var_str) = last_knowledge_of_server {
118 local_var_req_builder = local_var_req_builder.query(&[("last_knowledge_of_server", &local_var_str.to_string())]);
119 }
120 if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
121 local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
122 }
123 if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
124 local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
125 };
126
127 let local_var_req = local_var_req_builder.build()?;
128 let local_var_resp = local_var_client.execute(local_var_req).await?;
129
130 let local_var_status = local_var_resp.status();
131 let local_var_content = local_var_resp.text().await?;
132
133 if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
134 serde_json::from_str(&local_var_content).map_err(Error::from)
135 } else {
136 let local_var_entity: Option<GetAccountsError> = serde_json::from_str(&local_var_content).ok();
137 let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
138 Err(Error::ResponseError(local_var_error))
139 }
140}
141