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
use crate::response::{Error, extract_api_response};
macro_rules! endpoint {
() => {"https://www.fimfiction.net/api/v2"};
($name:literal) => {concat!(endpoint!(), $name)};
}
pub const BASE_URL: &str = endpoint!();
#[derive(Clone, Debug)]
pub struct Client {
bearer_token: String,
client: reqwest::Client,
}
impl Client {
pub async fn new(client_id: impl AsRef<str>, client_secret: impl AsRef<str>) -> Result<Self, Error> {
Self::with_client(client_id, client_secret, reqwest::Client::default()).await
}
pub async fn with_client(client_id: impl AsRef<str>, client_secret: impl AsRef<str>, http: reqwest::Client) -> Result<Self, Error> {
let form = [
("client_id", client_id.as_ref()),
("client_secret", client_secret.as_ref()),
("grant_type", "client_credentials")
];
let res = http.post(endpoint!("/token"))
.form(&form)
.send()
.await?;
let value: serde_json::Value = extract_api_response(res).await?;
Ok(Client {
bearer_token: format!("Bearer {}", value.get("access_token").unwrap().as_str().unwrap()),
client: http,
})
}
pub fn from_token(tok: impl Into<String>) -> Self {
Client {
bearer_token: tok.into(),
client: reqwest::Client::default(),
}
}
pub fn bearer_token(&self) -> &str {
&self.bearer_token
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test::init_env;
#[tokio::test]
pub async fn grab_token() {
init_env();
let run_new_token = std::env::var("GET_NEW_TOKEN").is_ok();
if !run_new_token {
println!("Did not run test because GET_NEW_TOKEN did not exist.");
return;
}
let client_id = std::env::var("FF_CLIENT_ID").unwrap();
let client_secret = std::env::var("FF_CLIENT_SECRET").unwrap();
let _ = Client::new(client_id, client_secret).await.unwrap();
}
}