1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#[macro_use]
extern crate error_chain;
extern crate futures;
extern crate hyper;
#[cfg(feature = "tls")]
extern crate hyper_tls;
#[macro_use]
extern crate log;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate serde_urlencoded;
extern crate tokio_core;

use std::collections::BTreeMap;

use futures::{Future as StdFuture, IntoFuture, Stream as StdStream};
use futures::future::Either;
#[cfg(feature = "tls")]
use hyper_tls::HttpsConnector;
use hyper::{Client as HyperClient, Method};
use hyper::client::{Connect, HttpConnector, Request};
use hyper::header::Authorization;
use serde::de::DeserializeOwned;
use tokio_core::reactor::Handle;

mod error {
    use std::io::Error as IoError;
    use hyper::Error as HttpError;
    use hyper::error::UriError;
    use serde_json::error::Error as SerdeError;
    error_chain! {
        foreign_links {
            Codec(SerdeError);
            Http(HttpError);
            IO(IoError);
            URI(UriError);
        }
    }
}

pub use error::{Error, ErrorKind, Result};

/// A type alias for a `Future` that may result in a `launchdarkly::Error`
pub type Future<T> = Box<StdFuture<Item = T, Error = Error>>;

const API_HOST: &str = "https://app.launchdarkly.com";

#[derive(Debug, Deserialize)]
pub struct Link {
    pub href: String,
    #[serde(rename = "type")]
    pub link_type: String,
}

#[derive(Debug, Deserialize)]
pub struct Links {
    #[serde(rename = "self")]
    pub _self: Link,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeatureFlag {
    pub key: String,
    pub name: String,
    pub kind: String,
    pub creation_date: usize,
    pub include_in_snippet: bool,
    pub temporary: bool,
    pub maintainer_id: Option<String>,
    pub tags: Vec<String>,
    pub variations: Vec<Variation>,
    #[serde(rename = "_links")]
    pub _links: Links,
    #[serde(rename = "_maintainer")]
    pub _maintainer: Option<Member>,
    pub environments: BTreeMap<String, FeatureFlagConfig>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FeatureFlagConfig {
    pub on: bool,
    pub archived: bool,
    pub salt: String,
    pub sel: String,
    pub last_modified: usize,
    pub version: usize,
    pub targets: Vec<Target>,
    pub rules: Vec<Rule>,
    pub fallthrough: Fallthrough,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Fallthrough {
    pub variation: Option<usize>,
    pub rollout: Option<Rollout>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Target {
    pub values: Vec<String>,
    pub variation: usize,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Rule {
    pub variation: Option<usize>,
    pub rollout: Option<Rollout>,
    pub clause: Option<Vec<Clause>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Clause {
    pub attribute: String,
    pub op: String,
    pub values: Vec<String>,
    pub negate: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Rollout {
    pub variations: Vec<WeightedVariation>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WeightedVariation {
    pub variation: usize,
    pub weight: usize,
}

#[derive(Debug, Deserialize)]
pub struct FeatureFlags {
    pub _links: Links,
    pub items: Vec<FeatureFlag>,
}

#[derive(Debug, Deserialize)]
pub struct Variation {
    pub name: Option<String>,
    pub description: Option<String>,
    pub value: ::serde_json::Value,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Member {
    #[serde(rename = "_links")]
    pub _links: Links,
    #[serde(rename = "_id")]
    pub _id: String,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub role: String,
    pub email: String,
    #[serde(rename = "_pending_invite")]
    pub _pending_invite: Option<bool>,
    pub is_beta: Option<bool>,
    pub custom_roles: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
pub struct Projects {
    #[serde(rename = "_links")]
    pub _links: Links,
    pub items: Vec<Project>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Project {
    #[serde(rename = "_links")]
    pub _links: Links,
    pub key: String,
    pub name: String,
    pub include_in_snippet_by_default: bool,
    pub environments: Vec<Environment>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pubnub {
    pub channel: String,
    pub cipher_key: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Environment {
    #[serde(rename = "_id")]
    pub _id: String,
    #[serde(rename = "_pubnub")]
    pub _pubnub: Pubnub,
    pub key: String,
    pub name: String,
    pub api_key: String,
    pub mobile_key: String,
    pub color: String,
    pub default_ttl: usize,
    pub secure_mode: bool,
}

#[derive(Debug, Serialize, Default)]
pub struct FlagOptions {
    pub env: Option<String>,
    pub tag: Option<String>,
}

#[derive(Clone, Debug)]
pub struct Client<C>
where
    C: Clone + Connect,
{
    http: HyperClient<C>,
    key: String,
}

#[cfg(feature = "tls")]
impl Client<HttpsConnector<HttpConnector>> {
    pub fn new<K>(key: K, handle: &Handle) -> Self
    where
        K: Into<String>,
    {
        let connector = HttpsConnector::new(4, handle).unwrap();
        let http = HyperClient::configure()
            .connector(connector)
            .keep_alive(true)
            .build(handle);
        Self::custom(key, http)
    }
}

impl<C> Client<C>
where
    C: Clone + Connect,
{
    pub fn custom<K>(credentials: K, http: HyperClient<C>) -> Self
    where
        K: Into<String>,
    {
        Self {
            http,
            key: credentials.into(),
        }
    }

    pub fn projects(&self) -> Future<Projects> {
        self.request(Method::Get, "/projects".into(), None)
    }

    pub fn flags<P>(&self, project: P, options: &FlagOptions) -> Future<FeatureFlags>
    where
        P: Into<String>,
    {
        self.request(
            Method::Get,
            format!(
                "/flags/{project}?{query}",
                project = project.into(),
                query = serde_urlencoded::to_string(options).unwrap()
            ),
            None,
        )
    }

    pub fn request<Out>(&self, method: Method, uri: String, body: Option<Vec<u8>>) -> Future<Out>
    where
        Out: DeserializeOwned + 'static,
    {
        let client = self.clone();
        let key = self.key.clone();
        let response = format!("{}/api/v2{}", API_HOST, uri)
            .parse()
            .into_future()
            .map_err(Error::from)
            .and_then(move |url| {
                let mut req = Request::new(method, url);
                req.headers_mut().set(Authorization(key));
                if let Some(body) = body {
                    req.set_body(body)
                }
                client.http.request(req).map_err(Error::from)
            });
        Box::new(response.and_then(move |response| {
            if response.status().is_success() {
                Either::A(response.body().concat2().map_err(Error::from).and_then(
                    move |response_body| {
                        debug!("{}", String::from_utf8_lossy(&response_body));
                        serde_json::from_slice::<Out>(&response_body)
                            .map_err(|error| ErrorKind::Codec(error).into())
                    },
                ))
            } else {
                Either::B(response.body().concat2().map_err(Error::from).and_then(
                    move |response_body| {
                        debug!("{}", String::from_utf8_lossy(&response_body));
                        Err(String::from("fail").into()).into_future()
                    },
                ))
            }
        }))
    }
}