Skip to main content

oanda_v20_openapi/apis/
order_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 [`accounts_account_id_orders_order_specifier_put`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum AccountsAccountIdOrdersOrderSpecifierPutError {
22    Status400(models::ReplaceOrderBadRequestResponse),
23    Status401(models::Error401),
24    Status404(models::ReplaceOrderNotFoundResponse),
25    Status405(models::Error405),
26    UnknownValue(serde_json::Value),
27}
28
29/// struct for typed errors of method [`cancel_order`]
30#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum CancelOrderError {
33    Status401(models::Error401),
34    Status404(models::CancelOrder404Response),
35    Status405(models::Error405),
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`create_order`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum CreateOrderError {
43    Status400(models::CreateOrderBadRequestResponse),
44    Status401(models::Error401),
45    Status403(models::Error403),
46    Status404(models::CreateOrderNotFoundResponse),
47    Status405(models::Error405),
48    UnknownValue(serde_json::Value),
49}
50
51/// struct for typed errors of method [`get_order`]
52#[derive(Debug, Clone, Serialize, Deserialize)]
53#[serde(untagged)]
54pub enum GetOrderError {
55    Status401(models::Error401),
56    Status404(models::Error404),
57    Status405(models::Error405),
58    UnknownValue(serde_json::Value),
59}
60
61/// struct for typed errors of method [`get_orders`]
62#[derive(Debug, Clone, Serialize, Deserialize)]
63#[serde(untagged)]
64pub enum GetOrdersError {
65    Status400(models::Error400),
66    Status404(models::Error404),
67    Status405(models::Error405),
68    UnknownValue(serde_json::Value),
69}
70
71/// struct for typed errors of method [`get_pending_orders`]
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(untagged)]
74pub enum GetPendingOrdersError {
75    Status401(models::Error401),
76    Status404(models::Error404),
77    Status405(models::Error405),
78    UnknownValue(serde_json::Value),
79}
80
81/// struct for typed errors of method [`set_order_extensions`]
82#[derive(Debug, Clone, Serialize, Deserialize)]
83#[serde(untagged)]
84pub enum SetOrderExtensionsError {
85    Status400(models::OrderExtensionsBadRequestResponse),
86    Status401(models::Error401),
87    Status404(models::OrderExtensionsNotFoundResponse),
88    Status405(models::Error405),
89    UnknownValue(serde_json::Value),
90}
91
92
93/// Replace an Order in an Account by simultaneously cancelling it and creating a replacement Order
94pub async fn accounts_account_id_orders_order_specifier_put(configuration: &configuration::Configuration, account_id: &str, order_specifier: &str, replace_order_request: models::ReplaceOrderRequest, accept_datetime_format: Option<models::DateTimeFormat>, client_request_id: Option<&str>) -> Result<models::ReplaceOrderResponse, Error<AccountsAccountIdOrdersOrderSpecifierPutError>> {
95    // add a prefix to parameters to efficiently prevent name collisions
96    let p_account_id = account_id;
97    let p_order_specifier = order_specifier;
98    let p_replace_order_request = replace_order_request;
99    let p_accept_datetime_format = accept_datetime_format;
100    let p_client_request_id = client_request_id;
101
102    let uri_str = format!("{}/accounts/{accountId}/orders/{orderSpecifier}", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), orderSpecifier=crate::apis::urlencode(p_order_specifier));
103    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
104
105    if let Some(ref user_agent) = configuration.user_agent {
106        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
107    }
108    if let Some(param_value) = p_accept_datetime_format {
109        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
110    }
111    if let Some(param_value) = p_client_request_id {
112        req_builder = req_builder.header("ClientRequestId", param_value.to_string());
113    }
114    if let Some(ref token) = configuration.bearer_access_token {
115        req_builder = req_builder.bearer_auth(token.to_owned());
116    };
117    req_builder = req_builder.json(&p_replace_order_request);
118
119    let req = req_builder.build()?;
120    let resp = configuration.client.execute(req).await?;
121
122    let status = resp.status();
123    let content_type = resp
124        .headers()
125        .get("content-type")
126        .and_then(|v| v.to_str().ok())
127        .unwrap_or("application/octet-stream");
128    let content_type = super::ContentType::from(content_type);
129
130    if !status.is_client_error() && !status.is_server_error() {
131        let content = resp.text().await?;
132        match content_type {
133            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
134            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ReplaceOrderResponse`"))),
135            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::ReplaceOrderResponse`")))),
136        }
137    } else {
138        let content = resp.text().await?;
139        let entity: Option<AccountsAccountIdOrdersOrderSpecifierPutError> = serde_json::from_str(&content).ok();
140        Err(Error::ResponseError(ResponseContent { status, content, entity }))
141    }
142}
143
144/// Cancel a pending Order in an Account
145pub async fn cancel_order(configuration: &configuration::Configuration, account_id: &str, order_specifier: &str, accept_datetime_format: Option<models::DateTimeFormat>, client_request_id: Option<&str>) -> Result<models::CancelOrderResponse, Error<CancelOrderError>> {
146    // add a prefix to parameters to efficiently prevent name collisions
147    let p_account_id = account_id;
148    let p_order_specifier = order_specifier;
149    let p_accept_datetime_format = accept_datetime_format;
150    let p_client_request_id = client_request_id;
151
152    let uri_str = format!("{}/accounts/{accountId}/orders/{orderSpecifier}/cancel", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), orderSpecifier=crate::apis::urlencode(p_order_specifier));
153    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
154
155    if let Some(ref user_agent) = configuration.user_agent {
156        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
157    }
158    if let Some(param_value) = p_accept_datetime_format {
159        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
160    }
161    if let Some(param_value) = p_client_request_id {
162        req_builder = req_builder.header("ClientRequestId", param_value.to_string());
163    }
164    if let Some(ref token) = configuration.bearer_access_token {
165        req_builder = req_builder.bearer_auth(token.to_owned());
166    };
167
168    let req = req_builder.build()?;
169    let resp = configuration.client.execute(req).await?;
170
171    let status = resp.status();
172    let content_type = resp
173        .headers()
174        .get("content-type")
175        .and_then(|v| v.to_str().ok())
176        .unwrap_or("application/octet-stream");
177    let content_type = super::ContentType::from(content_type);
178
179    if !status.is_client_error() && !status.is_server_error() {
180        let content = resp.text().await?;
181        match content_type {
182            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
183            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CancelOrderResponse`"))),
184            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::CancelOrderResponse`")))),
185        }
186    } else {
187        let content = resp.text().await?;
188        let entity: Option<CancelOrderError> = serde_json::from_str(&content).ok();
189        Err(Error::ResponseError(ResponseContent { status, content, entity }))
190    }
191}
192
193/// Create an Order for an Account
194pub async fn create_order(configuration: &configuration::Configuration, account_id: &str, create_order_request: models::CreateOrderRequest, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::CreateOrderResponse, Error<CreateOrderError>> {
195    // add a prefix to parameters to efficiently prevent name collisions
196    let p_account_id = account_id;
197    let p_create_order_request = create_order_request;
198    let p_accept_datetime_format = accept_datetime_format;
199
200    let uri_str = format!("{}/accounts/{accountId}/orders", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
201    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
202
203    if let Some(ref user_agent) = configuration.user_agent {
204        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
205    }
206    if let Some(param_value) = p_accept_datetime_format {
207        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
208    }
209    if let Some(ref token) = configuration.bearer_access_token {
210        req_builder = req_builder.bearer_auth(token.to_owned());
211    };
212    req_builder = req_builder.json(&p_create_order_request);
213
214    let req = req_builder.build()?;
215    let resp = configuration.client.execute(req).await?;
216
217    let status = resp.status();
218    let content_type = resp
219        .headers()
220        .get("content-type")
221        .and_then(|v| v.to_str().ok())
222        .unwrap_or("application/octet-stream");
223    let content_type = super::ContentType::from(content_type);
224
225    if !status.is_client_error() && !status.is_server_error() {
226        let content = resp.text().await?;
227        match content_type {
228            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
229            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateOrderResponse`"))),
230            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::CreateOrderResponse`")))),
231        }
232    } else {
233        let content = resp.text().await?;
234        let entity: Option<CreateOrderError> = serde_json::from_str(&content).ok();
235        Err(Error::ResponseError(ResponseContent { status, content, entity }))
236    }
237}
238
239/// Get details for a single Order in an Account
240pub async fn get_order(configuration: &configuration::Configuration, account_id: &str, order_specifier: &str, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::OrderResponse, Error<GetOrderError>> {
241    // add a prefix to parameters to efficiently prevent name collisions
242    let p_account_id = account_id;
243    let p_order_specifier = order_specifier;
244    let p_accept_datetime_format = accept_datetime_format;
245
246    let uri_str = format!("{}/accounts/{accountId}/orders/{orderSpecifier}", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), orderSpecifier=crate::apis::urlencode(p_order_specifier));
247    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
248
249    if let Some(ref user_agent) = configuration.user_agent {
250        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
251    }
252    if let Some(param_value) = p_accept_datetime_format {
253        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
254    }
255    if let Some(ref token) = configuration.bearer_access_token {
256        req_builder = req_builder.bearer_auth(token.to_owned());
257    };
258
259    let req = req_builder.build()?;
260    let resp = configuration.client.execute(req).await?;
261
262    let status = resp.status();
263    let content_type = resp
264        .headers()
265        .get("content-type")
266        .and_then(|v| v.to_str().ok())
267        .unwrap_or("application/octet-stream");
268    let content_type = super::ContentType::from(content_type);
269
270    if !status.is_client_error() && !status.is_server_error() {
271        let content = resp.text().await?;
272        match content_type {
273            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
274            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::OrderResponse`"))),
275            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::OrderResponse`")))),
276        }
277    } else {
278        let content = resp.text().await?;
279        let entity: Option<GetOrderError> = serde_json::from_str(&content).ok();
280        Err(Error::ResponseError(ResponseContent { status, content, entity }))
281    }
282}
283
284/// Get a list of Orders for an Account
285pub async fn get_orders(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>, ids: Option<Vec<i32>>, state: Option<&str>, instrument: Option<models::InstrumentName>, count: Option<i32>, before_id: Option<i32>) -> Result<models::OrdersResponse, Error<GetOrdersError>> {
286    // add a prefix to parameters to efficiently prevent name collisions
287    let p_account_id = account_id;
288    let p_accept_datetime_format = accept_datetime_format;
289    let p_ids = ids;
290    let p_state = state;
291    let p_instrument = instrument;
292    let p_count = count;
293    let p_before_id = before_id;
294
295    let uri_str = format!("{}/accounts/{accountId}/orders", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
296    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
297
298    if let Some(ref param_value) = p_ids {
299        req_builder = match "csv" {
300            "multi" => req_builder.query(&param_value.into_iter().map(|p| ("ids".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
301            _ => req_builder.query(&[("ids", &param_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
302        };
303    }
304    if let Some(ref param_value) = p_state {
305        req_builder = req_builder.query(&[("state", &param_value.to_string())]);
306    }
307    if let Some(ref param_value) = p_instrument {
308        req_builder = req_builder.query(&[("instrument", &param_value.to_string())]);
309    }
310    if let Some(ref param_value) = p_count {
311        req_builder = req_builder.query(&[("count", &param_value.to_string())]);
312    }
313    if let Some(ref param_value) = p_before_id {
314        req_builder = req_builder.query(&[("beforeID", &param_value.to_string())]);
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(param_value) = p_accept_datetime_format {
320        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
321    }
322    if let Some(ref token) = configuration.bearer_access_token {
323        req_builder = req_builder.bearer_auth(token.to_owned());
324    };
325
326    let req = req_builder.build()?;
327    let resp = configuration.client.execute(req).await?;
328
329    let status = resp.status();
330    let content_type = resp
331        .headers()
332        .get("content-type")
333        .and_then(|v| v.to_str().ok())
334        .unwrap_or("application/octet-stream");
335    let content_type = super::ContentType::from(content_type);
336
337    if !status.is_client_error() && !status.is_server_error() {
338        let content = resp.text().await?;
339        match content_type {
340            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
341            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::OrdersResponse`"))),
342            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::OrdersResponse`")))),
343        }
344    } else {
345        let content = resp.text().await?;
346        let entity: Option<GetOrdersError> = serde_json::from_str(&content).ok();
347        Err(Error::ResponseError(ResponseContent { status, content, entity }))
348    }
349}
350
351/// List all pending Orders in an Account
352pub async fn get_pending_orders(configuration: &configuration::Configuration, account_id: &str, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::PendingOrdersResponse, Error<GetPendingOrdersError>> {
353    // add a prefix to parameters to efficiently prevent name collisions
354    let p_account_id = account_id;
355    let p_accept_datetime_format = accept_datetime_format;
356
357    let uri_str = format!("{}/accounts/{accountId}/pendingOrders", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
358    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
359
360    if let Some(ref user_agent) = configuration.user_agent {
361        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
362    }
363    if let Some(param_value) = p_accept_datetime_format {
364        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
365    }
366    if let Some(ref token) = configuration.bearer_access_token {
367        req_builder = req_builder.bearer_auth(token.to_owned());
368    };
369
370    let req = req_builder.build()?;
371    let resp = configuration.client.execute(req).await?;
372
373    let status = resp.status();
374    let content_type = resp
375        .headers()
376        .get("content-type")
377        .and_then(|v| v.to_str().ok())
378        .unwrap_or("application/octet-stream");
379    let content_type = super::ContentType::from(content_type);
380
381    if !status.is_client_error() && !status.is_server_error() {
382        let content = resp.text().await?;
383        match content_type {
384            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
385            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PendingOrdersResponse`"))),
386            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::PendingOrdersResponse`")))),
387        }
388    } else {
389        let content = resp.text().await?;
390        let entity: Option<GetPendingOrdersError> = serde_json::from_str(&content).ok();
391        Err(Error::ResponseError(ResponseContent { status, content, entity }))
392    }
393}
394
395/// Update the Client Extensions for an Order in an Account. Do notset, modify, or delete clientExtensions if your account is associated with MT4.
396pub async fn set_order_extensions(configuration: &configuration::Configuration, account_id: &str, order_specifier: &str, order_extensions_request: models::OrderExtensionsRequest, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::OrderExtensionsResponse, Error<SetOrderExtensionsError>> {
397    // add a prefix to parameters to efficiently prevent name collisions
398    let p_account_id = account_id;
399    let p_order_specifier = order_specifier;
400    let p_order_extensions_request = order_extensions_request;
401    let p_accept_datetime_format = accept_datetime_format;
402
403    let uri_str = format!("{}/accounts/{accountId}/orders/{orderSpecifier}/clientExtensions", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), orderSpecifier=crate::apis::urlencode(p_order_specifier));
404    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
405
406    if let Some(ref user_agent) = configuration.user_agent {
407        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
408    }
409    if let Some(param_value) = p_accept_datetime_format {
410        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
411    }
412    if let Some(ref token) = configuration.bearer_access_token {
413        req_builder = req_builder.bearer_auth(token.to_owned());
414    };
415    req_builder = req_builder.json(&p_order_extensions_request);
416
417    let req = req_builder.build()?;
418    let resp = configuration.client.execute(req).await?;
419
420    let status = resp.status();
421    let content_type = resp
422        .headers()
423        .get("content-type")
424        .and_then(|v| v.to_str().ok())
425        .unwrap_or("application/octet-stream");
426    let content_type = super::ContentType::from(content_type);
427
428    if !status.is_client_error() && !status.is_server_error() {
429        let content = resp.text().await?;
430        match content_type {
431            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
432            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::OrderExtensionsResponse`"))),
433            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::OrderExtensionsResponse`")))),
434        }
435    } else {
436        let content = resp.text().await?;
437        let entity: Option<SetOrderExtensionsError> = serde_json::from_str(&content).ok();
438        Err(Error::ResponseError(ResponseContent { status, content, entity }))
439    }
440}
441