use crate::{
agent::{agent_config::AgentConfig, Agent, Transport},
AgentError, Identity, NonceFactory, NonceGenerator,
};
use std::sync::Arc;
#[derive(Default)]
pub struct AgentBuilder {
config: AgentConfig,
}
impl AgentBuilder {
pub fn build(self) -> Result<Agent, AgentError> {
Agent::new(self.config)
}
#[cfg(feature = "reqwest")]
pub fn with_url<S: Into<String>>(self, url: S) -> Self {
use crate::agent::http_transport::ReqwestTransport;
self.with_transport(ReqwestTransport::create(url).unwrap())
}
pub fn with_transport<T: 'static + Transport>(self, transport: T) -> Self {
self.with_arc_transport(Arc::new(transport))
}
pub fn with_arc_transport(mut self, transport: Arc<dyn Transport>) -> Self {
self.config.transport = Some(transport);
self
}
pub fn with_nonce_factory(self, nonce_factory: NonceFactory) -> AgentBuilder {
self.with_nonce_generator(nonce_factory)
}
pub fn with_nonce_generator<N: 'static + NonceGenerator>(
self,
nonce_factory: N,
) -> AgentBuilder {
self.with_arc_nonce_generator(Arc::new(nonce_factory))
}
pub fn with_arc_nonce_generator(
mut self,
nonce_factory: Arc<dyn NonceGenerator>,
) -> AgentBuilder {
self.config.nonce_factory = Arc::new(nonce_factory);
self
}
pub fn with_identity<I>(self, identity: I) -> Self
where
I: 'static + Identity,
{
self.with_arc_identity(Arc::new(identity))
}
pub fn with_boxed_identity(self, identity: Box<dyn Identity>) -> Self {
self.with_arc_identity(Arc::from(identity))
}
pub fn with_arc_identity(mut self, identity: Arc<dyn Identity>) -> Self {
self.config.identity = identity;
self
}
pub fn with_ingress_expiry(mut self, ingress_expiry: Option<std::time::Duration>) -> Self {
self.config.ingress_expiry = ingress_expiry;
self
}
}