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
use std::fmt;
use ureq;
#[derive(Debug, Clone)]
pub struct ErrorHTTP {
msg: String,
}
impl fmt::Display for ErrorHTTP {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.msg)
}
}
pub fn get_url(url: &str) -> Result<String, ErrorHTTP> {
let req = ureq::get(url);
match req.call() {
Ok(resp) => {
match resp.into_string() {
Ok(string) => return Ok(string),
Err(_) => return Err(ErrorHTTP{msg: String::from("could not parse response into string")}),
}
},
Err(_) => return Err(ErrorHTTP{msg: String::from("unable to make this http request")}),
};
}
#[test]
fn str_vs_string() {
let url: String = String::from("https://jsonplaceholder.typicode.com/posts/1");
let v1 = get_url(&"https://jsonplaceholder.typicode.com/posts/1").unwrap();
let v2 = get_url(&url).unwrap();
assert_eq!(v1, v2);
assert_eq!(v1.len() > 20, true);
}