#![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 C2CApi: Send + Sync {
async fn get_c2_c_trade_history(
&self,
params: GetC2CTradeHistoryParams,
) -> anyhow::Result<RestApiResponse<models::GetC2CTradeHistoryResponse>>;
}
#[derive(Debug, Clone)]
pub struct C2CApiClient {
configuration: ConfigurationRestApi,
}
impl C2CApiClient {
pub fn new(configuration: ConfigurationRestApi) -> Self {
Self { configuration }
}
}
#[derive(Clone, Debug, Builder, Default)]
#[builder(pattern = "owned", build_fn(error = "ParamBuildError"))]
pub struct GetC2CTradeHistoryParams {
#[builder(setter(into), default)]
pub trade_type: Option<String>,
#[builder(setter(into), default)]
pub start_timestamp: Option<i64>,
#[builder(setter(into), default)]
pub end_timestamp: Option<i64>,
#[builder(setter(into), default)]
pub page: Option<i64>,
#[builder(setter(into), default)]
pub rows: Option<i64>,
#[builder(setter(into), default)]
pub recv_window: Option<i64>,
}
impl GetC2CTradeHistoryParams {
#[must_use]
pub fn builder() -> GetC2CTradeHistoryParamsBuilder {
GetC2CTradeHistoryParamsBuilder::default()
}
}
#[async_trait]
impl C2CApi for C2CApiClient {
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,
recv_window,
} = 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));
}
if let Some(rw) = recv_window {
query_params.insert("recvWindow".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
},
true,
)
.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 MockC2CApiClient {
force_error: bool,
}
#[async_trait]
impl C2CApi for MockC2CApiClient {
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":"ab***","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap();
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 = MockC2CApiClient { 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":"ab***","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap();
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 = MockC2CApiClient { force_error: false };
let params = GetC2CTradeHistoryParams::builder().trade_type("trade_type_example".to_string()).start_timestamp(789).end_timestamp(789).page(1).rows(100).recv_window(5000).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":"ab***","advertisementRole":"TAKER"}],"total":1,"success":true}"#).unwrap();
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 = MockC2CApiClient { 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");
}
}
});
}
}