oanda_v20_openapi/apis/
position_api.rs1use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum ClosePositionError {
22 Status400(models::ClosePositionBadRequestResponse),
23 Status401(models::Error401),
24 Status404(models::ClosePositionNotFoundResponse),
25 Status405(models::Error405),
26 UnknownValue(serde_json::Value),
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum GetInstrumentPositionError {
33 Status401(models::Error401),
34 Status404(models::Error404),
35 Status405(models::Error405),
36 UnknownValue(serde_json::Value),
37}
38
39#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum GetOpenPositionsError {
43 Status401(models::Error401),
44 Status404(models::Error404),
45 Status405(models::Error405),
46 UnknownValue(serde_json::Value),
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize)]
51#[serde(untagged)]
52pub enum GetPositionsError {
53 Status401(models::Error401),
54 Status404(models::Error404),
55 Status405(models::Error405),
56 UnknownValue(serde_json::Value),
57}
58
59
60pub async fn close_position(configuration: &configuration::Configuration, account_id: &str, instrument: models::InstrumentName, close_position_request: models::ClosePositionRequest, accept_datetime_format: Option<models::DateTimeFormat>) -> Result<models::ClosePositionResponse, Error<ClosePositionError>> {
62 let p_account_id = account_id;
64 let p_instrument = instrument;
65 let p_close_position_request = close_position_request;
66 let p_accept_datetime_format = accept_datetime_format;
67
68 let uri_str = format!("{}/accounts/{accountId}/positions/{instrument}/close", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), instrument=p_instrument.to_string());
69 let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
70
71 if let Some(ref user_agent) = configuration.user_agent {
72 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
73 }
74 if let Some(param_value) = p_accept_datetime_format {
75 req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
76 }
77 if let Some(ref token) = configuration.bearer_access_token {
78 req_builder = req_builder.bearer_auth(token.to_owned());
79 };
80 req_builder = req_builder.json(&p_close_position_request);
81
82 let req = req_builder.build()?;
83 let resp = configuration.client.execute(req).await?;
84
85 let status = resp.status();
86 let content_type = resp
87 .headers()
88 .get("content-type")
89 .and_then(|v| v.to_str().ok())
90 .unwrap_or("application/octet-stream");
91 let content_type = super::ContentType::from(content_type);
92
93 if !status.is_client_error() && !status.is_server_error() {
94 let content = resp.text().await?;
95 match content_type {
96 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
97 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ClosePositionResponse`"))),
98 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::ClosePositionResponse`")))),
99 }
100 } else {
101 let content = resp.text().await?;
102 let entity: Option<ClosePositionError> = serde_json::from_str(&content).ok();
103 Err(Error::ResponseError(ResponseContent { status, content, entity }))
104 }
105}
106
107pub async fn get_instrument_position(configuration: &configuration::Configuration, account_id: &str, instrument: models::InstrumentName) -> Result<models::InstrumentPositionResponse, Error<GetInstrumentPositionError>> {
109 let p_account_id = account_id;
111 let p_instrument = instrument;
112
113 let uri_str = format!("{}/accounts/{accountId}/positions/{instrument}", configuration.base_path, accountId=crate::apis::urlencode(p_account_id), instrument=p_instrument.to_string());
114 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
115
116 if let Some(ref user_agent) = configuration.user_agent {
117 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
118 }
119 if let Some(ref token) = configuration.bearer_access_token {
120 req_builder = req_builder.bearer_auth(token.to_owned());
121 };
122
123 let req = req_builder.build()?;
124 let resp = configuration.client.execute(req).await?;
125
126 let status = resp.status();
127 let content_type = resp
128 .headers()
129 .get("content-type")
130 .and_then(|v| v.to_str().ok())
131 .unwrap_or("application/octet-stream");
132 let content_type = super::ContentType::from(content_type);
133
134 if !status.is_client_error() && !status.is_server_error() {
135 let content = resp.text().await?;
136 match content_type {
137 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
138 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstrumentPositionResponse`"))),
139 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::InstrumentPositionResponse`")))),
140 }
141 } else {
142 let content = resp.text().await?;
143 let entity: Option<GetInstrumentPositionError> = serde_json::from_str(&content).ok();
144 Err(Error::ResponseError(ResponseContent { status, content, entity }))
145 }
146}
147
148pub async fn get_open_positions(configuration: &configuration::Configuration, account_id: &str) -> Result<models::OpenPositionsResponse, Error<GetOpenPositionsError>> {
150 let p_account_id = account_id;
152
153 let uri_str = format!("{}/accounts/{accountId}/openPositions", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
154 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
155
156 if let Some(ref user_agent) = configuration.user_agent {
157 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
158 }
159 if let Some(ref token) = configuration.bearer_access_token {
160 req_builder = req_builder.bearer_auth(token.to_owned());
161 };
162
163 let req = req_builder.build()?;
164 let resp = configuration.client.execute(req).await?;
165
166 let status = resp.status();
167 let content_type = resp
168 .headers()
169 .get("content-type")
170 .and_then(|v| v.to_str().ok())
171 .unwrap_or("application/octet-stream");
172 let content_type = super::ContentType::from(content_type);
173
174 if !status.is_client_error() && !status.is_server_error() {
175 let content = resp.text().await?;
176 match content_type {
177 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
178 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::OpenPositionsResponse`"))),
179 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::OpenPositionsResponse`")))),
180 }
181 } else {
182 let content = resp.text().await?;
183 let entity: Option<GetOpenPositionsError> = serde_json::from_str(&content).ok();
184 Err(Error::ResponseError(ResponseContent { status, content, entity }))
185 }
186}
187
188pub async fn get_positions(configuration: &configuration::Configuration, account_id: &str) -> Result<models::PositionsResponse, Error<GetPositionsError>> {
190 let p_account_id = account_id;
192
193 let uri_str = format!("{}/accounts/{accountId}/positions", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
194 let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
195
196 if let Some(ref user_agent) = configuration.user_agent {
197 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
198 }
199 if let Some(ref token) = configuration.bearer_access_token {
200 req_builder = req_builder.bearer_auth(token.to_owned());
201 };
202
203 let req = req_builder.build()?;
204 let resp = configuration.client.execute(req).await?;
205
206 let status = resp.status();
207 let content_type = resp
208 .headers()
209 .get("content-type")
210 .and_then(|v| v.to_str().ok())
211 .unwrap_or("application/octet-stream");
212 let content_type = super::ContentType::from(content_type);
213
214 if !status.is_client_error() && !status.is_server_error() {
215 let content = resp.text().await?;
216 match content_type {
217 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
218 ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PositionsResponse`"))),
219 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::PositionsResponse`")))),
220 }
221 } else {
222 let content = resp.text().await?;
223 let entity: Option<GetPositionsError> = serde_json::from_str(&content).ok();
224 Err(Error::ResponseError(ResponseContent { status, content, entity }))
225 }
226}
227