Expand description
API client for etcd.
All API calls require a client.
Implementations§
source§impl Client<HttpConnector>
impl Client<HttpConnector>
sourcepub fn new(
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<HttpConnector>, Error>
pub fn new(
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<HttpConnector>, Error>
Constructs a new client using the HTTP protocol.
Parameters
- handle: A handle to the event loop.
- endpoints: URLs for one or more cluster members. When making an API call, the client will make the call to each member in order until it receives a successful respponse.
- basic_auth: Credentials for HTTP basic authentication.
Errors
Fails if no endpoints are provided or if any of the endpoints is an invalid URL.
source§impl Client<HttpsConnector<HttpConnector>>
impl Client<HttpsConnector<HttpConnector>>
sourcepub fn https(
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<HttpsConnector<HttpConnector>>, Error>
pub fn https(
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<HttpsConnector<HttpConnector>>, Error>
Constructs a new client using the HTTPS protocol.
Parameters
- handle: A handle to the event loop.
- endpoints: URLs for one or more cluster members. When making an API call, the client will make the call to each member in order until it receives a successful respponse.
- basic_auth: Credentials for HTTP basic authentication.
Errors
Fails if no endpoints are provided or if any of the endpoints is an invalid URL.
source§impl<C> Client<C>where
C: Clone + Connect + Sync + 'static,
impl<C> Client<C>where
C: Clone + Connect + Sync + 'static,
sourcepub fn custom(
hyper: Hyper<C>,
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<C>, Error>
pub fn custom(
hyper: Hyper<C>,
endpoints: &[&str],
basic_auth: Option<BasicAuth>
) -> Result<Client<C>, Error>
Constructs a new client using the provided hyper::Client
.
This method allows the user to configure the details of the underlying HTTP client to their liking. It is also necessary when using X.509 client certificate authentication.
Parameters
- hyper: A fully configured
hyper::Client
. - endpoints: URLs for one or more cluster members. When making an API call, the client will make the call to each member in order until it receives a successful respponse.
- basic_auth: Credentials for HTTP basic authentication.
Errors
Fails if no endpoints are provided or if any of the endpoints is an invalid URL.
Examples
Configuring the client to authenticate with both HTTP basic auth and an X.509 client certificate:
use std::fs::File;
use std::io::Read;
use futures::Future;
use hyper::client::HttpConnector;
use hyper_tls::HttpsConnector;
use native_tls::{Certificate, TlsConnector, Identity};
use tokio::runtime::Runtime;
use etcd::{Client, kv};
fn main() {
let mut ca_cert_file = File::open("ca.der").unwrap();
let mut ca_cert_buffer = Vec::new();
ca_cert_file.read_to_end(&mut ca_cert_buffer).unwrap();
let mut pkcs12_file = File::open("/source/tests/ssl/client.p12").unwrap();
let mut pkcs12_buffer = Vec::new();
pkcs12_file.read_to_end(&mut pkcs12_buffer).unwrap();
let mut builder = TlsConnector::builder();
builder.add_root_certificate(Certificate::from_der(&ca_cert_buffer).unwrap());
builder.identity(Identity::from_pkcs12(&pkcs12_buffer, "secret").unwrap());
let tls_connector = builder.build().unwrap();
let mut http_connector = HttpConnector::new(4);
http_connector.enforce_http(false);
let https_connector = HttpsConnector::from((http_connector, tls_connector));
let hyper = hyper::Client::builder().build(https_connector);
let client = Client::custom(hyper, &["https://etcd.example.com:2379"], None).unwrap();
let work = kv::set(&client, "/foo", "bar", None).and_then(move |_| {
let get_request = kv::get(&client, "/foo", kv::GetOptions::default());
get_request.and_then(|response| {
let value = response.data.node.value.unwrap();
assert_eq!(value, "bar".to_string());
Ok(())
})
});
assert!(Runtime::new().unwrap().block_on(work).is_ok());
}