ic_agent/agent/
builder.rs1use crate::{
2 agent::{agent_config::AgentConfig, Agent},
3 AgentError, Identity, NonceFactory, NonceGenerator,
4};
5use std::sync::Arc;
6
7use super::{route_provider::RouteProvider, HttpService};
8
9#[derive(Default)]
11pub struct AgentBuilder {
12 config: AgentConfig,
13}
14
15impl AgentBuilder {
16 pub fn build(self) -> Result<Agent, AgentError> {
18 Agent::new(self.config)
19 }
20
21 pub fn with_background_dynamic_routing(mut self) -> Self {
26 assert!(
27 self.config.route_provider.is_none(),
28 "with_background_dynamic_routing cannot be called with with_route_provider"
29 );
30 self.config.background_dynamic_routing = true;
31 self
32 }
33
34 pub fn with_url<S: Into<String>>(mut self, url: S) -> Self {
36 assert!(
37 self.config.route_provider.is_none(),
38 "with_url cannot be called with with_route_provider"
39 );
40 self.config.url = Some(url.into().parse().unwrap());
41 self
42 }
43
44 pub fn with_nonce_factory(self, nonce_factory: NonceFactory) -> AgentBuilder {
46 self.with_nonce_generator(nonce_factory)
47 }
48
49 pub fn with_nonce_generator<N: 'static + NonceGenerator>(
51 self,
52 nonce_factory: N,
53 ) -> AgentBuilder {
54 self.with_arc_nonce_generator(Arc::new(nonce_factory))
55 }
56
57 pub fn with_arc_nonce_generator(
59 mut self,
60 nonce_factory: Arc<dyn NonceGenerator>,
61 ) -> AgentBuilder {
62 self.config.nonce_factory = Arc::new(nonce_factory);
63 self
64 }
65
66 pub fn with_identity<I>(self, identity: I) -> Self
68 where
69 I: 'static + Identity,
70 {
71 self.with_arc_identity(Arc::new(identity))
72 }
73
74 pub fn with_boxed_identity(self, identity: Box<dyn Identity>) -> Self {
76 self.with_arc_identity(Arc::from(identity))
77 }
78
79 pub fn with_arc_identity(mut self, identity: Arc<dyn Identity>) -> Self {
81 self.config.identity = identity;
82 self
83 }
84
85 pub fn with_ingress_expiry(mut self, ingress_expiry: std::time::Duration) -> Self {
93 self.config.ingress_expiry = ingress_expiry;
94 self
95 }
96
97 pub fn with_verify_query_signatures(mut self, verify_query_signatures: bool) -> Self {
100 self.config.verify_query_signatures = verify_query_signatures;
101 self
102 }
103
104 pub fn with_max_concurrent_requests(mut self, max_concurrent_requests: usize) -> Self {
108 self.config.max_concurrent_requests = max_concurrent_requests;
109 self
110 }
111
112 pub fn with_route_provider(self, provider: impl RouteProvider + 'static) -> Self {
114 self.with_arc_route_provider(Arc::new(provider))
115 }
116
117 pub fn with_arc_route_provider(mut self, provider: Arc<dyn RouteProvider>) -> Self {
119 assert!(
120 !self.config.background_dynamic_routing,
121 "with_background_dynamic_routing cannot be called with with_route_provider"
122 );
123 assert!(
124 self.config.url.is_none(),
125 "with_url cannot be called with with_route_provider"
126 );
127 self.config.route_provider = Some(provider);
128 self
129 }
130
131 pub fn with_http_client(mut self, client: reqwest::Client) -> Self {
133 assert!(
134 self.config.http_service.is_none(),
135 "with_arc_http_middleware cannot be called with with_http_client"
136 );
137 self.config.client = Some(client);
138 self
139 }
140
141 pub fn with_arc_http_middleware(mut self, service: Arc<dyn HttpService>) -> Self {
147 assert!(
148 self.config.client.is_none(),
149 "with_arc_http_middleware cannot be called with with_http_client"
150 );
151 self.config.http_service = Some(service);
152 self
153 }
154
155 pub fn with_max_tcp_error_retries(mut self, retries: usize) -> Self {
157 self.config.max_tcp_error_retries = retries;
158 self
159 }
160
161 pub fn with_max_response_body_size(mut self, max_size: usize) -> Self {
163 self.config.max_response_body_size = Some(max_size);
164 self
165 }
166 pub fn with_max_polling_time(mut self, max_polling_time: std::time::Duration) -> Self {
168 self.config.max_polling_time = max_polling_time;
169 self
170 }
171}