Skip to content

Commit 037f824

Browse files
committed
follow 'hyper' example to run a test request
1 parent f5f1c71 commit 037f824

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

src/main.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
extern crate clap;
22
extern crate hyper;
33
extern crate url;
4+
extern crate futures;
5+
extern crate tokio_core;
46

57
use clap::{Arg, ArgMatches, App};
6-
use hyper::{Method, Request, Uri};
8+
use hyper::{Client, Method, Request, Uri};
79
use hyper::header::Authorization;
810
use hyper::header;
911
use std::str::FromStr;
1012
use url::{Url};
13+
use tokio_core::reactor::Core;
14+
use futures::{Future, Stream};
15+
use std::io::{self, Write};
1116

1217
fn main() {
1318
match ClientConfig::from_cli_args() {
@@ -21,12 +26,30 @@ fn main() {
2126
};
2227
}
2328

24-
fn make_test_request(config: &ClientConfig) -> () {
29+
fn make_test_request(config: &ClientConfig) {
2530
let uri = config.api_uri(&["x", "projects"]);
2631

2732
let mut req = Request::new(Method::Get, uri);
2833
config.auth_info.apply_to(&mut req);
2934
println!("Request:\n{:?}", req);
35+
36+
let mut core = Core::new()
37+
.expect("Failed to create an event loop for making HTTP requests");
38+
let client = Client::new(&core.handle());
39+
40+
let work = client.request(req).and_then(|res| {
41+
println!("Response: {}", res.status());
42+
43+
res.body().for_each(|chunk| {
44+
io::stdout()
45+
.write_all(&chunk)
46+
.map(|_| ())
47+
.map_err(From::from)
48+
})
49+
});
50+
51+
core.run(work)
52+
.expect("Failed to make request");
3053
}
3154

3255
/// Connection information for Code Dx.

0 commit comments

Comments
 (0)