oanda-v20-openapi 0.2.1

The full OANDA v20 API Specification. This specification defines how to interact with v20 Accounts, Trades, Orders, Pricing and more.
Documentation
/*
 * OANDA v20 API
 *
 * The full OANDA v20 API Specification. This specification defines how to interact with v20 Accounts, Trades, Orders, Pricing and more.
 *
 * The version of the OpenAPI document: 0.2.1
 * Contact: jmicoud02@gmail.com
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};


/// struct for typed errors of method [`get_instrument_candles`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetInstrumentCandlesError {
    Status400(models::Error400),
    Status401(models::Error401),
    Status404(models::Error404),
    Status405(models::Error405),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_instrument_order_book`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetInstrumentOrderBookError {
    Status400(models::Error400),
    Status401(models::Error401),
    Status404(models::Error404),
    Status405(models::Error405),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`get_instrument_position_book`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetInstrumentPositionBookError {
    Status400(models::Error400),
    Status401(models::Error401),
    Status404(models::Error404),
    Status405(models::Error405),
    UnknownValue(serde_json::Value),
}


/// Fetch candlestick data for an instrument.
pub 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>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_instrument = instrument;
    let p_accept_datetime_format = accept_datetime_format;
    let p_price = price;
    let p_granularity = granularity;
    let p_count = count;
    let p_from = from;
    let p_to = to;
    let p_smooth = smooth;
    let p_include_first = include_first;
    let p_daily_alignment = daily_alignment;
    let p_alignment_timezone = alignment_timezone;
    let p_weekly_alignment = weekly_alignment;

    let uri_str = format!("{}/instruments/{instrument}/candles", configuration.base_path, instrument=p_instrument.to_string());
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_price {
        req_builder = req_builder.query(&[("price", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_granularity {
        req_builder = req_builder.query(&[("granularity", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_count {
        req_builder = req_builder.query(&[("count", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_from {
        req_builder = req_builder.query(&[("from", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_to {
        req_builder = req_builder.query(&[("to", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_smooth {
        req_builder = req_builder.query(&[("smooth", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_include_first {
        req_builder = req_builder.query(&[("includeFirst", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_daily_alignment {
        req_builder = req_builder.query(&[("dailyAlignment", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_alignment_timezone {
        req_builder = req_builder.query(&[("alignmentTimezone", &param_value.to_string())]);
    }
    if let Some(ref param_value) = p_weekly_alignment {
        req_builder = req_builder.query(&[("weeklyAlignment", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_accept_datetime_format {
        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CandlesResponse`"))),
            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`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetInstrumentCandlesError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Fetch an order book for an instrument.
pub 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>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_instrument = instrument;
    let p_accept_datetime_format = accept_datetime_format;
    let p_time = time;

    let uri_str = format!("{}/instruments/{instrument}/orderBook", configuration.base_path, instrument=p_instrument.to_string());
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_time {
        req_builder = req_builder.query(&[("time", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_accept_datetime_format {
        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstrumentOrderBookResponse`"))),
            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`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetInstrumentOrderBookError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Fetch a position book for an instrument.
pub 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>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_instrument = instrument;
    let p_accept_datetime_format = accept_datetime_format;
    let p_time = time;

    let uri_str = format!("{}/instruments/{instrument}/positionBook", configuration.base_path, instrument=p_instrument.to_string());
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    if let Some(ref param_value) = p_time {
        req_builder = req_builder.query(&[("time", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    if let Some(param_value) = p_accept_datetime_format {
        req_builder = req_builder.header("Accept-Datetime-Format", param_value.to_string());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::InstrumentPositionBookResponse`"))),
            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`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetInstrumentPositionBookError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}