@@ -3,6 +3,11 @@ extern crate hyper;
33extern crate url;
44extern crate futures;
55extern crate tokio_core;
6+ extern crate serde;
7+ extern crate serde_json;
8+
9+ #[ macro_use]
10+ extern crate serde_derive;
611
712use clap:: { Arg , ArgMatches , App } ;
813use hyper:: { Client , Method , Request , Uri } ;
@@ -12,7 +17,6 @@ use std::str::FromStr;
1217use url:: { Url } ;
1318use tokio_core:: reactor:: Core ;
1419use futures:: { Future , Stream } ;
15- use std:: io:: { self , Write } ;
1620
1721fn main ( ) {
1822 match ClientConfig :: from_cli_args ( ) {
@@ -26,6 +30,14 @@ fn main() {
2630 } ;
2731}
2832
33+ #[ derive( Deserialize , Debug ) ]
34+ struct ApiProject {
35+ id : u32 ,
36+ name : String ,
37+ #[ serde( rename = "parentId" ) ]
38+ parent_id : Option < u32 >
39+ }
40+
2941fn make_test_request ( config : & ClientConfig ) {
3042 let uri = config. api_uri ( & [ "x" , "projects" ] ) ;
3143
@@ -35,17 +47,34 @@ fn make_test_request(config: &ClientConfig) {
3547
3648 let mut core = Core :: new ( )
3749 . expect ( "Failed to create an event loop for making HTTP requests" ) ;
38- let client = Client :: new ( & core. handle ( ) ) ;
50+ let client: Client < hyper :: client :: HttpConnector , hyper :: Body > = Client :: new ( & core. handle ( ) ) ;
3951
4052 let work = client. request ( req) . and_then ( |res| {
41- println ! ( "Response: {}" , res. status( ) ) ;
53+ println ! ( "Response Status: {}" , res. status( ) ) ;
54+ res. body ( )
55+ . fold ( Vec :: new ( ) , |mut acc, chunk| {
56+ acc. extend_from_slice ( & * chunk) ;
57+ futures:: future:: ok :: < _ , hyper:: Error > ( acc)
58+ } )
59+ . map ( |body| {
60+ let des: Vec < ApiProject > = serde_json:: from_slice ( & body) . unwrap ( ) ;
61+ for project in des {
62+ println ! ( "{:?}" , project) ;
63+ }
64+ } )
4265
43- res. body ( ) . for_each ( |chunk| {
44- io:: stdout ( )
45- . write_all ( & chunk)
46- . map ( |_| ( ) )
47- . map_err ( From :: from)
48- } )
66+
67+ // let foo = res.body().concat().and_then(|body| {
68+ //
69+ // });
70+ // let x = serde_json::from_reader(res.body());
71+ // println!("JSON: {:?}", x);
72+ // res.body().for_each(|chunk| {
73+ // io::stdout()
74+ // .write_all(&chunk)
75+ // .map(|_| ())
76+ // .map_err(From::from)
77+ // })
4978 } ) ;
5079
5180 core. run ( work)
0 commit comments