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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use reqwest::{Client, Method, RequestBuilder, Url, IntoUrl};
use reqwest::header::{Authorization, UserAgent};
use url::ParseError;
use std::sync::Arc;
pub use reqwest::Body;
#[derive(Clone)]
pub enum ApiAuth {
ApiKey(String),
None,
}
#[derive(Clone)]
pub struct HttpClient {
pub base_url: ::std::result::Result<Url, ParseError>,
api_auth: ApiAuth,
inner_client: Arc<Client>,
user_agent: String,
}
impl HttpClient {
pub fn new<U: IntoUrl>(api_auth: ApiAuth, base_url: U) -> HttpClient {
HttpClient {
api_auth: api_auth,
base_url: base_url.into_url(),
inner_client: Arc::new(Client::new().expect("Failed to init client")),
user_agent: format!("algorithmia-rust/{} (Rust {}",
option_env!("CARGO_PKG_VERSION").unwrap_or("unknown"),
::version::RUSTC_VERSION),
}
}
pub fn get(&self, url: Url) -> RequestBuilder {
self.build_request(Method::Get, url)
}
pub fn head(&self, url: Url) -> RequestBuilder {
self.build_request(Method::Head, url)
}
pub fn post(&self, url: Url) -> RequestBuilder {
self.build_request(Method::Post, url)
}
pub fn put(&self, url: Url) -> RequestBuilder {
self.build_request(Method::Put, url)
}
pub fn delete(&self, url: Url) -> RequestBuilder {
self.build_request(Method::Delete, url)
}
fn build_request(&self, verb: Method, url: Url) -> RequestBuilder {
let mut req = self.inner_client.request(verb, url.clone());
req = req.header(UserAgent(self.user_agent.clone()));
if let ApiAuth::ApiKey(ref api_key) = self.api_auth {
req = req.header(Authorization(format!("Simple {}", api_key)))
}
req
}
}
impl<'a> From<&'a str> for ApiAuth {
fn from(api_key: &'a str) -> Self {
match api_key.len() {
0 => ApiAuth::None,
_ => ApiAuth::ApiKey(api_key.into()),
}
}
}
impl From<String> for ApiAuth {
fn from(api_key: String) -> Self {
match api_key.len() {
0 => ApiAuth::None,
_ => ApiAuth::ApiKey(api_key),
}
}
}
impl From<()> for ApiAuth {
fn from(_: ()) -> Self {
ApiAuth::None
}
}