/*
 * Binance Crypto Loan REST API
 *
 * OpenAPI Specification for the Binance Crypto Loan 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::common::{
    config::ConfigurationRestApi,
    models::{ParamBuildError, RestApiResponse},
    utils::send_request,
};
use crate::crypto_loan::rest_api::models;

const HAS_TIME_UNIT: bool = false;

#[async_trait]
pub trait StableRateApi: Send + Sync {
    async fn check_collateral_repay_rate_stable_rate(
        &self,
        params: CheckCollateralRepayRateStableRateParams,
    ) -> anyhow::Result<RestApiResponse<models::CheckCollateralRepayRateStableRateResponse>>;
    async fn get_crypto_loans_income_history(
        &self,
        params: GetCryptoLoansIncomeHistoryParams,
    ) -> anyhow::Result<RestApiResponse<Vec<models::GetCryptoLoansIncomeHistoryResponseInner>>>;
    async fn get_loan_borrow_history(
        &self,
        params: GetLoanBorrowHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanBorrowHistoryResponse>>;
    async fn get_loan_ltv_adjustment_history(
        &self,
        params: GetLoanLtvAdjustmentHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanLtvAdjustmentHistoryResponse>>;
    async fn get_loan_repayment_history(
        &self,
        params: GetLoanRepaymentHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanRepaymentHistoryResponse>>;
}

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

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

/// Request parameters for the [`check_collateral_repay_rate_stable_rate`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`check_collateral_repay_rate_stable_rate`](#method.check_collateral_repay_rate_stable_rate).
#[derive(Clone, Debug, Builder)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct CheckCollateralRepayRateStableRateParams {
    ///
    /// The `loan_coin` parameter.
    ///
    /// This field is **required.
    #[builder(setter(into))]
    pub loan_coin: String,
    ///
    /// The `collateral_coin` parameter.
    ///
    /// This field is **required.
    #[builder(setter(into))]
    pub collateral_coin: String,
    /// repay amount of loanCoin
    ///
    /// This field is **required.
    #[builder(setter(into))]
    pub repay_amount: rust_decimal::Decimal,
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl CheckCollateralRepayRateStableRateParams {
    /// Create a builder for [`check_collateral_repay_rate_stable_rate`].
    ///
    /// Required parameters:
    ///
    /// * `loan_coin` — String
    /// * `collateral_coin` — String
    /// * `repay_amount` — repay amount of loanCoin
    ///
    #[must_use]
    pub fn builder(
        loan_coin: String,
        collateral_coin: String,
        repay_amount: rust_decimal::Decimal,
    ) -> CheckCollateralRepayRateStableRateParamsBuilder {
        CheckCollateralRepayRateStableRateParamsBuilder::default()
            .loan_coin(loan_coin)
            .collateral_coin(collateral_coin)
            .repay_amount(repay_amount)
    }
}
/// Request parameters for the [`get_crypto_loans_income_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_crypto_loans_income_history`](#method.get_crypto_loans_income_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetCryptoLoansIncomeHistoryParams {
    ///
    /// The `asset` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub asset: Option<String>,
    /// All types will be returned by default. Enum：`borrowIn` ,`collateralSpent`, `repayAmount`, `collateralReturn`(Collateral return after repayment), `addCollateral`, `removeCollateral`, `collateralReturnAfterLiquidation`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub r#type: Option<String>,
    ///
    /// The `start_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Default: 10; max: 100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub limit: Option<i64>,
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetCryptoLoansIncomeHistoryParams {
    /// Create a builder for [`get_crypto_loans_income_history`].
    ///
    #[must_use]
    pub fn builder() -> GetCryptoLoansIncomeHistoryParamsBuilder {
        GetCryptoLoansIncomeHistoryParamsBuilder::default()
    }
}
/// Request parameters for the [`get_loan_borrow_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_loan_borrow_history`](#method.get_loan_borrow_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetLoanBorrowHistoryParams {
    /// orderId in `POST /sapi/v1/loan/borrow`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub order_id: Option<i64>,
    ///
    /// The `loan_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub loan_coin: Option<String>,
    ///
    /// The `collateral_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub collateral_coin: Option<String>,
    ///
    /// The `start_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Current querying page. Start from 1; default: 1; max: 1000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub current: Option<i64>,
    /// Default: 10; max: 100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub limit: Option<i64>,
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetLoanBorrowHistoryParams {
    /// Create a builder for [`get_loan_borrow_history`].
    ///
    #[must_use]
    pub fn builder() -> GetLoanBorrowHistoryParamsBuilder {
        GetLoanBorrowHistoryParamsBuilder::default()
    }
}
/// Request parameters for the [`get_loan_ltv_adjustment_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_loan_ltv_adjustment_history`](#method.get_loan_ltv_adjustment_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetLoanLtvAdjustmentHistoryParams {
    /// orderId in `POST /sapi/v1/loan/borrow`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub order_id: Option<i64>,
    ///
    /// The `loan_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub loan_coin: Option<String>,
    ///
    /// The `collateral_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub collateral_coin: Option<String>,
    ///
    /// The `start_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Current querying page. Start from 1; default: 1; max: 1000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub current: Option<i64>,
    /// Default: 10; max: 100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub limit: Option<i64>,
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetLoanLtvAdjustmentHistoryParams {
    /// Create a builder for [`get_loan_ltv_adjustment_history`].
    ///
    #[must_use]
    pub fn builder() -> GetLoanLtvAdjustmentHistoryParamsBuilder {
        GetLoanLtvAdjustmentHistoryParamsBuilder::default()
    }
}
/// Request parameters for the [`get_loan_repayment_history`] operation.
///
/// This struct holds all of the inputs you can pass when calling
/// [`get_loan_repayment_history`](#method.get_loan_repayment_history).
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetLoanRepaymentHistoryParams {
    /// orderId in `POST /sapi/v1/loan/borrow`
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub order_id: Option<i64>,
    ///
    /// The `loan_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub loan_coin: Option<String>,
    ///
    /// The `collateral_coin` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub collateral_coin: Option<String>,
    ///
    /// The `start_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub start_time: Option<i64>,
    ///
    /// The `end_time` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub end_time: Option<i64>,
    /// Current querying page. Start from 1; default: 1; max: 1000
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub current: Option<i64>,
    /// Default: 10; max: 100
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub limit: Option<i64>,
    ///
    /// The `recv_window` parameter.
    ///
    /// This field is **optional.
    #[builder(setter(into), default)]
    pub recv_window: Option<i64>,
}

impl GetLoanRepaymentHistoryParams {
    /// Create a builder for [`get_loan_repayment_history`].
    ///
    #[must_use]
    pub fn builder() -> GetLoanRepaymentHistoryParamsBuilder {
        GetLoanRepaymentHistoryParamsBuilder::default()
    }
}

#[async_trait]
impl StableRateApi for StableRateApiClient {
    async fn check_collateral_repay_rate_stable_rate(
        &self,
        params: CheckCollateralRepayRateStableRateParams,
    ) -> anyhow::Result<RestApiResponse<models::CheckCollateralRepayRateStableRateResponse>> {
        let CheckCollateralRepayRateStableRateParams {
            loan_coin,
            collateral_coin,
            repay_amount,
            recv_window,
        } = params;

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

        query_params.insert("loanCoin".to_string(), json!(loan_coin));

        query_params.insert("collateralCoin".to_string(), json!(collateral_coin));

        query_params.insert("repayAmount".to_string(), json!(repay_amount));

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

        send_request::<models::CheckCollateralRepayRateStableRateResponse>(
            &self.configuration,
            "/sapi/v1/loan/repay/collateral/rate",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn get_crypto_loans_income_history(
        &self,
        params: GetCryptoLoansIncomeHistoryParams,
    ) -> anyhow::Result<RestApiResponse<Vec<models::GetCryptoLoansIncomeHistoryResponseInner>>>
    {
        let GetCryptoLoansIncomeHistoryParams {
            asset,
            r#type,
            start_time,
            end_time,
            limit,
            recv_window,
        } = params;

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

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

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

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

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

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

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

        send_request::<Vec<models::GetCryptoLoansIncomeHistoryResponseInner>>(
            &self.configuration,
            "/sapi/v1/loan/income",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn get_loan_borrow_history(
        &self,
        params: GetLoanBorrowHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanBorrowHistoryResponse>> {
        let GetLoanBorrowHistoryParams {
            order_id,
            loan_coin,
            collateral_coin,
            start_time,
            end_time,
            current,
            limit,
            recv_window,
        } = params;

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

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

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

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

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

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

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

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

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

        send_request::<models::GetLoanBorrowHistoryResponse>(
            &self.configuration,
            "/sapi/v1/loan/borrow/history",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn get_loan_ltv_adjustment_history(
        &self,
        params: GetLoanLtvAdjustmentHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanLtvAdjustmentHistoryResponse>> {
        let GetLoanLtvAdjustmentHistoryParams {
            order_id,
            loan_coin,
            collateral_coin,
            start_time,
            end_time,
            current,
            limit,
            recv_window,
        } = params;

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

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

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

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

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

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

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

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

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

        send_request::<models::GetLoanLtvAdjustmentHistoryResponse>(
            &self.configuration,
            "/sapi/v1/loan/ltv/adjustment/history",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }

    async fn get_loan_repayment_history(
        &self,
        params: GetLoanRepaymentHistoryParams,
    ) -> anyhow::Result<RestApiResponse<models::GetLoanRepaymentHistoryResponse>> {
        let GetLoanRepaymentHistoryParams {
            order_id,
            loan_coin,
            collateral_coin,
            start_time,
            end_time,
            current,
            limit,
            recv_window,
        } = params;

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

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

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

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

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

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

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

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

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

        send_request::<models::GetLoanRepaymentHistoryResponse>(
            &self.configuration,
            "/sapi/v1/loan/repay/history",
            reqwest::Method::GET,
            query_params,
            body_params,
            if HAS_TIME_UNIT {
                self.configuration.time_unit
            } else {
                None
            },
            true,
        )
        .await
    }
}

#[cfg(all(test, feature = "crypto_loan"))]
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 MockStableRateApiClient {
        force_error: bool,
    }

    #[async_trait]
    impl StableRateApi for MockStableRateApiClient {
        async fn check_collateral_repay_rate_stable_rate(
            &self,
            _params: CheckCollateralRepayRateStableRateParams,
        ) -> anyhow::Result<RestApiResponse<models::CheckCollateralRepayRateStableRateResponse>>
        {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"loanlCoin":"BUSD","collateralCoin":"BNB","repayAmount":"1000","rate":"300.36781234"}"#).unwrap();
            let dummy_response: models::CheckCollateralRepayRateStableRateResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::CheckCollateralRepayRateStableRateResponse");

            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())
        }

        async fn get_crypto_loans_income_history(
            &self,
            _params: GetCryptoLoansIncomeHistoryParams,
        ) -> anyhow::Result<RestApiResponse<Vec<models::GetCryptoLoansIncomeHistoryResponseInner>>>
        {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"[{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1633771139847,"tranId":"80423589583"},{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1634638371496,"tranId":"81685123491"}]"#).unwrap();
            let dummy_response: Vec<models::GetCryptoLoansIncomeHistoryResponseInner> =
                serde_json::from_value(resp_json.clone()).expect(
                    "should parse into Vec<models::GetCryptoLoansIncomeHistoryResponseInner>",
                );

            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())
        }

        async fn get_loan_borrow_history(
            &self,
            _params: GetLoanBorrowHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetLoanBorrowHistoryResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"orderId":100000001,"loanCoin":"BUSD","initialLoanAmount":"10000","hourlyInterestRate":"0.000057","loanTerm":"7","collateralCoin":"BNB","initialCollateralAmount":"49.27565492","borrowTime":1575018510000,"status":"Repaid"}],"total":1}"#).unwrap();
            let dummy_response: models::GetLoanBorrowHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetLoanBorrowHistoryResponse");

            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())
        }

        async fn get_loan_ltv_adjustment_history(
            &self,
            _params: GetLoanLtvAdjustmentHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetLoanLtvAdjustmentHistoryResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","collateralCoin":"BNB","direction":"ADDITIONAL","amount":"5.235","preLTV":"0.78","afterLTV":"0.56","adjustTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let dummy_response: models::GetLoanLtvAdjustmentHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetLoanLtvAdjustmentHistoryResponse");

            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())
        }

        async fn get_loan_repayment_history(
            &self,
            _params: GetLoanRepaymentHistoryParams,
        ) -> anyhow::Result<RestApiResponse<models::GetLoanRepaymentHistoryResponse>> {
            if self.force_error {
                return Err(ConnectorError::ConnectorClientError {
                    msg: "ResponseError".to_string(),
                    code: None,
                }
                .into());
            }

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","repayAmount":"10000","collateralCoin":"BNB","collateralUsed":"0","collateralReturn":"49.27565492","repayType":"1","repayStatus":"Repaid","repayTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let dummy_response: models::GetLoanRepaymentHistoryResponse =
                serde_json::from_value(resp_json.clone())
                    .expect("should parse into models::GetLoanRepaymentHistoryResponse");

            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 check_collateral_repay_rate_stable_rate_required_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = CheckCollateralRepayRateStableRateParams::builder("loan_coin_example".to_string(),"collateral_coin_example".to_string(),dec!(1.0),).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"loanlCoin":"BUSD","collateralCoin":"BNB","repayAmount":"1000","rate":"300.36781234"}"#).unwrap();
            let expected_response : models::CheckCollateralRepayRateStableRateResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::CheckCollateralRepayRateStableRateResponse");

            let resp = client.check_collateral_repay_rate_stable_rate(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 check_collateral_repay_rate_stable_rate_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = CheckCollateralRepayRateStableRateParams::builder("loan_coin_example".to_string(),"collateral_coin_example".to_string(),dec!(1.0),).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"loanlCoin":"BUSD","collateralCoin":"BNB","repayAmount":"1000","rate":"300.36781234"}"#).unwrap();
            let expected_response : models::CheckCollateralRepayRateStableRateResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::CheckCollateralRepayRateStableRateResponse");

            let resp = client.check_collateral_repay_rate_stable_rate(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 check_collateral_repay_rate_stable_rate_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: true };

            let params = CheckCollateralRepayRateStableRateParams::builder(
                "loan_coin_example".to_string(),
                "collateral_coin_example".to_string(),
                dec!(1.0),
            )
            .build()
            .unwrap();

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

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

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

            let resp_json: Value = serde_json::from_str(r#"[{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1633771139847,"tranId":"80423589583"},{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1634638371496,"tranId":"81685123491"}]"#).unwrap();
            let expected_response : Vec<models::GetCryptoLoansIncomeHistoryResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::GetCryptoLoansIncomeHistoryResponseInner>");

            let resp = client.get_crypto_loans_income_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_crypto_loans_income_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = GetCryptoLoansIncomeHistoryParams::builder().asset("asset_example".to_string()).r#type("0".to_string()).start_time(1623319461670).end_time(1641782889000).limit(10).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"[{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1633771139847,"tranId":"80423589583"},{"asset":"BUSD","type":"borrowIn","amount":"100","timestamp":1634638371496,"tranId":"81685123491"}]"#).unwrap();
            let expected_response : Vec<models::GetCryptoLoansIncomeHistoryResponseInner> = serde_json::from_value(resp_json.clone()).expect("should parse into Vec<models::GetCryptoLoansIncomeHistoryResponseInner>");

            let resp = client.get_crypto_loans_income_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_crypto_loans_income_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: true };

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

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

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

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

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"orderId":100000001,"loanCoin":"BUSD","initialLoanAmount":"10000","hourlyInterestRate":"0.000057","loanTerm":"7","collateralCoin":"BNB","initialCollateralAmount":"49.27565492","borrowTime":1575018510000,"status":"Repaid"}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanBorrowHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanBorrowHistoryResponse");

            let resp = client.get_loan_borrow_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_loan_borrow_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = GetLoanBorrowHistoryParams::builder().order_id(1).loan_coin("loan_coin_example".to_string()).collateral_coin("collateral_coin_example".to_string()).start_time(1623319461670).end_time(1641782889000).current(1).limit(10).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"orderId":100000001,"loanCoin":"BUSD","initialLoanAmount":"10000","hourlyInterestRate":"0.000057","loanTerm":"7","collateralCoin":"BNB","initialCollateralAmount":"49.27565492","borrowTime":1575018510000,"status":"Repaid"}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanBorrowHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanBorrowHistoryResponse");

            let resp = client.get_loan_borrow_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_loan_borrow_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: true };

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

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

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

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

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","collateralCoin":"BNB","direction":"ADDITIONAL","amount":"5.235","preLTV":"0.78","afterLTV":"0.56","adjustTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanLtvAdjustmentHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanLtvAdjustmentHistoryResponse");

            let resp = client.get_loan_ltv_adjustment_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_loan_ltv_adjustment_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = GetLoanLtvAdjustmentHistoryParams::builder().order_id(1).loan_coin("loan_coin_example".to_string()).collateral_coin("collateral_coin_example".to_string()).start_time(1623319461670).end_time(1641782889000).current(1).limit(10).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","collateralCoin":"BNB","direction":"ADDITIONAL","amount":"5.235","preLTV":"0.78","afterLTV":"0.56","adjustTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanLtvAdjustmentHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanLtvAdjustmentHistoryResponse");

            let resp = client.get_loan_ltv_adjustment_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_loan_ltv_adjustment_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: true };

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

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

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

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

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","repayAmount":"10000","collateralCoin":"BNB","collateralUsed":"0","collateralReturn":"49.27565492","repayType":"1","repayStatus":"Repaid","repayTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanRepaymentHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanRepaymentHistoryResponse");

            let resp = client.get_loan_repayment_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_loan_repayment_history_optional_params_success() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: false };

            let params = GetLoanRepaymentHistoryParams::builder().order_id(1).loan_coin("loan_coin_example".to_string()).collateral_coin("collateral_coin_example".to_string()).start_time(1623319461670).end_time(1641782889000).current(1).limit(10).recv_window(5000).build().unwrap();

            let resp_json: Value = serde_json::from_str(r#"{"rows":[{"loanCoin":"BUSD","repayAmount":"10000","collateralCoin":"BNB","collateralUsed":"0","collateralReturn":"49.27565492","repayType":"1","repayStatus":"Repaid","repayTime":1575018510000,"orderId":756783308056935400}],"total":1}"#).unwrap();
            let expected_response : models::GetLoanRepaymentHistoryResponse = serde_json::from_value(resp_json.clone()).expect("should parse into models::GetLoanRepaymentHistoryResponse");

            let resp = client.get_loan_repayment_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_loan_repayment_history_response_error() {
        TOKIO_SHARED_RT.block_on(async {
            let client = MockStableRateApiClient { force_error: true };

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

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