diff --git a/src/main.rs b/src/main.rs index cb6b1f0..d568aa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ extern crate hyper; extern crate hyper_native_tls; +extern crate url; use std::env; use std::io; @@ -7,18 +8,22 @@ use hyper::Client; use hyper::header::{Headers, Authorization, Basic}; use hyper::net::HttpsConnector; use hyper_native_tls::NativeTlsClient; +use url::{ParseError, Url}; -fn collection_uri(hatena_id: &str, blog_id: &str) -> String { - format!("https://blog.hatena.ne.jp/{hatena_id}/{blog_id}/atom/entry", - hatena_id = hatena_id, - blog_id = blog_id) +fn collection_uri(hatena_id: &str, blog_id: &str) -> Result { + let base_url = "https://blog.hatena.ne.jp"; + let url_string = format!("{base_url}/{hatena_id}/{blog_id}/atom/entry", + base_url = base_url, + hatena_id = hatena_id, + blog_id = blog_id); + Url::parse(&url_string) } fn get() { let hatena_id = env::var("HATENA_USERNAME").unwrap(); let blog_id = env::var("HATENA_BLOG_ID").unwrap(); let api_key = env::var("HATENA_API_KEY").unwrap(); - let url = collection_uri(&hatena_id, &blog_id); + let url = collection_uri(&hatena_id, &blog_id).unwrap(); let ssl = NativeTlsClient::new().unwrap(); let connector = HttpsConnector::new(ssl); @@ -29,7 +34,7 @@ fn get() { password: Some(api_key.to_owned()), }; headers.set(Authorization(basic)); - let mut res = client.get(&url).headers(headers).send().unwrap(); + let mut res = client.get(url).headers(headers).send().unwrap(); println!("Response: {}", res.status); println!("Headers:\n{}", res.headers); io::copy(&mut res, &mut io::stdout()).unwrap();