Skip to main content

oanda_v20_openapi/apis/
account_api.rs

1/*
2 * OANDA v20 API
3 *
4 * The full OANDA v20 API Specification. This specification defines how to interact with v20 Accounts, Trades, Orders, Pricing and more.
5 *
6 * The version of the OpenAPI document: 0.2.1
7 * Contact: jmicoud02@gmail.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`configure_account`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum ConfigureAccountError {
22    Status400(models::AccountConfigurationBadRequestResponse),
23    Status401(models::Error401),
24    Status403(models::AccountConfigurationForbiddenResponse),
25    Status404(models::Error404),
26    Status405(models::Error405),
27    UnknownValue(serde_json::Value),
28}
29
30/// struct for typed errors of method [`get_account`]
31#[derive(Debug, Clone, Serialize, Deserialize)]
32#[serde(untagged)]
33pub enum GetAccountError {
34    Status400(models::Error400),
35    Status401(models::Error401),
36    Status405(models::Error405),
37    UnknownValue(serde_json::Value),
38}
39
40/// struct for typed errors of method [`get_account_changes`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum GetAccountChangesError {
44    Status401(models::Error401),
45    Status404(models::Error404),
46    Status405(models::Error405),
47    Status416(models::Error416),
48    UnknownValue(serde_json::Value),
49}
50
51/// struct for typed errors of method [`get_account_instruments`]
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[serde(untagged)]
54pub enum GetAccountInstrumentsError {
55    Status400(models::Error400),
56    Status401(models::Error401),
57    Status405(models::Error405),
58    UnknownValue(serde_json::Value),
59}
60
61/// struct for typed errors of method [`get_account_summary`]
62#[derive(Debug, Clone, Serialize, Deserialize)]
63#[serde(untagged)]
64pub enum GetAccountSummaryError {
65    Status400(models::Error400),
66    Status401(models::Error401),
67    Status405(models::Error405),
68    UnknownValue(serde_json::Value),
69}
70
71/// struct for typed errors of method [`get_accounts`]
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(untagged)]
74pub enum GetAccountsError {
75    Status401(models::Error401),
76    Status405(models::Error405),
77    UnknownValue(serde_json::Value),
78}
79
80
81/// Set the client-configurable portions of an Account.
82pub async fn configure_account(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>, account_configuration_request: Option<models::AccountConfigurationRequest>) -> Result<models::AccountConfigurationResponse, Error<ConfigureAccountError>> {
83    // add a prefix to parameters to efficiently prevent name collisions
84    let p_account_id = account_id;
85    let p_accept_datetime_format = accept_datetime_format;
86    let p_account_configuration_request = account_configuration_request;
87
88    let uri_str = format!("{}/accounts/{accountId}/configuration", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
89    let mut req_builder = configuration.client.request(reqwest::Method::PATCH, &uri_str);
90
91    if let Some(ref user_agent) = configuration.user_agent {
92        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
93    }
94    if let Some(param_value) = p_accept_datetime_format {
95        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
96    }
97    if let Some(ref token) = configuration.bearer_access_token {
98        req_builder = req_builder.bearer_auth(token.to_owned());
99    };
100    req_builder = req_builder.json(&p_account_configuration_request);
101
102    let req = req_builder.build()?;
103    let resp = configuration.client.execute(req).await?;
104
105    let status = resp.status();
106    let content_type = resp
107        .headers()
108        .get("content-type")
109        .and_then(|v| v.to_str().ok())
110        .unwrap_or("application/octet-stream");
111    let content_type = super::ContentType::from(content_type);
112
113    if !status.is_client_error() && !status.is_server_error() {
114        let content = resp.text().await?;
115        match content_type {
116            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
117            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountConfigurationResponse`"))),
118            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountConfigurationResponse`")))),
119        }
120    } else {
121        let content = resp.text().await?;
122        let entity: Option<ConfigureAccountError> = serde_json::from_str(&content).ok();
123        Err(Error::ResponseError(ResponseContent { status, content, entity }))
124    }
125}
126
127/// Get the full details for a single Account that a client has access to. Full pending Order, open Trade and open Position representations are provided.
128pub async fn get_account(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::AccountResponse, Error<GetAccountError>> {
129    // add a prefix to parameters to efficiently prevent name collisions
130    let p_account_id = account_id;
131    let p_accept_datetime_format = accept_datetime_format;
132
133    let uri_str = format!("{}/accounts/{accountId}", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
134    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
135
136    if let Some(ref user_agent) = configuration.user_agent {
137        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
138    }
139    if let Some(param_value) = p_accept_datetime_format {
140        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
141    }
142    if let Some(ref token) = configuration.bearer_access_token {
143        req_builder = req_builder.bearer_auth(token.to_owned());
144    };
145
146    let req = req_builder.build()?;
147    let resp = configuration.client.execute(req).await?;
148
149    let status = resp.status();
150    let content_type = resp
151        .headers()
152        .get("content-type")
153        .and_then(|v| v.to_str().ok())
154        .unwrap_or("application/octet-stream");
155    let content_type = super::ContentType::from(content_type);
156
157    if !status.is_client_error() && !status.is_server_error() {
158        let content = resp.text().await?;
159        match content_type {
160            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
161            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountResponse`"))),
162            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountResponse`")))),
163        }
164    } else {
165        let content = resp.text().await?;
166        let entity: Option<GetAccountError> = serde_json::from_str(&content).ok();
167        Err(Error::ResponseError(ResponseContent { status, content, entity }))
168    }
169}
170
171/// Endpoint used to poll an Account for its current state and changes since a specified TransactionID.
172pub async fn get_account_changes(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>, since_transaction_id: Option<i32>) -> Result<models::AccountChangesResponse, Error<GetAccountChangesError>> {
173    // add a prefix to parameters to efficiently prevent name collisions
174    let p_account_id = account_id;
175    let p_accept_datetime_format = accept_datetime_format;
176    let p_since_transaction_id = since_transaction_id;
177
178    let uri_str = format!("{}/accounts/{accountId}/changes", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
179    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
180
181    if let Some(ref param_value) = p_since_transaction_id {
182        req_builder = req_builder.query(&[("sinceTransactionID", &param_value.to_string())]);
183    }
184    if let Some(ref user_agent) = configuration.user_agent {
185        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
186    }
187    if let Some(param_value) = p_accept_datetime_format {
188        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
189    }
190    if let Some(ref token) = configuration.bearer_access_token {
191        req_builder = req_builder.bearer_auth(token.to_owned());
192    };
193
194    let req = req_builder.build()?;
195    let resp = configuration.client.execute(req).await?;
196
197    let status = resp.status();
198    let content_type = resp
199        .headers()
200        .get("content-type")
201        .and_then(|v| v.to_str().ok())
202        .unwrap_or("application/octet-stream");
203    let content_type = super::ContentType::from(content_type);
204
205    if !status.is_client_error() && !status.is_server_error() {
206        let content = resp.text().await?;
207        match content_type {
208            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
209            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountChangesResponse`"))),
210            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountChangesResponse`")))),
211        }
212    } else {
213        let content = resp.text().await?;
214        let entity: Option<GetAccountChangesError> = serde_json::from_str(&content).ok();
215        Err(Error::ResponseError(ResponseContent { status, content, entity }))
216    }
217}
218
219/// Get the list of tradeable instruments for the given Account. The list of tradeable instruments is dependent on the regulatory division that the Account is located in, thus should be the same for all Accounts owned by a single user.
220pub async fn get_account_instruments(configuration: &configuration::Configuration, account_id: &str, instruments: Option<Vec<models::InstrumentName>>) -> Result<models::AccountInstrumentsResponse, Error<GetAccountInstrumentsError>> {
221    // add a prefix to parameters to efficiently prevent name collisions
222    let p_account_id = account_id;
223    let p_instruments = instruments;
224
225    let uri_str = format!("{}/accounts/{accountId}/instruments", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
226    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
227
228    if let Some(ref param_value) = p_instruments {
229        req_builder = match "csv" {
230            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("instruments".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
231            _ => req_builder.query(&[("instruments", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
232        };
233    }
234    if let Some(ref user_agent) = configuration.user_agent {
235        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
236    }
237    if let Some(ref token) = configuration.bearer_access_token {
238        req_builder = req_builder.bearer_auth(token.to_owned());
239    };
240
241    let req = req_builder.build()?;
242    let resp = configuration.client.execute(req).await?;
243
244    let status = resp.status();
245    let content_type = resp
246        .headers()
247        .get("content-type")
248        .and_then(|v| v.to_str().ok())
249        .unwrap_or("application/octet-stream");
250    let content_type = super::ContentType::from(content_type);
251
252    if !status.is_client_error() && !status.is_server_error() {
253        let content = resp.text().await?;
254        match content_type {
255            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
256            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountInstrumentsResponse`"))),
257            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountInstrumentsResponse`")))),
258        }
259    } else {
260        let content = resp.text().await?;
261        let entity: Option<GetAccountInstrumentsError> = serde_json::from_str(&content).ok();
262        Err(Error::ResponseError(ResponseContent { status, content, entity }))
263    }
264}
265
266/// Get a summary for a single Account that a client has access to.
267pub async fn get_account_summary(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::AccountSummaryResponse, Error<GetAccountSummaryError>> {
268    // add a prefix to parameters to efficiently prevent name collisions
269    let p_account_id = account_id;
270    let p_accept_datetime_format = accept_datetime_format;
271
272    let uri_str = format!("{}/accounts/{accountId}/summary", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
273    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
274
275    if let Some(ref user_agent) = configuration.user_agent {
276        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
277    }
278    if let Some(param_value) = p_accept_datetime_format {
279        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
280    }
281    if let Some(ref token) = configuration.bearer_access_token {
282        req_builder = req_builder.bearer_auth(token.to_owned());
283    };
284
285    let req = req_builder.build()?;
286    let resp = configuration.client.execute(req).await?;
287
288    let status = resp.status();
289    let content_type = resp
290        .headers()
291        .get("content-type")
292        .and_then(|v| v.to_str().ok())
293        .unwrap_or("application/octet-stream");
294    let content_type = super::ContentType::from(content_type);
295
296    if !status.is_client_error() && !status.is_server_error() {
297        let content = resp.text().await?;
298        match content_type {
299            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
300            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountSummaryResponse`"))),
301            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountSummaryResponse`")))),
302        }
303    } else {
304        let content = resp.text().await?;
305        let entity: Option<GetAccountSummaryError> = serde_json::from_str(&content).ok();
306        Err(Error::ResponseError(ResponseContent { status, content, entity }))
307    }
308}
309
310/// Get a list of all Accounts authorized for the provided token.
311pub async fn get_accounts(configuration: &configuration::Configuration, ) -> Result<models::AccountsResponse, Error<GetAccountsError>> {
312
313    let uri_str = format!("{}/accounts", configuration.base_path);
314    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
315
316    if let Some(ref user_agent) = configuration.user_agent {
317        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
318    }
319    if let Some(ref token) = configuration.bearer_access_token {
320        req_builder = req_builder.bearer_auth(token.to_owned());
321    };
322
323    let req = req_builder.build()?;
324    let resp = configuration.client.execute(req).await?;
325
326    let status = resp.status();
327    let content_type = resp
328        .headers()
329        .get("content-type")
330        .and_then(|v| v.to_str().ok())
331        .unwrap_or("application/octet-stream");
332    let content_type = super::ContentType::from(content_type);
333
334    if !status.is_client_error() && !status.is_server_error() {
335        let content = resp.text().await?;
336        match content_type {
337            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
338            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::AccountsResponse`"))),
339            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::AccountsResponse`")))),
340        }
341    } else {
342        let content = resp.text().await?;
343        let entity: Option<GetAccountsError> = serde_json::from_str(&content).ok();
344        Err(Error::ResponseError(ResponseContent { status, content, entity }))
345    }
346}
347