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
use hyper::{Client, Url};
use hyper::status::StatusCode;
use hyper::header::{ContentType, Headers, Authorization, Basic};
//use hyper::client::IntoUrl;
use std::io::Read;
use errors::{Error};

fn get_headers (url: Url) -> Headers {
    let mut headers = Headers::new();

    headers.set(ContentType::json());
    headers.set(
       Authorization(
           Basic {
               username: url.username().to_string(),
               password: match url.password() {
                   None => None,
                   Some(password) => Some(password.to_string())
               }
           }
       )
    );

    headers
}

#[doc(hidden)]
pub fn post (url: &Url, body: &str) -> Result<String, Error> {
    let headers = get_headers(url.clone());
    let client = Client::new();
    let mut res = try!(client
                    .post(url.clone())
                    .headers(headers)
                    .body(body)
                    .send());

    let mut resp = String::new();
    res.read_to_string(&mut resp).unwrap();
    println!("POST QUERY {} {}",url.to_string(), resp);
    println!("{}", body);

    match res.status {
        StatusCode::Created | StatusCode::Ok => Ok(resp),
        _ => Err(Error::from_couch(&resp))
    }
}

#[doc(hidden)]
pub fn get (url :&Url) -> Result<String, Error> {
    let headers = get_headers(url.clone());
    let client = Client::new();
    let mut res = try!(client
                    .get(url.clone())
                    .headers(headers)
                    .send());

    let mut resp = String::new();
    try!(res.read_to_string(&mut resp));

    println!("GET {}", resp);
    println!("{}", res.status);
    if res.status == StatusCode::Ok {
        Ok(resp)
    } else {
        Err(Error::from_couch(&resp))
    }
}