Skip to content

Commit 48e0a3d

Browse files
authored
Merge pull request #92 from h-michael/authorization-header
Enable set authorization header
2 parents d5e460a + 135c0c6 commit 48e0a3d

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

graphql_client_cli/src/main.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ extern crate serde_derive;
99
extern crate serde;
1010
extern crate serde_json;
1111

12+
use reqwest::header::*;
13+
use reqwest::mime;
1214
use std::path::PathBuf;
1315
use structopt::StructOpt;
1416

@@ -30,6 +32,9 @@ enum Cli {
3032
#[structopt(parse(from_os_str))]
3133
#[structopt(long = "output")]
3234
output: Option<PathBuf>,
35+
// Set authorizaiton header.
36+
#[structopt(long = "authorization")]
37+
authorization: Option<String>,
3338
},
3439
#[structopt(name = "generate")]
3540
Generate {
@@ -48,7 +53,8 @@ fn main() -> Result<(), failure::Error> {
4853
Cli::IntrospectSchema {
4954
schema_location,
5055
output,
51-
} => introspect_schema(schema_location, output),
56+
authorization,
57+
} => introspect_schema(schema_location, output, authorization),
5258
Cli::Generate {
5359
paths: _,
5460
schema: _,
@@ -57,12 +63,14 @@ fn main() -> Result<(), failure::Error> {
5763
}
5864
}
5965

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> {
6371
use std::io::Write;
6472

65-
let mut out: Box<Write> = match output {
73+
let out: Box<Write> = match output {
6674
Some(path) => Box::new(::std::fs::File::create(path)?),
6775
None => Box::new(::std::io::stdout()),
6876
};
@@ -72,10 +80,12 @@ fn introspect_schema(location: String, output: Option<PathBuf>) -> Result<(), fa
7280
query: introspection_query::QUERY,
7381
};
7482

83+
let headers = set_headers(authorization);
84+
7585
let client = reqwest::Client::new();
7686
let mut res = client
7787
.post(&location)
78-
.header(Accept(vec![qitem(mime::APPLICATION_JSON)]))
88+
.headers(headers)
7989
.json(&request_body)
8090
.send()?;
8191

@@ -89,3 +99,14 @@ fn introspect_schema(location: String, output: Option<PathBuf>) -> Result<(), fa
8999
let json: serde_json::Value = res.json()?;
90100
Ok(serde_json::to_writer_pretty(out, &json)?)
91101
}
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

Comments
 (0)