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
// TODO: deny missing docs
#![allow(missing_docs)]

use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Handle;
use serde_json;

// TODO: use https
static BASE_URI: &'static &str = &"http://www.ungefiltert-surfen.de/nameserver";

#[derive(Deserialize)]
pub struct Server {
    pub ip: String,
    pub name: String,
    pub country_id: String,
    pub city: Option<String>,
    pub version: Option<String>,
    pub error: Option<String>,
    pub dnssec: bool,
    pub reliability: f32,
    pub checked_at: String,
    pub created_at: String,
}

pub fn retrieve_servers(
    loop_handle: &Handle,
    country_id: &str,
) -> Box<Future<Item = Vec<Server>, Error = Error>> {
    let uri = format!("{}/{}.json", BASE_URI, country_id);
    let uri = uri.parse().unwrap(); // TODO: .chain_err(|| ErrorKind::RetrievalError);
    let client = Client::new(loop_handle);

    Box::new(
        client
            .get(uri)
            .and_then(|res| {
                res.body().concat2().and_then(move |body| {
                    // TODO: Error
                    let v: Vec<Server> = serde_json::from_slice(&body).unwrap();

                    Ok(v)
                })
            })
            .map_err(move |e| Error::with_chain(e, ErrorKind::RetrievalError)),
    )
}

error_chain! {
    errors {
        RetrievalError {
            description("Failed to retrieve DNS servers")
            display("Failed to retrieve DNS servers")
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use tokio_core::reactor::Core;

    #[test]
    fn retrieve_de_servers() {
        let mut io_loop = Core::new().unwrap();

        let retrieve = retrieve_servers(&io_loop.handle(), "de");
        let result = io_loop.run(retrieve);
        let servers: Vec<Server> = result.unwrap();

        assert!(servers.len() > 0);
    }
}