use std::collections::HashMap;
use crate::error::Result;
use crate::http::{BaseClient, RequestOptions};
use crate::models::{
ArbitrageContract, ArbitrageContractRequest, ContractInfo, ContractInfoRequest,
DayTradeParamRequest, MainSeriesInfo, MainSeriesInfoRequest, MarginArbiPerfPara,
MarginArbiPerfParaRequest, NewContractInfo, NewContractInfoRequest, TradeParam, TradingParam,
TradingParamRequest,
};
const PATH_GET_DAY_TRADE_PARAM: &str = "/dceapi/forward/publicweb/tradepara/dayTradPara";
const PATH_GET_MONTH_TRADE_PARAM: &str = "/dceapi/forward/publicweb/tradepara/monthTradPara";
const PATH_GET_CONTRACT_INFO: &str = "/dceapi/forward/publicweb/tradepara/contractInfo";
const PATH_GET_ARBITRAGE_CONTRACT: &str = "/dceapi/forward/publicweb/tradepara/arbitrageContract";
const PATH_GET_TRADING_PARAM: &str = "/dceapi/forward/publicweb/tradepara/tradingParam";
const PATH_GET_MARGIN_ARBI_PERF_PARA: &str =
"/dceapi/forward/publicweb/tradepara/marginArbiPerfPara";
const PATH_GET_NEW_CONTRACT_INFO: &str = "/dceapi/forward/publicweb/tradepara/newContractInfo";
const PATH_GET_MAIN_SERIES_INFO: &str = "/dceapi/forward/publicweb/tradepara/mainSeriesInfo";
#[derive(Debug, Clone)]
pub struct TradeService {
client: BaseClient,
}
impl TradeService {
pub fn new(client: BaseClient) -> Self {
TradeService { client }
}
pub async fn get_day_trade_param(
&self,
req: &DayTradeParamRequest,
opts: Option<RequestOptions>,
) -> Result<Vec<TradeParam>> {
self.client
.do_post(PATH_GET_DAY_TRADE_PARAM, req, opts)
.await
}
pub async fn get_month_trade_param(
&self,
opts: Option<RequestOptions>,
) -> Result<HashMap<String, serde_json::Value>> {
#[derive(serde::Serialize)]
struct EmptyRequest {}
self.client
.do_post(PATH_GET_MONTH_TRADE_PARAM, &EmptyRequest {}, opts)
.await
}
pub async fn get_contract_info(
&self,
req: &ContractInfoRequest,
opts: Option<RequestOptions>,
) -> Result<Vec<ContractInfo>> {
self.client.do_post(PATH_GET_CONTRACT_INFO, req, opts).await
}
pub async fn get_arbitrage_contract(
&self,
lang: Option<&str>,
opts: Option<RequestOptions>,
) -> Result<Vec<ArbitrageContract>> {
let req = ArbitrageContractRequest {
lang: lang.unwrap_or("zh").to_string(),
};
self.client
.do_post(PATH_GET_ARBITRAGE_CONTRACT, &req, opts)
.await
}
pub async fn get_trading_param(
&self,
lang: Option<&str>,
opts: Option<RequestOptions>,
) -> Result<Vec<TradingParam>> {
let req = TradingParamRequest {
lang: lang.unwrap_or("zh").to_string(),
};
self.client
.do_post(PATH_GET_TRADING_PARAM, &req, opts)
.await
}
pub async fn get_margin_arbi_perf_para(
&self,
req: &MarginArbiPerfParaRequest,
opts: Option<RequestOptions>,
) -> Result<Vec<MarginArbiPerfPara>> {
self.client
.do_post(PATH_GET_MARGIN_ARBI_PERF_PARA, req, opts)
.await
}
pub async fn get_new_contract_info(
&self,
req: &NewContractInfoRequest,
opts: Option<RequestOptions>,
) -> Result<Vec<NewContractInfo>> {
self.client
.do_post(PATH_GET_NEW_CONTRACT_INFO, req, opts)
.await
}
pub async fn get_main_series_info(
&self,
req: &MainSeriesInfoRequest,
opts: Option<RequestOptions>,
) -> Result<Vec<MainSeriesInfo>> {
self.client
.do_post(PATH_GET_MAIN_SERIES_INFO, req, opts)
.await
}
}