dceapi_rs/services/
common.rs

1//! Common service for general API endpoints.
2
3use crate::error::Result;
4use crate::http::{BaseClient, RequestOptions};
5use crate::models::{TradeDate, Variety, VarietyMonthYearStat, VarietyMonthYearStatRequest};
6
7/// API endpoint for current trade date.
8const PATH_GET_CURR_TRADE_DATE: &str = "/dceapi/forward/publicweb/maxTradeDate";
9
10/// API endpoint for variety list.
11const PATH_GET_VARIETY_LIST: &str = "/dceapi/forward/publicweb/variety";
12
13/// API endpoint for variety month/year statistics.
14const PATH_GET_VARIETY_MONTH_YEAR_STAT: &str =
15    "/dceapi/forward/publicweb/phasestat/varietyMonthYearStat";
16
17/// Common service for general operations.
18#[derive(Debug, Clone)]
19pub struct CommonService {
20    client: BaseClient,
21}
22
23impl CommonService {
24    /// Create a new common service.
25    pub fn new(client: BaseClient) -> Self {
26        CommonService { client }
27    }
28
29    /// Get the current (latest) trade date.
30    ///
31    /// # Arguments
32    /// * `opts` - Optional request options
33    pub async fn get_curr_trade_date(&self, opts: Option<RequestOptions>) -> Result<TradeDate> {
34        self.client.do_get(PATH_GET_CURR_TRADE_DATE, opts).await
35    }
36
37    /// Get the list of available varieties (commodities).
38    ///
39    /// # Arguments
40    /// * `opts` - Optional request options (use trade_type to filter futures/options)
41    pub async fn get_variety_list(&self, opts: Option<RequestOptions>) -> Result<Vec<Variety>> {
42        self.client.do_get(PATH_GET_VARIETY_LIST, opts).await
43    }
44
45    /// Get variety monthly/yearly statistics.
46    ///
47    /// # Arguments
48    /// * `req` - Request with trade month, trade type, and language
49    /// * `opts` - Optional request options
50    pub async fn get_variety_month_year_stat(
51        &self,
52        req: &VarietyMonthYearStatRequest,
53        opts: Option<RequestOptions>,
54    ) -> Result<Vec<VarietyMonthYearStat>> {
55        self.client
56            .do_post(PATH_GET_VARIETY_MONTH_YEAR_STAT, req, opts)
57            .await
58    }
59}