hatchet_rest/
rate_limits.rs1use std::time::SystemTime;
6
7use serde::Deserialize;
8
9use crate::transport::Query;
10use crate::types::string_enum;
11use crate::{Operation, Page, RestClient, Result};
12
13string_enum! {
14 RateLimitOrderBy {
16 Key => "key",
18 Value => "value",
20 LimitValue => "limitValue",
22 }
23}
24
25string_enum! {
26 RateLimitOrderDirection {
29 Asc => "asc",
31 Desc => "desc",
33 }
34}
35
36#[derive(Clone, Debug, Deserialize)]
38#[serde(rename_all = "camelCase")]
39#[non_exhaustive]
40pub struct RateLimit {
41 pub key: String,
43 pub tenant_id: String,
45 pub limit_value: i64,
47 pub value: i64,
49 pub window: String,
51 #[serde(deserialize_with = "crate::time::deserialize")]
53 pub last_refill: SystemTime,
54 #[serde(flatten)]
56 pub other: crate::JsonObject,
57}
58
59#[derive(Clone, Debug, Default)]
62#[must_use]
63pub struct RateLimitListQuery {
64 offset: Option<i64>,
65 limit: Option<i64>,
66 search: Option<String>,
67 order_by: Option<(RateLimitOrderBy, RateLimitOrderDirection)>,
68}
69
70impl RateLimitListQuery {
71 pub fn new() -> Self {
73 Self::default()
74 }
75
76 pub fn offset(mut self, offset: i64) -> Self {
78 self.offset = Some(offset);
79 self
80 }
81
82 pub fn limit(mut self, limit: i64) -> Self {
84 self.limit = Some(limit);
85 self
86 }
87
88 pub fn search(mut self, text: impl Into<String>) -> Self {
90 self.search = Some(text.into());
91 self
92 }
93
94 pub fn order_by(mut self, field: RateLimitOrderBy, direction: RateLimitOrderDirection) -> Self {
96 self.order_by = Some((field, direction));
97 self
98 }
99
100 fn query(&self) -> Query {
101 let mut query = Query::new();
102 query.push_opt("offset", self.offset);
103 query.push_opt("limit", self.limit);
104 query.push_opt("search", self.search.as_deref());
105 if let Some((field, direction)) = &self.order_by {
106 query.push("orderByField", field);
107 query.push("orderByDirection", direction);
108 }
109 query
110 }
111}
112
113#[derive(Clone, Copy, Debug)]
115pub struct RateLimitsApi<'a> {
116 pub(crate) client: &'a RestClient,
117}
118
119impl RateLimitsApi<'_> {
120 pub async fn list(&self, query: RateLimitListQuery) -> Result<Page<RateLimit>> {
138 self.client
139 .inner
140 .call(Operation::RateLimitList, &[], query.query())
141 .await
142 }
143}