Skip to content

Commit f5f1c71

Browse files
committed
add some url manipulation, and staging for using the 'hyper' crate
1 parent 4a3d29a commit f5f1c71

3 files changed

Lines changed: 104 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ rand = "0.3.16"
88
clap = "2.26.2"
99
hyper = "0.11"
1010
futures = "0.1"
11-
tokio-core = "0.1"
11+
tokio-core = "0.1"
12+
url = "1.5.1"

src/main.rs

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,60 @@
11
extern crate clap;
22
extern crate hyper;
3+
extern crate url;
34

45
use clap::{Arg, ArgMatches, App};
5-
use hyper::Uri;
6+
use hyper::{Method, Request, Uri};
7+
use hyper::header::Authorization;
8+
use hyper::header;
69
use std::str::FromStr;
10+
use url::{Url};
711

812
fn main() {
913
match ClientConfig::from_cli_args() {
10-
Ok(config) => println!("arguments are good: {:?}", config),
14+
Ok(config) => {
15+
println!("arguments are good: {:?}", config);
16+
make_test_request(&config);
17+
},
1118
Err(ConfigError::MissingAuth) => println!("Authorization info missing or incomplete. Either an API Key or a Username + Password must be provided"),
1219
Err(ConfigError::MissingUrl) => println!("Missing the Base URL"),
1320
Err(ConfigError::InvalidUrl) => println!("Invalid Base URL"),
1421
};
1522
}
1623

24+
fn make_test_request(config: &ClientConfig) -> () {
25+
let uri = config.api_uri(&["x", "projects"]);
26+
27+
let mut req = Request::new(Method::Get, uri);
28+
config.auth_info.apply_to(&mut req);
29+
println!("Request:\n{:?}", req);
30+
}
31+
1732
/// Connection information for Code Dx.
1833
#[derive(Debug)]
1934
struct ClientConfig {
20-
base_url: hyper::Uri,
35+
base_url: Url,
2136
auth_info: ClientAuthInfo
2237
}
2338

39+
impl ClientConfig {
40+
fn api_uri(&self, segments: &[&str]) -> Uri {
41+
let mut url = self.base_url.clone();
42+
43+
// open a scope to borrow the url mutably
44+
{
45+
let mut url_segments = url.path_segments_mut()
46+
.expect("Can't apply a path to base-url");
47+
for segment in segments {
48+
url_segments.push(segment);
49+
}
50+
}
51+
// now that the mutable borrow is done, we can use url again
52+
53+
Uri::from_str(&url.into_string())
54+
.expect("Somehow failed to convert a valid URL to a URI")
55+
}
56+
}
57+
2458
/// Authentication credentials for connecting to Code Dx.
2559
/// Both "basic auth" (username + password) and "api key" are supported.
2660
#[derive(Debug)]
@@ -29,6 +63,26 @@ enum ClientAuthInfo {
2963
ApiKey(String),
3064
}
3165

66+
impl ClientAuthInfo {
67+
fn apply_to<'a>(&self, request: &'a mut Request) {
68+
match *self {
69+
ClientAuthInfo::Basic{ ref username, ref password } => {
70+
let mut headers = request.headers_mut();
71+
headers.set(Authorization(
72+
header::Basic{
73+
username: username.to_owned(),
74+
password: Some(password.to_owned())
75+
}
76+
));
77+
},
78+
ClientAuthInfo::ApiKey(ref key) => {
79+
let mut headers = request.headers_mut();
80+
headers.set_raw("API-Key", key.to_owned());
81+
},
82+
}
83+
}
84+
}
85+
3286
/// Things that can go wrong when parsing a `ClientConfig`
3387
#[derive(Debug)]
3488
enum ConfigError {
@@ -82,7 +136,7 @@ impl ClientConfig {
82136

83137
let base_uri = match matches.value_of("base-url") {
84138
None => Err(ConfigError::MissingUrl),
85-
Some(raw) => Uri::from_str(raw).map_err(|_| ConfigError::InvalidUrl),
139+
Some(raw) => Url::parse(raw).map_err(|_| ConfigError::InvalidUrl),
86140
};
87141

88142
let client_auth_info = match matches.value_of("api-key") {

0 commit comments

Comments
 (0)