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
use hyper;
use hyper::client::RedirectPolicy;
use hyper::client::response::Response;
use hyper::header::{Cookie, ContentType, SetCookie};
use url::form_urlencoded;
use error::Error;
#[derive(Debug)]
pub struct Client {
cookie: Cookie,
origin: String,
client: hyper::Client,
}
impl Client {
pub fn new<S: Into<String>>(origin: S) -> Self {
let mut hyper_client = hyper::Client::new();
hyper_client.set_redirect_policy(RedirectPolicy::FollowNone);
Client {
origin: origin.into(),
cookie: Cookie(Vec::new()),
client: hyper_client,
}
}
pub fn with_client<S: Into<String>>(origin: S, mut hyper_client: hyper::Client) -> Self {
hyper_client.set_redirect_policy(RedirectPolicy::FollowNone);
Client {
origin: origin.into(),
cookie: Cookie(Vec::new()),
client: hyper_client,
}
}
pub fn login(&mut self, username: &str, password: &str) -> Result<Response, Error> {
let body = form_urlencoded::serialize(vec![("credential_0", username),
("credential_1", password),
("destination", "/")]);
let s = format!("{}/LOGIN", self.origin);
let res = match self.client
.post(&s)
.body(&body)
.header(ContentType::form_url_encoded())
.send() {
Ok(x) => x,
Err(e) => {
return Err(Error::Hyper(e));
}
};
match res.headers.get::<SetCookie>() {
Some(header) => self.cookie.0 = header.0.to_owned(),
None => {
return Err(Error::Authentication);
}
}
Ok(res)
}
pub fn get(&self, path: &str, query: Option<&str>) -> Result<Response, Error> {
let s = match query {
Some(query) => format!("{}/{}?{}", self.origin, path, query),
None => format!("{}/{}", self.origin, path),
};
match self.client
.get(&s)
.header(self.cookie.clone())
.send() {
Ok(x) => Ok(x),
Err(e) => Err(Error::Hyper(e)),
}
}
}