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
//! HTTP helpers.

use std::io::Read;
use std::result;
use hyper::Client;
use hyper::header::ContentType;
use hyper::mime::{Mime, TopLevel, SubLevel, Attr, Value};
pub use hyper::status::StatusCode;
pub use hyper::Error as HttpError;

header! { (SoapAction, "SOAPAction") => [String] }

/// Simplified HTTP response representation.
#[derive(Debug)]
pub struct Response {
    pub status: StatusCode,
    pub body: String,
}

/// Perform a GET request to specified URL.
pub fn get(url: &str) -> Result<Response> {
    let client = Client::new();
    let mut response = try!(client.get(url).send());

    let mut body = String::new();
    try!(response.read_to_string(&mut body));

    Ok(Response {
        status: response.status,
        body: body,
    })
}

/// Perform a SOAP action to specified URL.
pub fn soap_action(url: &str, action: &str, xml: &str) -> Result<Response> {
    let client = Client::new();
    let mut response = try!(
        client.post(url)
            .header(ContentType(Mime(TopLevel::Text, SubLevel::Xml,
                     vec![(Attr::Charset, Value::Utf8)])))
            .header(SoapAction(action.into()))
            .body(xml)
            .send()
    );

    let mut body = String::new();
    try!(response.read_to_string(&mut body));

    Ok(Response {
        status: response.status,
        body: body,
    })
}

pub type Result<T> = result::Result<T, HttpError>;