binance-sdk 61.0.0

This is a lightweight library that works as a connector to the Binance public API.
Documentation
/*
 * C2C REST API
 *
 * Query fiat transaction history via the C2C REST API.
 *
 * The version of the OpenAPI document: 1.0.0
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

#![allow(unused_imports)]
use async_trait::async_trait;
use derive_builder::Builder;
use reqwest;
use rust_decimal::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use std::collections::BTreeMap;

use crate::c2c::rest_api::models;
use crate::common::{
    config::ConfigurationRestApi,
    models::{ParamBuildError, RestApiResponse},
    utils::send_request,
};

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait Api: Send + Sync {
    async fn get_c2_c_trade_history(
        &self,
        params: GetC2CTradeHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetC2CTradeHistoryResponse>>;
}

#[derive(Debug, Clone)]
pub struct ApiClient {
    configuration: ConfigurationRestApi,
}

impl ApiClient {
    pub fn new(configuration: ConfigurationRestApi) -> Self {
        Self { configuration }
    }
}

#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GetC2CTradeHistoryTradeTypeEnum {
    #[serde(rename = "BUY")]
    Buy,
    #[serde(rename = "SELL")]
    Sell,
}

impl GetC2CTradeHistoryTradeTypeEnum {
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Buy => "BUY",
            Self::Sell => "SELL",
        }
    }
}

impl std::str::FromStr for GetC2CTradeHistoryTradeTypeEnum {
    type Err = Box<dyn std::error::Error + Send + Sync>;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "BUY" => Ok(Self::Buy),
            "SELL" => Ok(Self::Sell),
            other => Err(format!("invalid GetC2CTradeHistoryTradeTypeEnum: {}", other).into()),
        }
    }
}

/// Request parameters for the [`get_c2_c_trade_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_c2_c_trade_history`](#method.get_c2_c_trade_history).
#[derive(Clone, Debug, Builder, Deserialize, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetC2CTradeHistoryParams {
    /// Trade side filter
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    #[serde(rename = "tradeType", default)]
    pub trade_type: Option<GetC2CTradeHistoryTradeTypeEnum>,
    ///
    /// The `start_timestamp` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    #[serde(rename = "startTimestamp", default)]
    pub start_timestamp: Option<i64>,
    ///
    /// The `end_timestamp` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    #[serde(rename = "endTimestamp", default)]
    pub end_timestamp: Option<i64>,
    /// Page number
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    #[serde(rename = "page", default)]
    pub page: Option<i64>,
    /// Number of records per page
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    #[serde(rename = "rows", default)]
    pub rows: Option<i64>,
}

impl GetC2CTradeHistoryParams {
    /// Create a builder for [`get_c2_c_trade_history`].
    ///
    #[must_use]
    pub fn builder() -> GetC2CTradeHistoryParamsBuilder {
        GetC2CTradeHistoryParamsBuilder::default()
    }
}

#[async_trait]
impl Api for ApiClient {
    async fn get_c2_c_trade_history(
        &self,
        params: GetC2CTradeHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetC2CTradeHistoryResponse>> {
        let GetC2CTradeHistoryParams {
            trade_type,
            start_timestamp,
            end_timestamp,
            page,
            rows,
        } = params;

        let mut query_params = BTreeMap::new();
        let body_params = BTreeMap::new();

        if let Some(rw) = trade_type {
            query_params.insert("tradeType".to_string(), json!(rw));
        }

        if let Some(rw) = start_timestamp {
            query_params.insert("startTimestamp".to_string(), json!(rw));
        }

        if let Some(rw) = end_timestamp {
            query_params.insert("endTimestamp".to_string(), json!(rw));
        }

        if let Some(rw) = page {
            query_params.insert("page".to_string(), json!(rw));
        }

        if let Some(rw) = rows {
            query_params.insert("rows".to_string(), json!(rw));
        }

        send_request::<models::GetC2CTradeHistoryResponse>(
            &self.configuration,
            "/sapi/v1/c2c/orderMatch/listUserOrderHistory",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            false,
        )
        .await
    }
}

#[cfg(all(test, feature = "c2c"))]
mod tests {
    use super::*;
    use crate::TOKIO_SHARED_RT;
    use crate::{errors::ConnectorError, models::DataFuture, models::RestApiRateLimit};
    use async_trait::async_trait;
    use std::collections::HashMap;

    struct DummyRestApiResponse<T> {
        inner: Box<dyn FnOnce() -> DataFuture<Result<T, ConnectorError>> + Send + Sync>,
        status: u16,
        headers: HashMap<String, String>,
        rate_limits: Option<Vec<RestApiRateLimit>>,
    }

    impl<T> From<DummyRestApiResponse<T>> for RestApiResponse<T> {
        fn from(dummy: DummyRestApiResponse<T>) -> Self {
            Self {
                data_fn: dummy.inner,
                status: dummy.status,
                headers: dummy.headers,
                rate_limits: dummy.rate_limits,
            }
        }
    }

    struct MockApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl Api for MockApiClient {
        async fn get_c2_c_trade_history(
            &self,
            _params: GetC2CTradeHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetC2CTradeHistoryResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"orderNumber":"20219644646554779648","advNo":"11218246497340923904","tradeType":"SELL","asset":"BUSD","fiat":"CNY","fiatSymbol":"¥","amount":"5000.00000000","totalPrice":"33400.00000000","unitPrice":"6.68","orderStatus":"COMPLETED","createTime":1619361369000,"commission":"0","counterPartNickName":"阿涛❤***","payMethodName":"BANK","additionalKycVerify":0,"takerCommissionRate":"0","takerCommission":"0","takerAmount":"343.4","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap_or_else(|_| serde_json::json!({}));
            let dummy_response: models::GetC2CTradeHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetC2CTradeHistoryResponse");

            let dummy = DummyRestApiResponse {
                inner: Box::new(move || Box::pin(async move { Ok(dummy_response) })),
                status: 200,
                headers: HashMap::new(),
                rate_limits: None,
            };

            Ok(dummy.into())
        }
    }

    #[test]
    fn get_c2_c_trade_history_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockApiClient { force_error: false };

            let params = GetC2CTradeHistoryParams::builder().build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"orderNumber":"20219644646554779648","advNo":"11218246497340923904","tradeType":"SELL","asset":"BUSD","fiat":"CNY","fiatSymbol":"¥","amount":"5000.00000000","totalPrice":"33400.00000000","unitPrice":"6.68","orderStatus":"COMPLETED","createTime":1619361369000,"commission":"0","counterPartNickName":"阿涛❤***","payMethodName":"BANK","additionalKycVerify":0,"takerCommissionRate":"0","takerCommission":"0","takerAmount":"343.4","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap_or_else(|_| serde_json::json!({}));
            let expected_response : models::GetC2CTradeHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetC2CTradeHistoryResponse");

            let resp = client.get_c2_c_trade_history(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_c2_c_trade_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockApiClient { force_error: false };

            let params = GetC2CTradeHistoryParams::builder().trade_type(GetC2CTradeHistoryTradeTypeEnum::Buy).start_timestamp(1770736694138).end_timestamp(1770736694138).page(1).rows(100).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"code":"000000","message":"success","data":[{"orderNumber":"20219644646554779648","advNo":"11218246497340923904","tradeType":"SELL","asset":"BUSD","fiat":"CNY","fiatSymbol":"¥","amount":"5000.00000000","totalPrice":"33400.00000000","unitPrice":"6.68","orderStatus":"COMPLETED","createTime":1619361369000,"commission":"0","counterPartNickName":"阿涛❤***","payMethodName":"BANK","additionalKycVerify":0,"takerCommissionRate":"0","takerCommission":"0","takerAmount":"343.4","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap_or_else(|_| serde_json::json!({}));
            let expected_response : models::GetC2CTradeHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetC2CTradeHistoryResponse");

            let resp = client.get_c2_c_trade_history(params).await.expect("Expected a response");
            let data_future = resp.data();
            let actual_response = data_future.await.unwrap();
            assert_eq!(actual_response, expected_response);
        });
    }

    #[test]
    fn get_c2_c_trade_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockApiClient { force_error: true };

            let params = GetC2CTradeHistoryParams::builder().build().unwrap();

            match client.get_c2_c_trade_history(params).await {
                Ok(_) => panic!("Expected an error"),
                Err(err) => {
                    assert_eq!(err.to_string(), "Connector client error: ResponseError");
                }
            }
        });
    }
}