dceapi_rs/services/
market.rs

1//! Market service for quote and market data APIs.
2
3use crate::error::{Error, Result};
4use crate::http::{BaseClient, RequestOptions};
5use crate::models::{
6    ContractStat, ContractStatRequest, MonthQuotesRequest, Quote, QuotesRequest, WeekQuotesRequest,
7};
8
9/// API endpoint for night quotes.
10const PATH_GET_NIGHT_QUOTES: &str = "/dceapi/forward/publicweb/dailystat/tiNightQuotes";
11
12/// API endpoint for day quotes.
13const PATH_GET_DAY_QUOTES: &str = "/dceapi/forward/publicweb/dailystat/dayQuotes";
14
15/// API endpoint for week quotes.
16const PATH_GET_WEEK_QUOTES: &str = "/dceapi/forward/publicweb/dailystat/weekQuotes";
17
18/// API endpoint for month quotes.
19const PATH_GET_MONTH_QUOTES: &str = "/dceapi/forward/publicweb/dailystat/monthQuotes";
20
21/// API endpoint for contract statistics.
22const PATH_GET_CONTRACT_STAT: &str = "/dceapi/forward/publicweb/dailystat/contractStat";
23
24/// Market service for accessing quote and market data.
25#[derive(Debug, Clone)]
26pub struct MarketService {
27    client: BaseClient,
28}
29
30impl MarketService {
31    /// Create a new market service.
32    pub fn new(client: BaseClient) -> Self {
33        MarketService { client }
34    }
35
36    /// Get night session quotes.
37    ///
38    /// # Arguments
39    /// * `req` - Request with variety and trade date
40    /// * `opts` - Optional request options
41    pub async fn get_night_quotes(
42        &self,
43        req: &QuotesRequest,
44        opts: Option<RequestOptions>,
45    ) -> Result<Vec<Quote>> {
46        self.client.do_post(PATH_GET_NIGHT_QUOTES, req, opts).await
47    }
48
49    /// Get day session quotes.
50    ///
51    /// # Arguments
52    /// * `req` - Request with variety and trade date
53    /// * `opts` - Optional request options
54    pub async fn get_day_quotes(
55        &self,
56        req: &QuotesRequest,
57        opts: Option<RequestOptions>,
58    ) -> Result<Vec<Quote>> {
59        self.client.do_post(PATH_GET_DAY_QUOTES, req, opts).await
60    }
61
62    /// Get weekly quotes.
63    ///
64    /// # Arguments
65    /// * `req` - Request with variety, year, and week number
66    /// * `opts` - Optional request options
67    pub async fn get_week_quotes(
68        &self,
69        req: &WeekQuotesRequest,
70        opts: Option<RequestOptions>,
71    ) -> Result<Vec<Quote>> {
72        self.client.do_post(PATH_GET_WEEK_QUOTES, req, opts).await
73    }
74
75    /// Get monthly quotes.
76    ///
77    /// # Arguments
78    /// * `req` - Request with variety, year, and month
79    /// * `opts` - Optional request options
80    pub async fn get_month_quotes(
81        &self,
82        req: &MonthQuotesRequest,
83        opts: Option<RequestOptions>,
84    ) -> Result<Vec<Quote>> {
85        self.client.do_post(PATH_GET_MONTH_QUOTES, req, opts).await
86    }
87
88    /// Get contract statistics for a date range.
89    ///
90    /// # Arguments
91    /// * `req` - Request with contract code and date range
92    /// * `opts` - Optional request options
93    pub async fn get_contract_stat(
94        &self,
95        req: &ContractStatRequest,
96        opts: Option<RequestOptions>,
97    ) -> Result<ContractStat> {
98        if req.contract_code.is_empty() {
99            return Err(Error::validation("contract_code", "contract_code is required"));
100        }
101        self.client.do_post(PATH_GET_CONTRACT_STAT, req, opts).await
102    }
103}