/*
 * 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_account_account_id_alert_activate_post`
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum IserverAccountAccountIdAlertActivatePostError {
    UnknownValue(serde_json::Value),
}

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

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

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

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

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


/// Please note, if alertId is 0, it will activate/deactivate all alerts
pub async fn iserver_account_account_id_alert_activate_post(configuration: &configuration::Configuration, account_id: &str, body: crate::models::InlineObject2) -> Result<crate::models::InlineResponse20010, Error<IserverAccountAccountIdAlertActivatePostError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/{accountId}/alert/activate", configuration.base_path, accountId=crate::apis::urlencode(account_id));
    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<IserverAccountAccountIdAlertActivatePostError> = 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))
    }
}

/// Please be careful, if alertId is 0, it will delete all alerts
pub async fn iserver_account_account_id_alert_alert_id_delete(configuration: &configuration::Configuration, account_id: &str, alert_id: &str) -> Result<crate::models::InlineResponse20010, Error<IserverAccountAccountIdAlertAlertIdDeleteError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/{accountId}/alert/{alertId}", configuration.base_path, accountId=crate::apis::urlencode(account_id), alertId=crate::apis::urlencode(alert_id));
    let mut local_var_req_builder = local_var_client.delete(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<IserverAccountAccountIdAlertAlertIdDeleteError> = 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))
    }
}

/// Please note here, DO NOT pass orderId when creating a new alert, toolId is only required for MTA alert 
pub async fn iserver_account_account_id_alert_post(configuration: &configuration::Configuration, account_id: &str, body: crate::models::AlertRequest) -> Result<crate::models::InlineResponse2008, Error<IserverAccountAccountIdAlertPostError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/{accountId}/alert", configuration.base_path, accountId=crate::apis::urlencode(account_id));
    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<IserverAccountAccountIdAlertPostError> = 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))
    }
}

/// The response will contain both active and inactive alerts, but it won't have MTA alert
pub async fn iserver_account_account_id_alerts_get(configuration: &configuration::Configuration, account_id: &str) -> Result<Vec<crate::models::InlineResponse2009>, Error<IserverAccountAccountIdAlertsGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/{accountId}/alerts", configuration.base_path, accountId=crate::apis::urlencode(account_id));
    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<IserverAccountAccountIdAlertsGetError> = 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))
    }
}

/// Use the endpoint /iserver/account/:accountId/alerts to receive the alert id. The order_id in the response is the alert id. 
pub async fn iserver_account_alert_id_get(configuration: &configuration::Configuration, id: &str) -> Result<crate::models::AlertResponse, Error<IserverAccountAlertIdGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/alert/{id}", configuration.base_path, id=crate::apis::urlencode(id));
    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<IserverAccountAlertIdGetError> = 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))
    }
}

/// Each login user only has one mobile trading assistant (MTA) alert with it's own unique tool id. The tool id cannot be changed. When modified a new order Id is generated. MTA alerts can not be created or deleted. If you call delete /iserver/account/:accountId/alert/:alertId, it will reset MTA to default. See [here](https://www.interactivebrokers.com/en/software/mobileiphonemobile/mobileiphone.htm#monitor/trading-assistant.htm) for more information on MTA alerts. 
pub async fn iserver_account_mta_get(configuration: &configuration::Configuration, ) -> Result<crate::models::AlertResponse, Error<IserverAccountMtaGetError>> {

    let local_var_client = &configuration.client;

    let local_var_uri_str = format!("{}/iserver/account/mta", configuration.base_path);
    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<IserverAccountMtaGetError> = 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))
    }
}

