ironflow_engine/config/
http.rs1use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct HttpConfig {
17 pub method: String,
19 pub url: String,
21 pub headers: Vec<(String, String)>,
23 pub body: Option<Value>,
25 pub timeout_secs: Option<u64>,
27 #[serde(default)]
29 pub allow_failure: bool,
30}
31
32impl HttpConfig {
33 pub fn get(url: &str) -> Self {
44 Self::new("GET", url)
45 }
46
47 pub fn post(url: &str) -> Self {
49 Self::new("POST", url)
50 }
51
52 pub fn put(url: &str) -> Self {
54 Self::new("PUT", url)
55 }
56
57 pub fn patch(url: &str) -> Self {
59 Self::new("PATCH", url)
60 }
61
62 pub fn delete(url: &str) -> Self {
64 Self::new("DELETE", url)
65 }
66
67 fn new(method: &str, url: &str) -> Self {
68 Self {
69 method: method.to_string(),
70 url: url.to_string(),
71 headers: Vec::new(),
72 body: None,
73 timeout_secs: None,
74 allow_failure: false,
75 }
76 }
77
78 pub fn header(mut self, name: &str, value: &str) -> Self {
80 self.headers.push((name.to_string(), value.to_string()));
81 self
82 }
83
84 pub fn json(mut self, body: Value) -> Self {
86 self.body = Some(body);
87 self
88 }
89
90 pub fn timeout_secs(mut self, secs: u64) -> Self {
92 self.timeout_secs = Some(secs);
93 self
94 }
95
96 pub fn allow_failure(mut self) -> Self {
107 self.allow_failure = true;
108 self
109 }
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115 use serde_json::json;
116
117 #[test]
118 fn methods() {
119 assert_eq!(HttpConfig::get("http://x").method, "GET");
120 assert_eq!(HttpConfig::post("http://x").method, "POST");
121 assert_eq!(HttpConfig::put("http://x").method, "PUT");
122 assert_eq!(HttpConfig::patch("http://x").method, "PATCH");
123 assert_eq!(HttpConfig::delete("http://x").method, "DELETE");
124 }
125
126 #[test]
127 fn builder() {
128 let config = HttpConfig::post("http://api.example.com")
129 .header("Authorization", "Bearer token")
130 .json(json!({"key": "value"}))
131 .timeout_secs(10);
132
133 assert_eq!(config.headers.len(), 1);
134 assert!(config.body.is_some());
135 assert_eq!(config.timeout_secs, Some(10));
136 }
137}