Skip to main content

braid_http/types/
request.rs

1//! Braid-specific request parameters.
2
3use crate::types::{Patch, Version};
4
5/// Braid-specific request parameters.
6#[derive(Clone, Debug, Default)]
7pub struct BraidRequest {
8    pub version: Option<Vec<Version>>,
9    pub parents: Option<Vec<Version>>,
10    pub subscribe: bool,
11    pub patches: Option<Vec<Patch>>,
12    pub heartbeat_interval: Option<u64>,
13    pub peer: Option<String>,
14    pub ack: Option<Vec<Version>>,
15    pub enable_multiplex: bool,
16    pub merge_type: Option<String>,
17    pub content_type: Option<String>,
18    pub method: String,
19    pub body: bytes::Bytes,
20    pub extra_headers: std::collections::BTreeMap<String, String>,
21    pub retry: Option<crate::client::retry::RetryConfig>,
22}
23
24impl BraidRequest {
25    #[inline]
26    pub fn new() -> Self {
27        Self {
28            method: "GET".to_string(),
29            body: bytes::Bytes::new(),
30            extra_headers: std::collections::BTreeMap::new(),
31            ..Default::default()
32        }
33    }
34
35    pub fn subscribe(mut self) -> Self {
36        self.subscribe = true;
37        self
38    }
39
40    #[inline]
41    pub fn is_subscription(&self) -> bool {
42        self.subscribe
43    }
44
45    pub fn with_version(mut self, version: Version) -> Self {
46        self.version.get_or_insert_with(Vec::new).push(version);
47        self
48    }
49
50    pub fn with_versions(mut self, versions: Vec<Version>) -> Self {
51        self.version = Some(versions);
52        self
53    }
54
55    pub fn with_parent(mut self, version: Version) -> Self {
56        self.parents.get_or_insert_with(Vec::new).push(version);
57        self
58    }
59
60    pub fn with_peer(mut self, peer: impl Into<String>) -> Self {
61        self.peer = Some(peer.into());
62        self
63    }
64
65    pub fn with_ack(mut self, version: Version) -> Self {
66        self.ack.get_or_insert_with(Vec::new).push(version);
67        self
68    }
69
70    pub fn with_parents(mut self, parents: Vec<Version>) -> Self {
71        self.parents = Some(parents);
72        self
73    }
74
75    pub fn with_patches(mut self, patches: Vec<Patch>) -> Self {
76        self.patches = Some(patches);
77        self
78    }
79
80    #[inline]
81    pub fn has_patches(&self) -> bool {
82        self.patches.as_ref().is_some_and(|p| !p.is_empty())
83    }
84
85    pub fn with_heartbeat(mut self, seconds: u64) -> Self {
86        self.heartbeat_interval = Some(seconds);
87        self
88    }
89
90    pub fn with_multiplex(mut self, enable: bool) -> Self {
91        self.enable_multiplex = enable;
92        self
93    }
94
95    pub fn with_merge_type(mut self, merge_type: impl Into<String>) -> Self {
96        self.merge_type = Some(merge_type.into());
97        self
98    }
99
100    pub fn with_content_type(mut self, content_type: impl Into<String>) -> Self {
101        self.content_type = Some(content_type.into());
102        self
103    }
104
105    pub fn with_method(mut self, method: impl Into<String>) -> Self {
106        self.method = method.into();
107        self
108    }
109
110    pub fn with_body(mut self, body: impl Into<bytes::Bytes>) -> Self {
111        self.body = body.into();
112        self
113    }
114
115    pub fn with_header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
116        self.extra_headers.insert(key.into(), value.into());
117        self
118    }
119
120    pub fn with_retry(mut self, config: crate::client::retry::RetryConfig) -> Self {
121        self.retry = Some(config);
122        self
123    }
124
125    pub fn retry(self) -> Self {
126        self.with_retry(crate::client::retry::RetryConfig::default())
127    }
128}
129
130#[cfg(test)]
131mod tests {
132    use super::*;
133
134    #[test]
135    fn test_braid_request_builder() {
136        let req = BraidRequest::new()
137            .subscribe()
138            .with_version(Version::new("v1"))
139            .with_heartbeat(5);
140
141        assert!(req.subscribe);
142        assert_eq!(req.version.unwrap().len(), 1);
143        assert_eq!(req.heartbeat_interval, Some(5));
144    }
145}