1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#![cfg_attr(feature = "serde_macros", feature(plugin, custom_derive))]
#![cfg_attr(feature = "serde_macros", plugin(serde_macros))]
extern crate serde;
extern crate serde_json;
extern crate hyper;
#[macro_use]
extern crate log;
extern crate url;
#[cfg(feature = "serde_macros")]
include!("serde_types.in.rs");
#[cfg(feature = "serde_codegen")]
include!(concat!(env!("OUT_DIR"), "/serde_types.rs"));
use hyper::Client;
use std::io::Read;
use url::percent_encoding;
pub struct Quack;
const LIB_VERSION: &'static str = env!("CARGO_PKG_VERSION");
const LIB_NAME: &'static str = env!("CARGO_PKG_NAME");
const BASE_URL: &'static str = "https://api.duckduckgo.com/";
impl Quack {
#[allow(dead_code)]
#[allow(unused_variables)]
pub fn new(query: &str) -> Option<InstantAnswer> {
let client = Client::new();
let url = build_url(&query);
debug!("url: {}", &url);
let mut res = client.get(&url)
.send()
.unwrap();
debug!("response status code: {}", res.status);
if res.status != hyper::status::StatusCode::Ok {
panic!("Received status code: {}", res.status);
}
let mut buffer = String::new();
res.read_to_string(&mut buffer).expect("no body");
debug!("buffer: {}", buffer);
let raw_json: serde_json::Value = serde_json::from_str(&buffer).unwrap();
let check_type_value = raw_json.as_object().unwrap().get("Type").unwrap();
let check_type = format!("{}", check_type_value).replace("\"", "");
debug!("check_type: empty:{} size:{} data:{:?}",
check_type.is_empty(),
check_type.len(),
check_type);
if check_type == "D" || check_type.is_empty() {
return None;
}
Some(serde_json::from_str(&buffer).unwrap())
}
}
fn build_url(query: &str) -> String {
let mut url = String::from(BASE_URL);
url.push_str("?q=");
let query_encoded = format!("{}",
percent_encoding::percent_encode(&query.as_bytes(),
percent_encoding::QUERY_ENCODE_SET));
url.push_str(&query_encoded);
url.push_str("&format=json");
url.push_str("&pretty=0");
url.push_str("&no_redirect=1");
url.push_str("&skip_disambig=1");
url.push_str("&no_html=1");
url.push_str("&t=");
url.push_str(LIB_NAME);
url.push_str("-v");
url.push_str(LIB_VERSION);
url.to_string()
}
#[cfg(test)]
fn harness(test_file: &str) -> InstantAnswer {
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push("tests");
path.push("fixtures");
path.push(&test_file);
let file_path = path.as_os_str();
use std::fs::File;
use std::io::Read;
let mut file = File::open(&file_path).unwrap();
let mut file_string = String::new();
file.read_to_string(&mut file_string).unwrap();
serde_json::from_str(&file_string).unwrap()
}
#[test]
fn should_be_type_exclusive() {
let result = harness("bang_no_redirect.json");
assert_eq!("E", result.response_type);
}
#[test]
fn should_be_type_article() {
let result = harness("topic_summaries.json");
assert_eq!("A", result.response_type);
}
#[test]
fn should_be_type_category() {
let result = harness("categories.json");
assert_eq!("C", result.response_type);
}