Skip to main content

wfe_core/models/
poll_config.rs

1use std::collections::HashMap;
2use std::time::Duration;
3
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
7/// Httpmethod.
8pub enum HttpMethod {
9    #[default]
10    /// Get.
11    Get,
12    /// Post.
13    Post,
14    /// Put.
15    Put,
16    /// Patch.
17    Patch,
18    /// Delete.
19    Delete,
20}
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23/// Pollcondition.
24pub enum PollCondition {
25    /// Check a JSON path equals a value: e.g. JsonPathEquals("$.status", "complete")
26    JsonPathEquals {
27        path: String,
28        value: serde_json::Value,
29    },
30    /// Check HTTP status code
31    StatusCode(u16),
32    /// Check response body contains string
33    BodyContains(String),
34}
35
36impl Default for PollCondition {
37    fn default() -> Self {
38        Self::StatusCode(200)
39    }
40}
41
42#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
43/// Pollendpointconfig.
44pub struct PollEndpointConfig {
45    /// URL template. Supports `{placeholder}` interpolation from workflow data.
46    pub url: String,
47    /// Method.
48    pub method: HttpMethod,
49    #[serde(default)]
50    /// Headers.
51    pub headers: HashMap<String, String>,
52    #[serde(default)]
53    /// Body.
54    pub body: Option<serde_json::Value>,
55    #[serde(with = "super::duration_millis")]
56    /// Interval.
57    pub interval: Duration,
58    #[serde(with = "super::duration_millis")]
59    /// Timeout.
60    pub timeout: Duration,
61    /// Condition.
62    pub condition: PollCondition,
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use pretty_assertions::assert_eq;
69
70    #[test]
71    fn poll_config_serde_round_trip() {
72        let config = PollEndpointConfig {
73            url: "https://api.example.com/status/{id}".into(),
74            method: HttpMethod::Get,
75            headers: HashMap::from([("Authorization".into(), "Bearer token123".into())]),
76            body: None,
77            interval: Duration::from_secs(30),
78            timeout: Duration::from_secs(3600),
79            condition: PollCondition::JsonPathEquals {
80                path: "$.status".into(),
81                value: serde_json::Value::String("complete".into()),
82            },
83        };
84        let json = serde_json::to_string(&config).unwrap();
85        let deserialized: PollEndpointConfig = serde_json::from_str(&json).unwrap();
86        assert_eq!(config, deserialized);
87    }
88
89    #[test]
90    fn poll_condition_status_code() {
91        let cond = PollCondition::StatusCode(200);
92        let json = serde_json::to_string(&cond).unwrap();
93        let deserialized: PollCondition = serde_json::from_str(&json).unwrap();
94        assert_eq!(cond, deserialized);
95    }
96
97    #[test]
98    fn poll_condition_body_contains() {
99        let cond = PollCondition::BodyContains("success".into());
100        let json = serde_json::to_string(&cond).unwrap();
101        let deserialized: PollCondition = serde_json::from_str(&json).unwrap();
102        assert_eq!(cond, deserialized);
103    }
104}