@@ -9,6 +9,8 @@ extern crate serde_derive;
9
9
extern crate serde;
10
10
extern crate serde_json;
11
11
12
+ use reqwest:: header:: * ;
13
+ use reqwest:: mime;
12
14
use std:: path:: PathBuf ;
13
15
use structopt:: StructOpt ;
14
16
@@ -30,6 +32,9 @@ enum Cli {
30
32
#[ structopt( parse( from_os_str) ) ]
31
33
#[ structopt( long = "output" ) ]
32
34
output : Option < PathBuf > ,
35
+ // Set authorizaiton header.
36
+ #[ structopt( long = "authorization" ) ]
37
+ authorization : Option < String > ,
33
38
} ,
34
39
#[ structopt( name = "generate" ) ]
35
40
Generate {
@@ -48,7 +53,8 @@ fn main() -> Result<(), failure::Error> {
48
53
Cli :: IntrospectSchema {
49
54
schema_location,
50
55
output,
51
- } => introspect_schema ( schema_location, output) ,
56
+ authorization,
57
+ } => introspect_schema ( schema_location, output, authorization) ,
52
58
Cli :: Generate {
53
59
paths : _,
54
60
schema : _,
@@ -57,12 +63,14 @@ fn main() -> Result<(), failure::Error> {
57
63
}
58
64
}
59
65
60
- fn introspect_schema ( location : String , output : Option < PathBuf > ) -> Result < ( ) , failure:: Error > {
61
- use reqwest:: header:: * ;
62
- use reqwest:: mime;
66
+ fn introspect_schema (
67
+ location : String ,
68
+ output : Option < PathBuf > ,
69
+ authorization : Option < String > ,
70
+ ) -> Result < ( ) , failure:: Error > {
63
71
use std:: io:: Write ;
64
72
65
- let mut out: Box < Write > = match output {
73
+ let out: Box < Write > = match output {
66
74
Some ( path) => Box :: new ( :: std:: fs:: File :: create ( path) ?) ,
67
75
None => Box :: new ( :: std:: io:: stdout ( ) ) ,
68
76
} ;
@@ -72,10 +80,12 @@ fn introspect_schema(location: String, output: Option<PathBuf>) -> Result<(), fa
72
80
query : introspection_query:: QUERY ,
73
81
} ;
74
82
83
+ let headers = set_headers ( authorization) ;
84
+
75
85
let client = reqwest:: Client :: new ( ) ;
76
86
let mut res = client
77
87
. post ( & location)
78
- . header ( Accept ( vec ! [ qitem ( mime :: APPLICATION_JSON ) ] ) )
88
+ . headers ( headers )
79
89
. json ( & request_body)
80
90
. send ( ) ?;
81
91
@@ -89,3 +99,14 @@ fn introspect_schema(location: String, output: Option<PathBuf>) -> Result<(), fa
89
99
let json: serde_json:: Value = res. json ( ) ?;
90
100
Ok ( serde_json:: to_writer_pretty ( out, & json) ?)
91
101
}
102
+
103
+ fn set_headers ( authorization : Option < String > ) -> Headers {
104
+ let mut headers = Headers :: new ( ) ;
105
+ headers. set ( Accept ( vec ! [ qitem( mime:: APPLICATION_JSON ) ] ) ) ;
106
+
107
+ match authorization {
108
+ Some ( token) => headers. set ( reqwest:: header:: Authorization ( token) ) ,
109
+ None => ( ) ,
110
+ } ;
111
+ headers
112
+ }
0 commit comments