Skip to content
This repository has been archived by the owner on May 27, 2023. It is now read-only.

Commit

Permalink
use url
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Apr 20, 2017
1 parent d60379e commit 3834c59
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main.rs
@@ -1,24 +1,29 @@
extern crate hyper;
extern crate hyper_native_tls;
extern crate url;

use std::env;
use std::io;
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<Url, ParseError> {
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);
Expand All @@ -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();
Expand Down

0 comments on commit 3834c59

Please sign in to comment.