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
extern crate reqwest;
extern crate serde;
extern crate serde_json;
extern crate url;
use reqwest::blocking::Client as ReqwestClient;
use reqwest::blocking::Response;
use reqwest::Error;
use std::marker::PhantomData;
use url::Url;
macro_rules! add_param {
($f:ident, $p:ident, $t:ty) => {
pub fn $f(&'a mut self, v: $t) -> &'a mut Self {
self.url.query_pairs_mut().append_pair(stringify!($p), &*v.to_string());
self
}
};
}
pub mod error;
pub mod user;
pub struct RequestBuilder<'a, T: 'a> {
client: &'a mut Client,
url: Url,
phantom: PhantomData<&'a T>,
}
pub struct Client {
api_key: String,
http_client: ReqwestClient,
}
impl Client {
pub fn new(api_key: &str) -> Client {
Client {
api_key: api_key.to_owned(),
http_client: ReqwestClient::new(),
}
}
fn build_url(&self, params: Vec<(&str, &str)>) -> Url {
let mut url = Url::parse("http://ws.audioscrobbler.com/2.0/").unwrap();
url.query_pairs_mut().clear().append_pair("api_key", &*self.api_key).append_pair("format", "json");
for (key, value) in params {
url.query_pairs_mut().append_pair(key, value);
}
url
}
fn request(&mut self, url: &Url) -> Result<Response, Error> {
self.http_client.get(url.as_str()).send()
}
}
#[cfg(test)]
mod tests {
use super::Client;
use std::env;
pub fn make_client() -> Client {
let api_key = env::var("API_KEY").unwrap();
Client::new(&api_key)
}
#[test]
fn test_build_url() {
let api_key = env::var("API_KEY").unwrap();
let user = env::var("USER").unwrap();
let client = make_client();
let url = client.build_url(vec![("method", "user.getrecenttracks"), ("user", &user)]);
assert_eq!(
url.as_str(),
&format!("http://ws.audioscrobbler.com/2.0/?api_key={}&format=json&method=user.getrecenttracks&user={}", api_key, user)
);
let url = client.build_url(vec![]);
assert_eq!(url.as_str(), &format!("http://ws.audioscrobbler.com/2.0/?api_key={}&format=json", api_key));
}
}