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
extern crate serde;
extern crate serde_json as json;
extern crate serde_urlencoded as urlencoded;

use error::Error;
use hyper::Client;
use hyper::client::RequestBuilder;
use hyper::header::{Authorization, Basic, ContentType, Headers};
use hyper::net::HttpsConnector;
use hyper_openssl::OpensslClient;
use std::io::Read;

pub fn get<T: serde::Deserialize>(path: &str, key: &str) -> Result<T, Error> {
    let client = get_client();
    let url = get_url(path);
    let headers = get_headers(key);
    let request = client.get(&url).headers(headers);
    send(request)
}

pub fn post<T: serde::Deserialize, P: serde::Serialize>(path: &str, key: &str, params: P) -> Result<T, Error> {
    let client = get_client();
    let url = get_url(path);
    let headers = get_headers(key);
    let body = match urlencoded::to_string(&params) {
        Err(err) => { return Err(Error::from_encode(err)); },
        Ok(data) => data,
    };
    let request = client.post(&url).headers(headers).body(&body);
    send(request)
}

pub fn delete<T: serde::Deserialize>(path: &str, key: &str) -> Result<T, Error> {
    let client = get_client();
    let url = get_url(path);
    let headers = get_headers(key);
    let request = client.delete(&url).headers(headers);
    send(request)
}

fn send<T: serde::Deserialize>(request: RequestBuilder) -> Result<T, Error> {
    let mut response = match request.send() {
        Err(err) => { return Err(Error::from_http(err)); },
        Ok(data) => data,
    };

    // Read the body from the response
    let mut body_string = String::with_capacity(4096);
    match response.read_to_string(&mut body_string) {
        Err(err) => { return Err(Error::from_io(err)); },
        Ok(_) => {},
    }

    // Handle the returned status code
    let status_code = response.status_raw().0;
    match status_code {
        200...299 => { /* ok */ },
        // TODO: probably merge these branches...
        400...499 => { return Err(Error::from_status(status_code, body_string)); },
        500...599 => { return Err(Error::from_status(status_code, body_string)); },
        _ => { return Err(Error::from_status(status_code, body_string)); }
    }

    match json::from_str(&body_string) {
        Err(err) => Err(Error::from_decode(err)),
        Ok(data) => Ok(data),
    }
}

fn get_client() -> Client {
    let ssl = OpensslClient::new().unwrap();
    let connector = HttpsConnector::new(ssl);
    Client::with_connector(connector)
}

fn get_url(path: &str) -> String {
    String::from("https://api.stripe.com/v1") + path
}

fn get_headers(key: &str) -> Headers {
    let mut headers = Headers::new();
    headers.set(Authorization(Basic{username: key.to_owned(), password: None}));
    headers.set(ContentType::form_url_encoded());
    headers
}