1#![allow(non_snake_case)]
2#![allow(unused_doc_comments)]
3
4#[macro_use]
5extern crate error_chain;
6#[macro_use]
7extern crate serde_derive;
8
9pub mod agent;
10pub mod catalog;
11pub mod connect_ca;
12pub mod errors;
13pub mod health;
14pub mod kv;
15pub mod session;
16
17mod request;
18
19use std::env;
20
21use std::time::Duration;
22
23use reqwest::Client as HttpClient;
24use reqwest::ClientBuilder;
25
26use errors::{Result, ResultExt};
27
28#[derive(Clone, Debug)]
29pub struct Client {
30 config: Config,
31}
32
33impl Client {
34 pub fn new(config: Config) -> Self {
35 Client { config }
36 }
37}
38
39#[derive(Clone, Debug)]
40pub struct Config {
41 pub address: String,
42 pub datacenter: Option<String>,
43 pub http_client: HttpClient,
44 pub token: Option<String>,
45 pub wait_time: Option<Duration>,
46}
47
48impl Config {
49 pub fn new() -> Result<Config> {
50 ClientBuilder::new()
51 .build()
52 .chain_err(|| "Failed to build reqwest client")
53 .map(|client| Config {
54 address: String::from("http://localhost:8500"),
55 datacenter: None,
56 http_client: client,
57 token: None,
58 wait_time: None,
59 })
60 }
61
62 pub fn new_from_env() -> Result<Config> {
63 let consul_addr = match env::var("CONSUL_HTTP_ADDR") {
64 Ok(val) => {
65 if val.starts_with("http") {
66 val
67 } else {
68 format!("http://{}", val)
69 }
70 }
71 Err(_e) => String::from("http://127.0.0.1:8500"),
72 };
73 let consul_token = env::var("CONSUL_HTTP_TOKEN").ok();
74 ClientBuilder::new()
75 .build()
76 .chain_err(|| "Failed to build reqwest client")
77 .map(|client| Config {
78 address: consul_addr,
79 datacenter: None,
80 http_client: client,
81 token: consul_token,
82 wait_time: None,
83 })
84 }
85
86 pub fn new_from_consul_host(
87 host: &str,
88 port: Option<u16>,
89 token: Option<String>,
90 ) -> Result<Config> {
91 ClientBuilder::new()
92 .build()
93 .chain_err(|| "Failed to build reqwest client")
94 .map(|client| Config {
95 address: format!("{}:{}", host, port.unwrap_or(8500)),
96 datacenter: None,
97 http_client: client,
98 token,
99 wait_time: None,
100 })
101 }
102}
103
104#[derive(Clone, Debug, Default)]
105pub struct QueryOptions {
106 pub datacenter: Option<String>,
107 pub wait_index: Option<u64>,
108 pub wait_time: Option<Duration>,
109}
110
111#[derive(Clone, Debug)]
112pub struct QueryMeta {
113 pub last_index: Option<u64>,
114 pub request_time: Duration,
115}
116
117#[derive(Clone, Debug, Default)]
118pub struct WriteOptions {
119 pub datacenter: Option<String>,
120}
121
122#[derive(Clone, Debug)]
123pub struct WriteMeta {
124 pub request_time: Duration,
125}