openxapi-binance 0.1.2

Rust client for Binance API
Documentation
/*
 * Binance Sub Account API
 *
 * OpenAPI specification for Binance exchange - Subaccount API
 *
 * The version of the OpenAPI document: 0.1.0
 * 
 * Generated by: https://openapi-generator.tech
 */


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

/// struct for passing parameters to the method [`subaccount_create_sub_account_sub_account_api_ip_restriction_v2`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Params {
    pub email: String,
    pub status: String,
    pub sub_account_api_key: String,
    pub timestamp: i64,
    pub ip_address: Option<String>,
    pub recv_window: Option<i64>
}

/// struct for passing parameters to the method [`subaccount_delete_sub_account_sub_account_api_ip_restriction_ip_list_v1`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Params {
    /// <a href=\"/docs/sub_account/api-management/Delete-IP-List-For-a-Sub-account-API-Key#email-address\">Sub-account email</a>
    pub email: String,
    pub sub_account_api_key: String,
    pub timestamp: i64,
    /// Can be added in batches, separated by commas
    pub ip_address: Option<String>,
    pub recv_window: Option<i64>
}

/// struct for passing parameters to the method [`subaccount_get_sub_account_sub_account_api_ip_restriction_v1`]
#[derive(Clone, Debug, Default)]
pub struct SubaccountGetSubAccountSubAccountApiIpRestrictionV1Params {
    /// <a href=\"/docs/sub_account/api-management#email-address\">Sub-account email</a>
    pub email: String,
    pub sub_account_api_key: String,
    pub timestamp: i64,
    pub recv_window: Option<i64>
}


/// struct for typed errors of method [`subaccount_create_sub_account_sub_account_api_ip_restriction_v2`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Error {
    Status4XX(models::ApiError),
    Status5XX(models::ApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`subaccount_delete_sub_account_sub_account_api_ip_restriction_ip_list_v1`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Error {
    Status4XX(models::ApiError),
    Status5XX(models::ApiError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`subaccount_get_sub_account_sub_account_api_ip_restriction_v1`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubaccountGetSubAccountSubAccountApiIpRestrictionV1Error {
    Status4XX(models::ApiError),
    Status5XX(models::ApiError),
    UnknownValue(serde_json::Value),
}


/// Add IP Restriction for Sub-Account API key
pub async fn subaccount_create_sub_account_sub_account_api_ip_restriction_v2(configuration: &configuration::Configuration, params: SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Params) -> Result<models::SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Resp, Error<SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Error>> {

    let uri_str = format!("{}/sapi/v2/sub-account/subAccountApi/ipRestriction", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    // Create a mutable vector for query parameters
    let mut query_params: Vec<(String, String)> = Vec::new();


    // Create header parameters collection
    let mut header_params = std::collections::HashMap::new();

    // Handle Binance Auth first if configured
    if let Some(ref binance_auth) = configuration.binance_auth {
        // Add API key to headers
        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
        
        // Generate request body for signing (if any)
        let body_string: Option<Vec<u8>> = None;
        
        // Sign the request
        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
            Ok(sig) => sig,
            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
        };
        
        // Add signature to query params
        query_params.push(("signature".to_string(), signature));
    }

    // Apply all query parameters
    if !query_params.is_empty() {
        req_builder = req_builder.query(&query_params);
    }


    // Add user agent if configured
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    // Apply all header parameters
    for (header_name, header_value) in header_params {
        req_builder = req_builder.header(&header_name, &header_value);
    }

    let mut multipart_form_params = std::collections::HashMap::new();
    multipart_form_params.insert("email", params.email.to_string());
    if let Some(param_value) = params.ip_address {
        multipart_form_params.insert("ipAddress", param_value.to_string());
    }
    if let Some(param_value) = params.recv_window {
        multipart_form_params.insert("recvWindow", param_value.to_string());
    }
    multipart_form_params.insert("status", params.status.to_string());
    multipart_form_params.insert("subAccountApiKey", params.sub_account_api_key.to_string());
    multipart_form_params.insert("timestamp", params.timestamp.to_string());
    req_builder = req_builder.form(&multipart_form_params);

    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::SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Resp`"))),
            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::SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Resp`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountCreateSubAccountSubAccountApiIpRestrictionV2Error> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Delete IP List For a Sub-account API Key
pub async fn subaccount_delete_sub_account_sub_account_api_ip_restriction_ip_list_v1(configuration: &configuration::Configuration, params: SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Params) -> Result<models::SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Resp, Error<SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Error>> {

    let uri_str = format!("{}/sapi/v1/sub-account/subAccountApi/ipRestriction/ipList", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);

    // Create a mutable vector for query parameters
    let mut query_params: Vec<(String, String)> = Vec::new();

    query_params.push(("email".to_string(), params.email.to_string()));
    query_params.push(("subAccountApiKey".to_string(), params.sub_account_api_key.to_string()));
    if let Some(ref param_value) = params.ip_address {
        query_params.push(("ipAddress".to_string(), param_value.to_string()));
    }
    if let Some(ref param_value) = params.recv_window {
        query_params.push(("recvWindow".to_string(), param_value.to_string()));
    }
    query_params.push(("timestamp".to_string(), params.timestamp.to_string()));

    // Create header parameters collection
    let mut header_params = std::collections::HashMap::new();

    // Handle Binance Auth first if configured
    if let Some(ref binance_auth) = configuration.binance_auth {
        // Add API key to headers
        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
        
        // Generate request body for signing (if any)
        let body_string: Option<Vec<u8>> = None;
        
        // Sign the request
        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
            Ok(sig) => sig,
            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
        };
        
        // Add signature to query params
        query_params.push(("signature".to_string(), signature));
    }

    // Apply all query parameters
    if !query_params.is_empty() {
        req_builder = req_builder.query(&query_params);
    }


    // Add user agent if configured
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    // Apply all header parameters
    for (header_name, header_value) in header_params {
        req_builder = req_builder.header(&header_name, &header_value);
    }


    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::SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Resp`"))),
            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::SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Resp`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountDeleteSubAccountSubAccountApiIpRestrictionIpListV1Error> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Get IP Restriction for a Sub-account API Key
pub async fn subaccount_get_sub_account_sub_account_api_ip_restriction_v1(configuration: &configuration::Configuration, params: SubaccountGetSubAccountSubAccountApiIpRestrictionV1Params) -> Result<models::SubaccountGetSubAccountSubAccountApiIpRestrictionV1Resp, Error<SubaccountGetSubAccountSubAccountApiIpRestrictionV1Error>> {

    let uri_str = format!("{}/sapi/v1/sub-account/subAccountApi/ipRestriction", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    // Create a mutable vector for query parameters
    let mut query_params: Vec<(String, String)> = Vec::new();

    query_params.push(("email".to_string(), params.email.to_string()));
    query_params.push(("subAccountApiKey".to_string(), params.sub_account_api_key.to_string()));
    if let Some(ref param_value) = params.recv_window {
        query_params.push(("recvWindow".to_string(), param_value.to_string()));
    }
    query_params.push(("timestamp".to_string(), params.timestamp.to_string()));

    // Create header parameters collection
    let mut header_params = std::collections::HashMap::new();

    // Handle Binance Auth first if configured
    if let Some(ref binance_auth) = configuration.binance_auth {
        // Add API key to headers
        header_params.insert("X-MBX-APIKEY".to_string(), binance_auth.api_key().to_string());
        
        // Generate request body for signing (if any)
        let body_string: Option<Vec<u8>> = None;
        
        // Sign the request
        let signature = match binance_auth.sign(Some(&query_params), body_string.as_deref()) {
            Ok(sig) => sig,
            Err(e) => return Err(Error::Generic(format!("Failed to sign request: {}", e))),
        };
        
        // Add signature to query params
        query_params.push(("signature".to_string(), signature));
    }

    // Apply all query parameters
    if !query_params.is_empty() {
        req_builder = req_builder.query(&query_params);
    }


    // Add user agent if configured
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    // Apply all header parameters
    for (header_name, header_value) in header_params {
        req_builder = req_builder.header(&header_name, &header_value);
    }


    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::SubaccountGetSubAccountSubAccountApiIpRestrictionV1Resp`"))),
            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::SubaccountGetSubAccountSubAccountApiIpRestrictionV1Resp`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubaccountGetSubAccountSubAccountApiIpRestrictionV1Error> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}