use serde_json::Value;
use crate::client::api_request::ApiRequest;
use crate::client::http_client::HttpClient;
use crate::error::TigerError;
pub struct TradeClient<'a> {
http_client: &'a HttpClient,
account: String,
}
impl<'a> TradeClient<'a> {
pub fn new(http_client: &'a HttpClient, account: impl Into<String>) -> Self {
Self {
http_client,
account: account.into(),
}
}
async fn execute(&self, method: &str, params: Value) -> Result<Option<Value>, TigerError> {
let biz_content = serde_json::to_string(¶ms)
.map_err(|e| TigerError::Config(format!("序列化业务参数失败: {}", e)))?;
let req = ApiRequest::new(method, biz_content);
let resp = self.http_client.execute_request(&req).await?;
Ok(resp.data)
}
pub async fn contract(&self, symbol: &str, sec_type: &str) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({
"account": self.account,
"symbol": symbol,
"secType": sec_type,
});
self.execute("contract", params).await
}
pub async fn contracts(&self, symbols: &[&str], sec_type: &str) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({
"account": self.account,
"symbols": symbols,
"secType": sec_type,
});
self.execute("contracts", params).await
}
pub async fn quote_contract(&self, symbol: &str, sec_type: &str) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({
"account": self.account,
"symbol": symbol,
"secType": sec_type,
});
self.execute("quote_contract", params).await
}
pub async fn place_order(&self, order: Value) -> Result<Option<Value>, TigerError> {
let mut params = order;
params["account"] = serde_json::json!(self.account);
self.execute("place_order", params).await
}
pub async fn preview_order(&self, order: Value) -> Result<Option<Value>, TigerError> {
let mut params = order;
params["account"] = serde_json::json!(self.account);
self.execute("preview_order", params).await
}
pub async fn modify_order(&self, id: i64, order: Value) -> Result<Option<Value>, TigerError> {
let mut params = order;
params["account"] = serde_json::json!(self.account);
params["id"] = serde_json::json!(id);
self.execute("modify_order", params).await
}
pub async fn cancel_order(&self, id: i64) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({
"account": self.account,
"id": id,
});
self.execute("cancel_order", params).await
}
pub async fn orders(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("orders", params).await
}
pub async fn active_orders(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("active_orders", params).await
}
pub async fn inactive_orders(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("inactive_orders", params).await
}
pub async fn filled_orders(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("filled_orders", params).await
}
pub async fn positions(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("positions", params).await
}
pub async fn assets(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("assets", params).await
}
pub async fn prime_assets(&self) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({"account": self.account});
self.execute("prime_assets", params).await
}
pub async fn order_transactions(&self, id: i64) -> Result<Option<Value>, TigerError> {
let params = serde_json::json!({
"account": self.account,
"id": id,
});
self.execute("order_transactions", params).await
}
}
#[cfg(test)]
mod tests;