Skip to main content

rust_okx/api/finance/requests/
common.rs

1use serde::Serialize;
2
3use crate::model::{
4    RequestValidationError, ValidateRequest, optional_non_empty, optional_unsigned_integer_string,
5    range_u64,
6};
7
8/// Common currency and cursor pagination query used by finance history endpoints.
9#[derive(Debug, Clone, Default, Serialize)]
10pub struct FinanceHistoryRequest {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    ccy: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    after: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    before: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    limit: Option<u32>,
19}
20
21impl FinanceHistoryRequest {
22    /// Create an unfiltered history query.
23    pub fn new() -> Self {
24        Self::default()
25    }
26
27    /// Restrict the result to one currency.
28    pub fn currency(mut self, ccy: impl Into<String>) -> Self {
29        self.ccy = Some(ccy.into());
30        self
31    }
32
33    /// Set the endpoint-specific `after` cursor.
34    pub fn after(mut self, after: impl Into<String>) -> Self {
35        self.after = Some(after.into());
36        self
37    }
38
39    /// Set the endpoint-specific `before` cursor.
40    pub fn before(mut self, before: impl Into<String>) -> Self {
41        self.before = Some(before.into());
42        self
43    }
44
45    /// Set the number of records to return, from 1 through 100.
46    pub fn limit(mut self, limit: u32) -> Self {
47        self.limit = Some(limit);
48        self
49    }
50}
51
52impl ValidateRequest for FinanceHistoryRequest {
53    fn validate(&self) -> Result<(), RequestValidationError> {
54        optional_non_empty("ccy", self.ccy.as_deref())?;
55        optional_unsigned_integer_string("after", self.after.as_deref())?;
56        optional_unsigned_integer_string("before", self.before.as_deref())?;
57        if let Some(limit) = self.limit {
58            range_u64("limit", u64::from(limit), 1, 100)?;
59        }
60        Ok(())
61    }
62}