Skip to main content

oanda_v20_openapi/apis/
pricing_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_pricing`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum GetPricingError {
22    Status400(models::Error400),
23    Status401(models::Error401),
24    Status404(models::Error404),
25    Status405(models::Error405),
26    UnknownValue(serde_json::Value),
27}
28
29
30/// Get pricing information for a specified list of Instruments within an Account.
31pub async fn get_pricing(configuration: &configuration::Configuration, account_id: &str, instruments: Vec<models::InstrumentName>, accept_datetime_format: Option<models::DateTimeFormat>, since: Option<&str>, include_units_available: Option<bool>, include_home_conversions: Option<bool>) -> Result<models::PricingResponse, Error<GetPricingError>> {
32    // add a prefix to parameters to efficiently prevent name collisions
33    let p_account_id = account_id;
34    let p_instruments = instruments;
35    let p_accept_datetime_format = accept_datetime_format;
36    let p_since = since;
37    let p_include_units_available = include_units_available;
38    let p_include_home_conversions = include_home_conversions;
39
40    let uri_str = format!("{}/accounts/{accountId}/pricing", configuration.base_path, accountId=crate::apis::urlencode(p_account_id));
41    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
42
43    req_builder = match "csv" {
44        "multi" => req_builder.query(&p_instruments.into_iter().map(|p| ("instruments".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
45        _ => req_builder.query(&[("instruments", &p_instruments.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
46    };
47    if let Some(ref param_value) = p_since {
48        req_builder = req_builder.query(&[("since", &param_value.to_string())]);
49    }
50    if let Some(ref param_value) = p_include_units_available {
51        req_builder = req_builder.query(&[("includeUnitsAvailable", &param_value.to_string())]);
52    }
53    if let Some(ref param_value) = p_include_home_conversions {
54        req_builder = req_builder.query(&[("includeHomeConversions", &param_value.to_string())]);
55    }
56    if let Some(ref user_agent) = configuration.user_agent {
57        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
58    }
59    if let Some(param_value) = p_accept_datetime_format {
60        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
61    }
62    if let Some(ref token) = configuration.bearer_access_token {
63        req_builder = req_builder.bearer_auth(token.to_owned());
64    };
65
66    let req = req_builder.build()?;
67    let resp = configuration.client.execute(req).await?;
68
69    let status = resp.status();
70    let content_type = resp
71        .headers()
72        .get("content-type")
73        .and_then(|v| v.to_str().ok())
74        .unwrap_or("application/octet-stream");
75    let content_type = super::ContentType::from(content_type);
76
77    if !status.is_client_error() && !status.is_server_error() {
78        let content = resp.text().await?;
79        match content_type {
80            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
81            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::PricingResponse`"))),
82            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::PricingResponse`")))),
83        }
84    } else {
85        let content = resp.text().await?;
86        let entity: Option<GetPricingError> = serde_json::from_str(&content).ok();
87        Err(Error::ResponseError(ResponseContent { status, content, entity }))
88    }
89}
90