1use serde::{Deserialize, Serialize};
5
6use crate::transport::Query;
7use crate::types::to_object;
8use crate::{Error, JsonObject, Operation, Page, ResourceMeta, RestClient, Result};
9
10#[derive(Clone, Debug, Deserialize)]
12#[serde(rename_all = "camelCase")]
13#[non_exhaustive]
14pub struct Filter {
15 pub metadata: ResourceMeta,
17 pub tenant_id: String,
19 pub workflow_id: String,
21 pub scope: String,
23 pub expression: String,
25 #[serde(default)]
27 pub payload: Option<JsonObject>,
28 #[serde(default)]
31 pub is_declarative: Option<bool>,
32 #[serde(flatten)]
34 pub other: JsonObject,
35}
36
37#[derive(Clone, Debug)]
39#[must_use]
40pub struct FilterSpec {
41 workflow_id: String,
42 scope: String,
43 expression: String,
44 payload: Option<std::result::Result<JsonObject, String>>,
45}
46
47impl FilterSpec {
48 pub fn new(
52 workflow_id: impl Into<String>,
53 scope: impl Into<String>,
54 expression: impl Into<String>,
55 ) -> Self {
56 Self {
57 workflow_id: workflow_id.into(),
58 scope: scope.into(),
59 expression: expression.into(),
60 payload: None,
61 }
62 }
63
64 pub fn payload(mut self, payload: &impl Serialize) -> Self {
67 self.payload = Some(to_object(payload, "the payload"));
68 self
69 }
70}
71
72#[derive(Serialize)]
73#[serde(rename_all = "camelCase")]
74struct CreateBody<'a> {
75 workflow_id: &'a str,
76 scope: &'a str,
77 expression: &'a str,
78 #[serde(skip_serializing_if = "Option::is_none")]
79 payload: Option<JsonObject>,
80}
81
82#[derive(Clone, Debug, Default, Serialize)]
85#[serde(rename_all = "camelCase")]
86#[must_use]
87pub struct FilterUpdate {
88 #[serde(skip_serializing_if = "Option::is_none")]
89 expression: Option<String>,
90 #[serde(skip_serializing_if = "Option::is_none")]
91 scope: Option<String>,
92 #[serde(skip_serializing_if = "Option::is_none")]
93 payload: Option<JsonObject>,
94 #[serde(skip)]
95 error: Option<String>,
96}
97
98impl FilterUpdate {
99 pub fn new() -> Self {
101 Self::default()
102 }
103
104 pub fn expression(mut self, expression: impl Into<String>) -> Self {
106 self.expression = Some(expression.into());
107 self
108 }
109
110 pub fn scope(mut self, scope: impl Into<String>) -> Self {
112 self.scope = Some(scope.into());
113 self
114 }
115
116 pub fn payload(mut self, payload: &impl Serialize) -> Self {
118 match to_object(payload, "the payload") {
119 Ok(object) => self.payload = Some(object),
120 Err(message) => self.error = Some(message),
121 }
122 self
123 }
124}
125
126#[derive(Clone, Debug, Default)]
129#[must_use]
130pub struct FilterListQuery {
131 offset: Option<i64>,
132 limit: Option<i64>,
133 workflow_ids: Vec<String>,
134 scopes: Vec<String>,
135}
136
137impl FilterListQuery {
138 pub fn new() -> Self {
140 Self::default()
141 }
142
143 pub fn offset(mut self, offset: i64) -> Self {
145 self.offset = Some(offset);
146 self
147 }
148
149 pub fn limit(mut self, limit: i64) -> Self {
151 self.limit = Some(limit);
152 self
153 }
154
155 pub fn workflow_ids(mut self, ids: impl IntoIterator<Item = impl Into<String>>) -> Self {
157 self.workflow_ids.extend(ids.into_iter().map(Into::into));
158 self
159 }
160
161 pub fn scopes(mut self, scopes: impl IntoIterator<Item = impl Into<String>>) -> Self {
163 self.scopes.extend(scopes.into_iter().map(Into::into));
164 self
165 }
166
167 fn query(&self) -> Query {
168 let mut query = Query::new();
169 query.push_opt("offset", self.offset);
170 query.push_opt("limit", self.limit);
171 query.push_all("workflowIds", &self.workflow_ids);
172 query.push_all("scopes", &self.scopes);
173 query
174 }
175}
176
177#[derive(Clone, Copy, Debug)]
179pub struct FiltersApi<'a> {
180 pub(crate) client: &'a RestClient,
181}
182
183impl RestClient {
184 pub fn filters(&self) -> FiltersApi<'_> {
187 FiltersApi { client: self }
188 }
189}
190
191impl FiltersApi<'_> {
192 pub async fn list(&self, query: FilterListQuery) -> Result<Page<Filter>> {
198 self.client
199 .inner
200 .call(Operation::V1FilterList, &[], query.query())
201 .await
202 }
203
204 pub async fn get(&self, filter_id: &str) -> Result<Filter> {
213 self.client
214 .inner
215 .call(Operation::V1FilterGet, &[filter_id], Query::new())
216 .await
217 }
218
219 pub async fn create(&self, spec: FilterSpec) -> Result<Filter> {
243 let payload = spec
244 .payload
245 .clone()
246 .transpose()
247 .map_err(Error::InvalidArgument)?;
248 let body = CreateBody {
249 workflow_id: &spec.workflow_id,
250 scope: &spec.scope,
251 expression: &spec.expression,
252 payload,
253 };
254 self.client
255 .inner
256 .call_json(Operation::V1FilterCreate, &[], &body)
257 .await
258 }
259
260 pub async fn update(&self, filter_id: &str, update: FilterUpdate) -> Result<Filter> {
268 if let Some(message) = &update.error {
269 return Err(Error::InvalidArgument(message.clone()));
270 }
271 if update.expression.is_none() && update.scope.is_none() && update.payload.is_none() {
272 return Err(Error::InvalidArgument(
273 "a filter update must change something".into(),
274 ));
275 }
276 self.client
277 .inner
278 .call_json(Operation::V1FilterUpdate, &[filter_id], &update)
279 .await
280 }
281
282 pub async fn delete(&self, filter_id: &str) -> Result<Filter> {
290 self.client
291 .inner
292 .call(Operation::V1FilterDelete, &[filter_id], Query::new())
293 .await
294 }
295}