armature_graphql_client/
config.rs1use std::time::Duration;
4
5#[derive(Debug, Clone)]
7pub struct GraphQLClientConfig {
8 pub endpoint: String,
10 pub ws_endpoint: Option<String>,
12 pub timeout: Duration,
14 pub default_headers: Vec<(String, String)>,
16 pub batching: bool,
18 pub max_batch_size: usize,
20 pub batch_delay: Duration,
22 pub caching: bool,
24 pub cache_ttl: Duration,
26 pub user_agent: String,
28 pub retry_enabled: bool,
30 pub max_retries: u32,
32}
33
34impl Default for GraphQLClientConfig {
35 fn default() -> Self {
36 Self {
37 endpoint: "http://localhost:4000/graphql".to_string(),
38 ws_endpoint: None,
39 timeout: Duration::from_secs(30),
40 default_headers: Vec::new(),
41 batching: false,
42 max_batch_size: 10,
43 batch_delay: Duration::from_millis(10),
44 caching: false,
45 cache_ttl: Duration::from_secs(300),
46 user_agent: format!("armature-graphql-client/{}", env!("CARGO_PKG_VERSION")),
47 retry_enabled: true,
48 max_retries: 3,
49 }
50 }
51}
52
53impl GraphQLClientConfig {
54 pub fn builder() -> GraphQLClientConfigBuilder {
56 GraphQLClientConfigBuilder::default()
57 }
58
59 pub fn new(endpoint: impl Into<String>) -> Self {
61 Self {
62 endpoint: endpoint.into(),
63 ..Default::default()
64 }
65 }
66}
67
68#[derive(Debug, Default)]
70pub struct GraphQLClientConfigBuilder {
71 config: GraphQLClientConfig,
72}
73
74impl GraphQLClientConfigBuilder {
75 pub fn endpoint(mut self, endpoint: impl Into<String>) -> Self {
77 self.config.endpoint = endpoint.into();
78 self
79 }
80
81 pub fn ws_endpoint(mut self, endpoint: impl Into<String>) -> Self {
83 self.config.ws_endpoint = Some(endpoint.into());
84 self
85 }
86
87 pub fn timeout(mut self, timeout: Duration) -> Self {
89 self.config.timeout = timeout;
90 self
91 }
92
93 pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
95 self.config
96 .default_headers
97 .push((name.into(), value.into()));
98 self
99 }
100
101 pub fn bearer_auth(mut self, token: impl Into<String>) -> Self {
103 self.config.default_headers.push((
104 "Authorization".to_string(),
105 format!("Bearer {}", token.into()),
106 ));
107 self
108 }
109
110 pub fn batching(mut self, enabled: bool) -> Self {
112 self.config.batching = enabled;
113 self
114 }
115
116 pub fn max_batch_size(mut self, size: usize) -> Self {
118 self.config.max_batch_size = size;
119 self
120 }
121
122 pub fn batch_delay(mut self, delay: Duration) -> Self {
124 self.config.batch_delay = delay;
125 self
126 }
127
128 pub fn caching(mut self, enabled: bool) -> Self {
130 self.config.caching = enabled;
131 self
132 }
133
134 pub fn cache_ttl(mut self, ttl: Duration) -> Self {
136 self.config.cache_ttl = ttl;
137 self
138 }
139
140 pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
142 self.config.user_agent = user_agent.into();
143 self
144 }
145
146 pub fn retry(mut self, enabled: bool) -> Self {
148 self.config.retry_enabled = enabled;
149 self
150 }
151
152 pub fn max_retries(mut self, retries: u32) -> Self {
154 self.config.max_retries = retries;
155 self
156 }
157
158 pub fn build(self) -> GraphQLClientConfig {
160 self.config
161 }
162}