pub mod api;
pub mod client;
pub mod config;
pub mod error;
pub mod types;
pub use client::HttpClient;
pub use config::{Config, ConfigBuilder, HttpBasicAuth, TlsConfig};
pub use error::{ConsulError, Result};
pub use types::*;
use std::sync::Arc;
use api::{ACL, Agent, Catalog, Health, KV, Session};
pub struct Client {
http_client: Arc<HttpClient>,
kv: KV,
agent: Agent,
catalog: Catalog,
health: Health,
session: Session,
acl: ACL,
}
impl Client {
pub fn new(config: Config) -> Result<Self> {
let http_client = Arc::new(HttpClient::new(config)?);
Ok(Self {
kv: KV::new(http_client.clone()),
agent: Agent::new(http_client.clone()),
catalog: Catalog::new(http_client.clone()),
health: Health::new(http_client.clone()),
session: Session::new(http_client.clone()),
acl: ACL::new(http_client.clone()),
http_client,
})
}
pub fn from_env() -> Result<Self> {
let config = Config::from_env()?;
Self::new(config)
}
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
pub fn kv(&self) -> &KV {
&self.kv
}
pub fn agent(&self) -> &Agent {
&self.agent
}
pub fn catalog(&self) -> &Catalog {
&self.catalog
}
pub fn health(&self) -> &Health {
&self.health
}
pub fn session(&self) -> &Session {
&self.session
}
pub fn acl(&self) -> &ACL {
&self.acl
}
pub fn http_client(&self) -> &HttpClient {
&self.http_client
}
pub async fn ping(&self) -> Result<()> {
self.http_client.ping().await
}
}
pub struct ClientBuilder {
config: Config,
}
impl ClientBuilder {
pub fn new() -> Self {
Self {
config: Config::default(),
}
}
pub fn address(mut self, address: &str) -> Self {
self.config.address = address.to_string();
self
}
pub fn scheme(mut self, scheme: &str) -> Self {
self.config.scheme = scheme.to_string();
self
}
pub fn datacenter(mut self, dc: &str) -> Self {
self.config.datacenter = Some(dc.to_string());
self
}
pub fn token(mut self, token: &str) -> Self {
self.config.token = Some(token.to_string());
self
}
pub fn http_auth(mut self, username: &str, password: &str) -> Self {
self.config.http_auth = Some(HttpBasicAuth::new(username, password));
self
}
pub fn tls_config(mut self, tls: TlsConfig) -> Self {
self.config.tls_config = Some(tls);
self
}
pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
self.config.timeout = timeout;
self
}
pub fn namespace(mut self, ns: &str) -> Self {
self.config.namespace = Some(ns.to_string());
self
}
pub fn partition(mut self, partition: &str) -> Self {
self.config.partition = Some(partition.to_string());
self
}
pub fn build(self) -> Result<Client> {
Client::new(self.config)
}
}
impl Default for ClientBuilder {
fn default() -> Self {
Self::new()
}
}
pub mod prelude {
pub use crate::{
Client, ClientBuilder, Config, ConfigBuilder, ConsulError, QueryOptions, Result, TlsConfig,
WriteOptions,
api::{
acl::{ACLPolicy, ACLToken, ACLTokenPolicyLink},
agent::{AgentServiceCheck, AgentServiceRegistration},
catalog::{CatalogDeregistration, CatalogRegistration, CatalogServiceRegistration},
kv::KVPair,
session::{SessionBehavior, SessionRequest},
},
};
}