Skip to main content

oanda_v20_openapi/apis/
instrument_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 [`get_instrument_candles`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetInstrumentCandlesError {
22    Status400(models::Error400),
23    Status401(models::Error401),
24    Status404(models::Error404),
25    Status405(models::Error405),
26    UnknownValue(serde_json::Value),
27}
28
29/// struct for typed errors of method [`get_instrument_order_book`]
30#[derive(Debug, Clone, Serialize, Deserialize)]
31#[serde(untagged)]
32pub enum GetInstrumentOrderBookError {
33    Status400(models::Error400),
34    Status401(models::Error401),
35    Status404(models::Error404),
36    Status405(models::Error405),
37    UnknownValue(serde_json::Value),
38}
39
40/// struct for typed errors of method [`get_instrument_position_book`]
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum GetInstrumentPositionBookError {
44    Status400(models::Error400),
45    Status401(models::Error401),
46    Status404(models::Error404),
47    Status405(models::Error405),
48    UnknownValue(serde_json::Value),
49}
50
51
52/// Fetch candlestick data for an instrument.
53pub async fn get_instrument_candles(configuration: &configuration::Configuration, instrument: models::InstrumentName, accept_datetime_format: Option<models::DateTimeFormat>, price: Option<&str>, granularity: Option<models::CandlestickGranularity>, count: Option<i32>, from: Option<&str>, to: Option<&str>, smooth: Option<bool>, include_first: Option<bool>, daily_alignment: Option<i32>, alignment_timezone: Option<&str>, weekly_alignment: Option<&str>) -> Result<models::CandlesResponse, Error<GetInstrumentCandlesError>> {
54    // add a prefix to parameters to efficiently prevent name collisions
55    let p_instrument = instrument;
56    let p_accept_datetime_format = accept_datetime_format;
57    let p_price = price;
58    let p_granularity = granularity;
59    let p_count = count;
60    let p_from = from;
61    let p_to = to;
62    let p_smooth = smooth;
63    let p_include_first = include_first;
64    let p_daily_alignment = daily_alignment;
65    let p_alignment_timezone = alignment_timezone;
66    let p_weekly_alignment = weekly_alignment;
67
68    let uri_str = format!("{}/instruments/{instrument}/candles", configuration.base_path, instrument=p_instrument.to_string());
69    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
70
71    if let Some(ref param_value) = p_price {
72        req_builder = req_builder.query(&[("price", &param_value.to_string())]);
73    }
74    if let Some(ref param_value) = p_granularity {
75        req_builder = req_builder.query(&[("granularity", &param_value.to_string())]);
76    }
77    if let Some(ref param_value) = p_count {
78        req_builder = req_builder.query(&[("count", &param_value.to_string())]);
79    }
80    if let Some(ref param_value) = p_from {
81        req_builder = req_builder.query(&[("from", &param_value.to_string())]);
82    }
83    if let Some(ref param_value) = p_to {
84        req_builder = req_builder.query(&[("to", &param_value.to_string())]);
85    }
86    if let Some(ref param_value) = p_smooth {
87        req_builder = req_builder.query(&[("smooth", &param_value.to_string())]);
88    }
89    if let Some(ref param_value) = p_include_first {
90        req_builder = req_builder.query(&[("includeFirst", &param_value.to_string())]);
91    }
92    if let Some(ref param_value) = p_daily_alignment {
93        req_builder = req_builder.query(&[("dailyAlignment", &param_value.to_string())]);
94    }
95    if let Some(ref param_value) = p_alignment_timezone {
96        req_builder = req_builder.query(&[("alignmentTimezone", &param_value.to_string())]);
97    }
98    if let Some(ref param_value) = p_weekly_alignment {
99        req_builder = req_builder.query(&[("weeklyAlignment", &param_value.to_string())]);
100    }
101    if let Some(ref user_agent) = configuration.user_agent {
102        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
103    }
104    if let Some(param_value) = p_accept_datetime_format {
105        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
106    }
107    if let Some(ref token) = configuration.bearer_access_token {
108        req_builder = req_builder.bearer_auth(token.to_owned());
109    };
110
111    let req = req_builder.build()?;
112    let resp = configuration.client.execute(req).await?;
113
114    let status = resp.status();
115    let content_type = resp
116        .headers()
117        .get("content-type")
118        .and_then(|v| v.to_str().ok())
119        .unwrap_or("application/octet-stream");
120    let content_type = super::ContentType::from(content_type);
121
122    if !status.is_client_error() && !status.is_server_error() {
123        let content = resp.text().await?;
124        match content_type {
125            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
126            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CandlesResponse`"))),
127            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::CandlesResponse`")))),
128        }
129    } else {
130        let content = resp.text().await?;
131        let entity: Option<GetInstrumentCandlesError> = serde_json::from_str(&content).ok();
132        Err(Error::ResponseError(ResponseContent { status, content, entity }))
133    }
134}
135
136/// Fetch an order book for an instrument.
137pub async fn get_instrument_order_book(configuration: &configuration::Configuration, instrument: models::InstrumentName, accept_datetime_format: Option<models::DateTimeFormat>, time: Option<&str>) -> Result<models::InstrumentOrderBookResponse, Error<GetInstrumentOrderBookError>> {
138    // add a prefix to parameters to efficiently prevent name collisions
139    let p_instrument = instrument;
140    let p_accept_datetime_format = accept_datetime_format;
141    let p_time = time;
142
143    let uri_str = format!("{}/instruments/{instrument}/orderBook", configuration.base_path, instrument=p_instrument.to_string());
144    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
145
146    if let Some(ref param_value) = p_time {
147        req_builder = req_builder.query(&[("time", &param_value.to_string())]);
148    }
149    if let Some(ref user_agent) = configuration.user_agent {
150        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
151    }
152    if let Some(param_value) = p_accept_datetime_format {
153        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
154    }
155    if let Some(ref token) = configuration.bearer_access_token {
156        req_builder = req_builder.bearer_auth(token.to_owned());
157    };
158
159    let req = req_builder.build()?;
160    let resp = configuration.client.execute(req).await?;
161
162    let status = resp.status();
163    let content_type = resp
164        .headers()
165        .get("content-type")
166        .and_then(|v| v.to_str().ok())
167        .unwrap_or("application/octet-stream");
168    let content_type = super::ContentType::from(content_type);
169
170    if !status.is_client_error() && !status.is_server_error() {
171        let content = resp.text().await?;
172        match content_type {
173            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
174            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstrumentOrderBookResponse`"))),
175            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::InstrumentOrderBookResponse`")))),
176        }
177    } else {
178        let content = resp.text().await?;
179        let entity: Option<GetInstrumentOrderBookError> = serde_json::from_str(&content).ok();
180        Err(Error::ResponseError(ResponseContent { status, content, entity }))
181    }
182}
183
184/// Fetch a position book for an instrument.
185pub async fn get_instrument_position_book(configuration: &configuration::Configuration, instrument: models::InstrumentName, accept_datetime_format: Option<models::DateTimeFormat>, time: Option<&str>) -> Result<models::InstrumentPositionBookResponse, Error<GetInstrumentPositionBookError>> {
186    // add a prefix to parameters to efficiently prevent name collisions
187    let p_instrument = instrument;
188    let p_accept_datetime_format = accept_datetime_format;
189    let p_time = time;
190
191    let uri_str = format!("{}/instruments/{instrument}/positionBook", configuration.base_path, instrument=p_instrument.to_string());
192    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
193
194    if let Some(ref param_value) = p_time {
195        req_builder = req_builder.query(&[("time", &param_value.to_string())]);
196    }
197    if let Some(ref user_agent) = configuration.user_agent {
198        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
199    }
200    if let Some(param_value) = p_accept_datetime_format {
201        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
202    }
203    if let Some(ref token) = configuration.bearer_access_token {
204        req_builder = req_builder.bearer_auth(token.to_owned());
205    };
206
207    let req = req_builder.build()?;
208    let resp = configuration.client.execute(req).await?;
209
210    let status = resp.status();
211    let content_type = resp
212        .headers()
213        .get("content-type")
214        .and_then(|v| v.to_str().ok())
215        .unwrap_or("application/octet-stream");
216    let content_type = super::ContentType::from(content_type);
217
218    if !status.is_client_error() && !status.is_server_error() {
219        let content = resp.text().await?;
220        match content_type {
221            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
222            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstrumentPositionBookResponse`"))),
223            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::InstrumentPositionBookResponse`")))),
224        }
225    } else {
226        let content = resp.text().await?;
227        let entity: Option<GetInstrumentPositionBookError> = serde_json::from_str(&content).ok();
228        Err(Error::ResponseError(ResponseContent { status, content, entity }))
229    }
230}
231