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
use super::{Host, HORIZON_TEST_URI, HORIZON_URI};
use endpoint::IntoRequest;
use error::{Error, Result};
use http::{self, Uri};
use reqwest;
use serde_json;
use std::time::Duration;
use StellarError;
mod iter;
pub use self::iter::Iter;
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
#[derive(Debug, Clone)]
pub struct Client {
inner: reqwest::Client,
host: Host,
}
impl Client {
pub fn new(uri: &str) -> Result<Self> {
let _: Uri = uri.parse()?;
Self::build(Host::Other(uri.to_string()))
}
fn build(host: Host) -> Result<Self> {
let inner = reqwest::ClientBuilder::new()
.timeout(Some(DEFAULT_TIMEOUT))
.build()
.expect("Http client failed to build");
Ok(Client { host, inner })
}
pub fn horizon_test() -> Result<Self> {
Self::build(Host::HorizonTest)
}
pub fn is_horizon_test(&self) -> bool {
self.host == Host::HorizonTest
}
pub fn horizon() -> Result<Self> {
Self::build(Host::HorizonProd)
}
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,
}
}
pub fn request<E>(&self, endpoint: E) -> Result<E::Response>
where
E: IntoRequest,
{
let request = endpoint.into_request(&self.uri())?;
let request = Self::http_to_reqwest(&request);
let response = self.inner.execute(request)?;
if response.status().is_success() {
let resp: E::Response = serde_json::from_reader(response)?;
Ok(resp)
} else if response.status().is_client_error() {
let e: StellarError = serde_json::from_reader(response)?;
Err(Error::BadResponse(e))
} else {
Err(Error::ServerError)
}
}
fn http_to_reqwest<T>(request: &http::Request<T>) -> reqwest::Request {
use http::method::Method;
let method = match *request.method() {
Method::GET => reqwest::Method::Get,
_ => unimplemented!(),
};
let url: reqwest::Url = format!("{}", request.uri()).parse().unwrap();
reqwest::Request::new(method, url)
}
}
#[cfg(test)]
mod tests {
use super::*;
use stellar_error;
#[test]
fn it_constructs_a_test_client() {
let client = Client::horizon_test().unwrap();
assert_eq!(client.host, Host::HorizonTest);
assert_eq!(client.uri(), "https://horizon-testnet.stellar.org");
}
#[test]
fn it_constructs_a_horizon_client() {
let client = Client::horizon().unwrap();
assert_eq!(client.host, Host::HorizonProd);
assert_eq!(client.uri(), "https://horizon.stellar.org");
}
#[test]
fn it_constructs_a_client_to_other() {
let client = Client::new("https://www.google.com").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 result = Client::new("htps:/www");
assert!(result.is_err());
}
#[test]
fn it_can_make_a_request() {
use endpoint::account::Details;
let client = Client::horizon_test().unwrap();
let endpoint = Details::new("GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
let account = client.request(endpoint).unwrap();
assert_eq!(
account.id(),
"GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"
);
}
#[test]
fn it_can_make_a_failed_request() {
use endpoint::account::Details;
let client = Client::horizon_test().unwrap();
let endpoint = Details::new("LDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ");
match client.request(endpoint).unwrap_err() {
Error::BadResponse(error) => assert_eq!(error.kind(), stellar_error::Kind::NotFound),
error => panic!("Client did not return a bad response {:?}", error),
}
}
}