/*
 * Client Portal Web API
 *
 * Client Portal Web API
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;

use crate::apis::ResponseContent;
use super::{Error, configuration};


/// struct for typed errors of method `iserver_contract_conid_info_and_rules_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverContractConidInfoAndRulesGetError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `iserver_contract_conid_info_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverContractConidInfoGetError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `iserver_secdef_info_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverSecdefInfoGetError {
    Status500(crate::models::InlineResponse429),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `iserver_secdef_search_post`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverSecdefSearchPostError {
    Status500(crate::models::InlineResponse429),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `iserver_secdef_strikes_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverSecdefStrikesGetError {
    Status500(crate::models::InlineResponse429),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `trsrv_futures_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TrsrvFuturesGetError {
    Status500(crate::models::InlineResponse429),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `trsrv_secdef_post`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TrsrvSecdefPostError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `trsrv_secdef_schedule_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TrsrvSecdefScheduleGetError {
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method `trsrv_stocks_get`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TrsrvStocksGetError {
    Status500(crate::models::InlineResponse429),
    UnknownValue(serde_json::Value),
}


/// Returns trading related rules and info for contract
pub async fn iserver_contract_conid_info_and_rules_get(configuration: &configuration::Configuration, conid: &str, is_buy: bool) -> Result<crate::models::InlineResponse20022, Error<IserverContractConidInfoAndRulesGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/contract/{conid}/info-and-rules", configuration.base_path, conid=crate::apis::urlencode(conid));
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("isBuy", &is_buy.to_string())]);
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<IserverContractConidInfoAndRulesGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Using the Contract Identifier get contract info. You can use this to prefill your order before you submit an order
pub async fn iserver_contract_conid_info_get(configuration: &configuration::Configuration, conid: &str) -> Result<crate::models::Contract, Error<IserverContractConidInfoGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/contract/{conid}/info", configuration.base_path, conid=crate::apis::urlencode(conid));
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<IserverContractConidInfoGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Provides Contract Details of Futures, Options, Warrants, Cash and CFDs based on conid. To get the strike price for Options/Warrants use \"/iserver/secdef/strikes\" endpoint. Must call /secdef/search for the underlying contract first.
pub async fn iserver_secdef_info_get(configuration: &configuration::Configuration, conid: &str, sectype: &str, month: Option<&str>, exchange: Option<&str>, strike: Option<&str>, right: Option<&str>) -> Result<serde_json::Value, Error<IserverSecdefInfoGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/secdef/info", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("conid", &conid.to_string())]);
    local_var_req_builder = local_var_req_builder.query(&[("sectype", &sectype.to_string())]);
    if let Some(ref local_var_str) = month {
        local_var_req_builder = local_var_req_builder.query(&[("month", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = exchange {
        local_var_req_builder = local_var_req_builder.query(&[("exchange", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = strike {
        local_var_req_builder = local_var_req_builder.query(&[("strike", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = right {
        local_var_req_builder = local_var_req_builder.query(&[("right", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<IserverSecdefInfoGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Search by underlying or company name. Relays back what derivative contract(s) it has. This endpoint must be called before using /secdef/info
pub async fn iserver_secdef_search_post(configuration: &configuration::Configuration, symbol: crate::models::InlineObject6) -> Result<Vec<crate::models::InlineResponse20020>, Error<IserverSecdefSearchPostError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/secdef/search", configuration.base_path);
    let mut local_var_req_builder = local_var_client.post(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    local_var_req_builder = local_var_req_builder.json(&symbol);

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<IserverSecdefSearchPostError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Query strikes for Options/Warrants. For available contract months and exchanges use \"/iserver/secdef/search\"
pub async fn iserver_secdef_strikes_get(configuration: &configuration::Configuration, conid: &str, sectype: &str, month: &str, exchange: Option<&str>) -> Result<crate::models::InlineResponse20021, Error<IserverSecdefStrikesGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/secdef/strikes", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("conid", &conid.to_string())]);
    local_var_req_builder = local_var_req_builder.query(&[("sectype", &sectype.to_string())]);
    local_var_req_builder = local_var_req_builder.query(&[("month", &month.to_string())]);
    if let Some(ref local_var_str) = exchange {
        local_var_req_builder = local_var_req_builder.query(&[("exchange", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<IserverSecdefStrikesGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Returns a list of non-expired future contracts for given symbol(s)
pub async fn trsrv_futures_get(configuration: &configuration::Configuration, symbols: &str) -> Result<crate::models::InlineResponse20027, Error<TrsrvFuturesGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/trsrv/futures", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("symbols", &symbols.to_string())]);
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<TrsrvFuturesGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Returns a list of security definitions for the given conids
pub async fn trsrv_secdef_post(configuration: &configuration::Configuration, body: crate::models::InlineObject7) -> Result<Vec<serde_json::Value>, Error<TrsrvSecdefPostError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/trsrv/secdef", configuration.base_path);
    let mut local_var_req_builder = local_var_client.post(local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    local_var_req_builder = local_var_req_builder.json(&body);

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<TrsrvSecdefPostError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Returns the trading schedule up to a month for the requested contract
pub async fn trsrv_secdef_schedule_get(configuration: &configuration::Configuration, asset_class: &str, symbol: &str, exchange: Option<&str>) -> Result<crate::models::InlineResponse20026, Error<TrsrvSecdefScheduleGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/trsrv/secdef/schedule", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("assetClass", &asset_class.to_string())]);
    local_var_req_builder = local_var_req_builder.query(&[("symbol", &symbol.to_string())]);
    if let Some(ref local_var_str) = exchange {
        local_var_req_builder = local_var_req_builder.query(&[("exchange", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<TrsrvSecdefScheduleGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Returns an object contains all stock contracts for given symbol(s)
pub async fn trsrv_stocks_get(configuration: &configuration::Configuration, symbols: &str) -> Result<crate::models::InlineResponse20028, Error<TrsrvStocksGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/trsrv/stocks", configuration.base_path);
    let mut local_var_req_builder = local_var_client.get(local_var_uri_str.as_str());

    local_var_req_builder = local_var_req_builder.query(&[("symbols", &symbols.to_string())]);
    if let Some(ref local_var_user_agent) = configuration.user_agent {
        local_var_req_builder = local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<TrsrvStocksGetError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent { status: local_var_status, content: local_var_content, entity: local_var_entity };
        Err(Error::ResponseError(local_var_error))
    }
}

