use std::path::PathBuf;
use crate::client::{client::Client, config::*};
use crate::core::config::Config;
pub struct ClientBuilder {
config: ClientConfig,
}
impl Default for ClientBuilder {
fn default() -> Self {
ClientBuilder {
config: ClientConfig::default(),
}
}
}
impl ClientBuilder {
pub fn new() -> ClientBuilder {
ClientBuilder::default()
}
pub fn from_config<T>(path: T) -> Result<ClientBuilder, ()>
where
T: Into<PathBuf>,
{
Ok(ClientBuilder {
config: ClientConfig::load(&path.into())?,
})
}
pub fn client(self) -> Option<Client> {
if self.is_valid() {
Some(Client::new(self.config))
} else {
None
}
}
pub fn config(self) -> ClientConfig {
self.config
}
pub fn is_valid(&self) -> bool {
self.config.is_valid()
}
pub fn application_name<T>(mut self, application_name: T) -> Self
where
T: Into<String>,
{
self.config.application_name = application_name.into();
self
}
pub fn application_uri<T>(mut self, application_uri: T) -> Self
where
T: Into<String>,
{
self.config.application_uri = application_uri.into();
self
}
pub fn product_uri<T>(mut self, product_uri: T) -> Self
where
T: Into<String>,
{
self.config.product_uri = product_uri.into();
self
}
pub fn create_sample_keypair(mut self, create_sample_keypair: bool) -> Self {
self.config.create_sample_keypair = create_sample_keypair;
self
}
pub fn certificate_path<T>(mut self, certificate_path: T) -> Self
where
T: Into<PathBuf>,
{
self.config.certificate_path = Some(certificate_path.into());
self
}
pub fn private_key_path<T>(mut self, private_key_path: T) -> Self
where
T: Into<PathBuf>,
{
self.config.private_key_path = Some(private_key_path.into());
self
}
pub fn trust_server_certs(mut self, trust_server_certs: bool) -> Self {
self.config.trust_server_certs = trust_server_certs;
self
}
pub fn verify_server_certs(mut self, verify_server_certs: bool) -> Self {
self.config.verify_server_certs = verify_server_certs;
self
}
pub fn pki_dir<T>(mut self, pki_dir: T) -> Self
where
T: Into<PathBuf>,
{
self.config.pki_dir = pki_dir.into();
self
}
pub fn preferred_locales(mut self, preferred_locales: Vec<String>) -> Self {
self.config.preferred_locales = preferred_locales;
self
}
pub fn default_endpoint<T>(mut self, endpoint_id: T) -> Self
where
T: Into<String>,
{
self.config.default_endpoint = endpoint_id.into();
self
}
pub fn endpoint<T>(mut self, endpoint_id: T, endpoint: ClientEndpoint) -> Self
where
T: Into<String>,
{
self.config.endpoints.insert(endpoint_id.into(), endpoint);
self
}
pub fn endpoints<T>(mut self, endpoints: Vec<(T, ClientEndpoint)>) -> Self
where
T: Into<String>,
{
for e in endpoints {
self.config.endpoints.insert(e.0.into(), e.1);
}
self
}
pub fn user_token<T>(mut self, user_token_id: T, user_token: ClientUserToken) -> Self
where
T: Into<String>,
{
let user_token_id = user_token_id.into();
if user_token_id == ANONYMOUS_USER_TOKEN_ID {
panic!("User token id {} is reserved", user_token_id);
}
self.config.user_tokens.insert(user_token_id, user_token);
self
}
pub fn session_retry_limit(mut self, session_retry_limit: i32) -> Self {
if session_retry_limit < 0 && session_retry_limit != -1 {
panic!("Session retry limit must be -1, 0 or a positive number");
}
self.config.session_retry_limit = session_retry_limit;
self
}
pub fn session_retry_interval(mut self, session_retry_interval: u32) -> Self {
self.config.session_retry_interval = session_retry_interval;
self
}
pub fn session_timeout(mut self, session_timeout: u32) -> Self {
self.config.session_timeout = session_timeout;
self
}
pub fn ignore_clock_skew(mut self) -> Self {
self.config.performance.ignore_clock_skew = true;
self
}
pub fn single_threaded_executor(mut self) -> Self {
self.config.performance.single_threaded_executor = true;
self
}
pub fn multi_threaded_executor(mut self) -> Self {
self.config.performance.single_threaded_executor = false;
self
}
pub fn session_name<T>(mut self, session_name: T) -> Self
where
T: Into<String>,
{
self.config.session_name = session_name.into();
self
}
pub fn max_message_size(mut self, max_message_size: usize) -> Self {
self.config.decoding_options.max_message_size = max_message_size;
self
}
pub fn max_chunk_count(mut self, max_chunk_count: usize) -> Self {
self.config.decoding_options.max_chunk_count = max_chunk_count;
self
}
}
#[test]
fn client_builder() {
use std::str::FromStr;
let b = ClientBuilder::new()
.application_name("appname")
.application_uri("http://appname")
.product_uri("http://product")
.create_sample_keypair(true)
.certificate_path("certxyz")
.private_key_path("keyxyz")
.trust_server_certs(true)
.verify_server_certs(false)
.pki_dir("pkixyz")
.preferred_locales(vec!["a".to_string(), "b".to_string(), "c".to_string()])
.default_endpoint("http://default")
.session_retry_interval(1234)
.session_retry_limit(999)
.session_timeout(777)
.ignore_clock_skew()
.single_threaded_executor()
.session_name("SessionName")
;
let c = b.config();
assert_eq!(c.application_name, "appname");
assert_eq!(c.application_uri, "http://appname");
assert_eq!(c.product_uri, "http://product");
assert_eq!(c.create_sample_keypair, true);
assert_eq!(c.certificate_path, Some(PathBuf::from("certxyz")));
assert_eq!(c.private_key_path, Some(PathBuf::from("keyxyz")));
assert_eq!(c.trust_server_certs, true);
assert_eq!(c.verify_server_certs, false);
assert_eq!(c.pki_dir, PathBuf::from_str("pkixyz").unwrap());
assert_eq!(
c.preferred_locales,
vec!["a".to_string(), "b".to_string(), "c".to_string()]
);
assert_eq!(c.default_endpoint, "http://default");
assert_eq!(c.session_retry_interval, 1234);
assert_eq!(c.session_retry_limit, 999);
assert_eq!(c.session_timeout, 777);
assert_eq!(c.performance.ignore_clock_skew, true);
assert_eq!(c.performance.single_threaded_executor, true);
assert_eq!(c.session_name, "SessionName");
}