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
//! This module contains the client for asynchronous communcation.

use super::{Host, HORIZON_TEST_URI, HORIZON_URI};
use error::{Error, Result};
use http;
use hyper;
use hyper_tls::HttpsConnector;
use tokio_core::reactor::Handle;

/// A client that can issue requests to a horizon api.
#[derive(Debug, Clone)]
pub struct Client {
    inner: hyper::Client<HttpsConnector<hyper::client::HttpConnector>>,
    host: Host,
}

impl Client {
    /// Constructs a new stellar client.
    ///
    /// ## Examples
    ///
    /// ```
    /// # extern crate tokio_core;
    /// # extern crate stellar_client;
    /// # fn main() {
    /// use tokio_core::reactor::Core;
    /// use stellar_client::async::Client;
    /// let core = Core::new().unwrap();
    /// let client = Client::new("https://horizon-testnet.stellar.org", &core.handle()).unwrap();
    /// # }
    /// ```
    pub fn new(uri: &str, handle: &Handle) -> Result<Self> {
        // Ensure that the uri passed in can parse.
        let _: http::Uri = uri.parse()?;
        Self::build(Host::Other(uri.to_string()), &handle)
    }

    fn build(host: Host, handle: &Handle) -> Result<Self> {
        let inner = hyper::Client::configure()
            .connector(HttpsConnector::new(4, &handle).map_err(|_| Error::BadSSL)?)
            .build(&handle);
        Ok(Client { host, inner })
    }

    /// Constructs a new stellar client connected to the horizon test network.
    ///
    /// ## Examples
    ///
    /// ```
    /// # extern crate tokio_core;
    /// # extern crate stellar_client;
    /// # fn main() {
    /// use tokio_core::reactor::Core;
    /// use stellar_client::async::Client;
    /// let core = Core::new().unwrap();
    /// let client = Client::horizon_test(&core.handle()).unwrap();
    /// # }
    /// ```
    pub fn horizon_test(handle: &Handle) -> Result<Self> {
        Self::build(Host::HorizonTest, &handle)
    }

    /// Returns true if this is a test client.
    ///
    /// ## Examples
    ///
    /// ```
    /// # extern crate tokio_core;
    /// # extern crate stellar_client;
    /// # fn main() {
    /// # use tokio_core::reactor::Core;
    /// # use stellar_client::async::Client;
    /// # let core = Core::new().unwrap();
    /// let client = Client::horizon_test(&core.handle()).unwrap();
    /// assert!(!client.is_horizon());
    /// assert!(client.is_horizon_test());
    /// # }
    /// ```
    pub fn is_horizon_test(&self) -> bool {
        self.host == Host::HorizonTest
    }

    /// Constructs a new stellar client connected to the horizon prod network.
    ///
    /// ## Examples
    ///
    /// ```
    /// # extern crate tokio_core;
    /// # extern crate stellar_client;
    /// # fn main() {
    /// use tokio_core::reactor::Core;
    /// use stellar_client::async::Client;
    /// let core = Core::new().unwrap();
    /// let client = Client::horizon(&core.handle()).unwrap();
    /// # }
    /// ```
    pub fn horizon(handle: &Handle) -> Result<Self> {
        Self::build(Host::HorizonProd, &handle)
    }

    /// Returns true if this is a horizon@stellar client.
    ///
    /// ## Examples
    ///
    /// ```
    /// # extern crate tokio_core;
    /// # extern crate stellar_client;
    /// # fn main() {
    /// # use tokio_core::reactor::Core;
    /// # use stellar_client::async::Client;
    /// # let core = Core::new().unwrap();
    /// let client = Client::horizon(&core.handle()).unwrap();
    /// assert!(client.is_horizon());
    /// assert!(!client.is_horizon_test());
    /// # }
    /// ```
    pub fn is_horizon(&self) -> bool {
        self.host == Host::HorizonProd
    }

    #[allow(dead_code)]
    fn uri(&self) -> &str {
        match self.host {
            Host::HorizonTest => HORIZON_TEST_URI,
            Host::HorizonProd => HORIZON_URI,
            Host::Other(ref uri) => uri,
        }
    }
}

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

    #[test]
    fn it_constructs_a_test_client() {
        let core = Core::new().unwrap();
        let client = Client::horizon_test(&core.handle()).unwrap();
        assert_eq!(client.host, Host::HorizonTest);
        assert_eq!(client.uri(), "https://horizon-testnet.stellar.org");
    }

    #[test]
    fn it_constructs_a_horizon_client() {
        let core = Core::new().unwrap();
        let client = Client::horizon(&core.handle()).unwrap();
        assert_eq!(client.host, Host::HorizonProd);
        assert_eq!(client.uri(), "https://horizon.stellar.org");
    }

    #[test]
    fn it_constructs_a_client_to_other() {
        let core = Core::new().unwrap();
        let client = Client::new("https://www.google.com", &core.handle()).unwrap();
        assert_eq!(
            client.host,
            Host::Other("https://www.google.com".to_string())
        );
        assert_eq!(client.uri(), "https://www.google.com");
    }

    #[test]
    fn it_errs_if_a_bad_uri_is_provided() {
        let core = Core::new().unwrap();
        let result = Client::new("htps:/www", &core.handle());
        assert!(result.is_err());
    }
}