@@ -2,17 +2,19 @@ extern crate clap;
22extern crate futures;
33extern crate hyper;
44extern crate serde;
5- extern crate serde_json;
6- #[ macro_use]
7- extern crate serde_derive;
85extern crate tokio_core;
96extern crate url;
107
8+ #[ macro_use] extern crate maplit;
9+ #[ macro_use] extern crate serde_json;
10+ #[ macro_use] extern crate serde_derive;
11+
1112use clap:: { Arg , ArgMatches , App } ;
1213use futures:: { Future , Stream } ;
1314use hyper:: { Client , Method , Request , Uri } ;
1415use hyper:: header;
1516use hyper:: header:: Authorization ;
17+ use std:: collections:: HashMap ;
1618use std:: option:: Option :: * ;
1719use std:: str:: FromStr ;
1820use tokio_core:: reactor:: Core ;
@@ -26,21 +28,48 @@ fn main() {
2628 let request = client. get_project_list ( ) ;
2729 match client. run ( request) {
2830 Ok ( projects) => {
31+ println ! ( "All projects:" ) ;
2932 for project in projects {
30- println ! ( "{:?}" , project) ;
33+ println ! ( " {:?}" , project) ;
3134 }
3235 } ,
3336 Err ( e) => {
3437 println ! ( "Error in request: {:?}" , e) ;
3538 }
3639 }
40+
41+ println ! ( "" ) ;
42+
43+ let query_projects = client. query_projects ( & ApiProjectFilter {
44+ name : Some ( "scrape" ) ,
45+ metadata : Some ( hashmap ! {
46+ "Owner" => "dylan"
47+ } )
48+ } ) ;
49+ match client. run ( query_projects) {
50+ Ok ( projects) => {
51+ println ! ( "Projects with a name matching 'scrape' and owned by 'dylan':" ) ;
52+ for project in projects {
53+ println ! ( " {:?}" , project) ;
54+ }
55+ } ,
56+ Err ( e) => {
57+ println ! ( "Error in request: {:?}" , e)
58+ }
59+ }
3760 } ,
3861 Err ( ConfigError :: MissingAuth ) => println ! ( "Authorization info missing or incomplete. Either an API Key or a Username + Password must be provided" ) ,
3962 Err ( ConfigError :: MissingUrl ) => println ! ( "Missing the Base URL" ) ,
4063 Err ( ConfigError :: InvalidUrl ) => println ! ( "Invalid Base URL" ) ,
4164 } ;
4265}
4366
67+ #[ derive( Serialize ) ]
68+ struct ApiProjectFilter < ' a > {
69+ name : Option < & ' a str > ,
70+ metadata : Option < HashMap < & ' a str , & ' a str > >
71+ }
72+
4473#[ derive( Deserialize , Debug ) ]
4574struct ApiProject {
4675 id : u32 ,
@@ -59,6 +88,22 @@ struct ApiClient<'a> {
5988 client : Client < hyper:: client:: HttpConnector , hyper:: Body >
6089}
6190
91+ enum ReqBody {
92+ Json ( serde_json:: Value ) ,
93+ // TODO: support multipart forms
94+ }
95+
96+ impl From < ReqBody > for hyper:: Body {
97+ fn from ( body : ReqBody ) -> hyper:: Body {
98+ match body {
99+ ReqBody :: Json ( value) => {
100+ let raw_body = serde_json:: to_vec ( & value) . unwrap ( ) ;
101+ hyper:: Body :: from ( raw_body)
102+ } ,
103+ }
104+ }
105+ }
106+
62107impl < ' a > ApiClient < ' a > {
63108 fn new ( config : & ' a ClientConfig ) -> std:: io:: Result < ApiClient > {
64109 Core :: new ( ) . map ( |core| {
@@ -75,6 +120,13 @@ impl <'a> ApiClient<'a> {
75120 self . request_json ( Method :: Get , & [ "x" , "projects" ] )
76121 }
77122
123+ fn query_projects < ' f > ( & self , filter : & ' f ApiProjectFilter ) -> BoxedClientFuture < Vec < ApiProject > > {
124+ let body = ReqBody :: Json ( json ! ( {
125+ "filter" : filter
126+ } ) ) ;
127+ self . request_json_with_body ( Method :: Post , & [ "x" , "projects" , "query" ] , Some ( body) )
128+ }
129+
78130 /// Underlying method for creating a request to be sent to Code Dx.
79131 ///
80132 /// Accepts a `method` (GET, POST, etc)
0 commit comments