From 3a022c1bc9b8b92df0d2ef9977a7df5c1af1036b Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Wed, 8 Aug 2018 14:01:42 +0800 Subject: Use reqwest::Url to generate params --- src/content.rs | 51 +++++++++++++++++++++++++-------------------------- src/main.rs | 2 +- 2 files changed, 26 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/content.rs b/src/content.rs index 30d8f22..ed1d8c5 100644 --- a/src/content.rs +++ b/src/content.rs @@ -9,37 +9,36 @@ use cursive::theme::Effect; use cursive::utils::markup::StyledString; use cursive::views::Dialog; use cursive::Cursive; +use reqwest::Url; use serde_json::Value; use CONFIGURATION; -pub fn query_url_gen(title: &str) -> String { - // query config - let mut url = CONFIGURATION.wiki_url.clone(); - url.push_str("/w/api.php?"); - url.push_str("action=query&"); - url.push_str("format=json&"); - url.push_str("prop=extracts%7Clinks&"); - url.push_str("indexpageids=1&"); - url.push_str("titles="); - url.push_str(&urlencoding::encode(&title.replace(" ", "_"))); - url.push_str("&"); - url.push_str("redirects=1&"); - url.push_str("pllimit=100&"); - url.push_str("explaintext=1"); - url +pub fn query_url_gen(title: &str) -> Url { + Url::parse_with_params( + &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), + &[ + ("action", "query"), + ("format", "json"), + ("prop", "extracts|links"), + ("indexpageids", "1"), + ("titles", &urlencoding::encode(&title.replace(" ", "_"))), + ("redirects", "1"), + ("pllimit", "100"), + ("explaintext", "1"), + ], + ).unwrap() } -pub fn search_url_gen(search: &str) -> String { - // search config - let mut url = CONFIGURATION.wiki_url.clone(); - url.push_str("/w/api.php?"); - url.push_str("action=opensearch&"); - url.push_str("format=json&"); - url.push_str("search="); - url.push_str(&urlencoding::encode(&search)); - url.push_str("&"); - url.push_str("limit=20"); - url +pub fn search_url_gen(search: &str) -> Url { + Url::parse_with_params( + &(CONFIGURATION.wiki_url.clone() + "/w/api.php"), + &[ + ("action", "opensearch"), + ("format", "json"), + ("search", &urlencoding::encode(search)), + ("limit", "20"), + ], + ).unwrap() } pub fn get_extract(v: &Value) -> Result { diff --git a/src/main.rs b/src/main.rs index c6249c4..5057433 100644 --- a/src/main.rs +++ b/src/main.rs @@ -124,7 +124,7 @@ fn on_submit(s: &mut Cursive, name: &str) { let mut extract = String::new(); let mut link_vec: Vec = vec![]; - let mut res = reqwest::get(&url).unwrap(); + let mut res = reqwest::get(url).unwrap(); let v: Value = res.json().expect("Failed to parse json"); match get_extract(&v) { -- cgit v1.2.3