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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
pub mod comment;
pub mod filter;
pub mod firehose;
pub mod forum;
pub mod image;
pub mod post;
pub mod profile;
pub mod tag;
pub mod topic;
pub use comment::Comment;
pub use filter::Filter;
pub use firehose::Message;
pub use forum::Forum;
pub use image::{Image, Intensities, Representations};
pub use post::Post;
pub use profile::{Award, Link, User};
pub use tag::Tag;
pub use topic::Topic;
pub struct Client {
pub(crate) cli: reqwest::Client,
token: String,
api_base: String,
}
static APP_USER_AGENT: &str = concat!(
"library",
"/",
env!("CARGO_PKG_NAME"),
"/",
env!("CARGO_PKG_VERSION"),
" +https://github.com/Xe/furbooru",
);
impl Client {
pub fn new<T: Into<String>>(user_agent: T, token: T) -> reqwest::Result<Self> {
let cli = reqwest::Client::builder()
.user_agent(&format!("{} {}", user_agent.into(), APP_USER_AGENT))
.build()?;
let cli = Client {
cli: cli.into(),
token: token.into(),
api_base: "https://furbooru.org/".into(),
};
Ok(cli)
}
pub fn derpi<T: Into<String>>(user_agent: T, token: T) -> reqwest::Result<Self> {
let cli = reqwest::Client::builder()
.user_agent(&format!("{} {}", user_agent.into(), APP_USER_AGENT))
.build()?;
let cli = Client {
cli: cli.into(),
token: token.into(),
api_base: "https://derpibooru.org/".into(),
};
Ok(cli)
}
pub fn with_baseurl<T: Into<String>>(
user_agent: T,
token: T,
api_base: T,
) -> reqwest::Result<Self> {
let cli = reqwest::Client::builder()
.user_agent(&format!("{} {}", user_agent.into(), APP_USER_AGENT))
.build()?;
let cli = Client {
cli: cli.into(),
token: token.into(),
api_base: api_base.into(),
};
Ok(cli)
}
fn request(&self, method: reqwest::Method, path: &str) -> reqwest::RequestBuilder {
let url = reqwest::Url::parse(&format!("{}{}", self.api_base, path)).unwrap();
self.cli.request(method, url).query(&[("key", &self.token)])
}
}
#[cfg(test)]
mod tests {
use httptest::{matchers::*, responders::*, Expectation, Server};
#[tokio::test]
async fn basic() {
let _ = pretty_env_logger::try_init();
let server = Server::run();
server.expect(
Expectation::matching(request::method_path("GET", "/foo"))
.respond_with(status_code(200)),
);
let cli =
super::Client::with_baseurl("test", "42069", &format!("{}", server.url("/"))).unwrap();
let resp = cli
.request(reqwest::Method::GET, "foo")
.send()
.await
.unwrap();
assert_eq!(200, resp.status().as_u16());
}
}