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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#![deny(
anonymous_parameters,
clippy::all,
const_err,
illegal_floating_point_literal_pattern,
late_bound_lifetime_arguments,
path_statements,
patterns_in_fns_without_body,
rust_2018_idioms,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
unused_extern_crates
)]
#![warn(
clippy::dbg_macro,
clippy::decimal_literal_representation,
clippy::get_unwrap,
clippy::missing_docs_in_private_items,
clippy::pedantic,
clippy::print_stdout,
clippy::todo,
clippy::unimplemented,
clippy::unwrap_used,
clippy::use_debug,
missing_copy_implementations,
missing_debug_implementations,
unused_qualifications,
variant_size_differences,
clippy::cast_lossless,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss,
clippy::enum_glob_use,
clippy::map_err_ignore,
clippy::missing_errors_doc,
clippy::redundant_pub_crate,
clippy::wildcard_imports
)]
#![allow(
clippy::missing_const_for_fn,
clippy::must_use_candidate,
clippy::cast_precision_loss
)]
use async_trait::async_trait;
use chrono::{DateTime, Duration, Utc};
use log::{info, trace};
use reqwest::Response;
use std::sync::Arc;
use tokio::{
sync::Mutex,
time::{sleep, Duration as TkDuration},
};
pub mod board;
pub mod post;
pub mod thread;
pub mod threadlist;
pub(crate) type Result<T> = anyhow::Result<T>;
#[derive(Debug)]
pub struct Client {
creation_time: DateTime<Utc>,
req_client: reqwest::Client,
pub last_checked: DateTime<Utc>,
}
impl Client {
pub fn new() -> Arc<Mutex<Self>> {
let req_client = reqwest::Client::new();
let last_checked = Utc::now();
let creation_time = last_checked;
info!("constructed chan client.");
Arc::new(Mutex::new(Self {
creation_time,
req_client,
last_checked,
}))
}
pub fn req_client(&self) -> &reqwest::Client {
&self.req_client
}
pub async fn get(&mut self, url: &str) -> Result<Response> {
let current_time = Utc::now().signed_duration_since(self.last_checked);
if (current_time < Duration::seconds(1)) && (self.creation_time != self.last_checked) {
trace!("Requesting responses too fast! Slowing down requests to 1 per second");
sleep(TkDuration::from_secs(1)).await;
}
let resp = self.req_client.get(url).send().await?;
self.last_checked = Utc::now();
trace!(
"Updated the client last checked time: {}",
self.last_checked
);
Ok(resp)
}
}
type Dot4chClient = Arc<Mutex<Client>>;
pub(crate) async fn header(client: &Dot4chClient) -> String {
trace!("Sending request with If-Modified-Since header");
format!(
"{}",
client
.lock()
.await
.last_checked
.format("%a, %d %b %Y %T GMT")
)
}
#[doc(hidden)]
#[async_trait(?Send)]
pub trait IfModifiedSince {
async fn fetch(
client: &Dot4chClient,
url: &str,
header: &str,
) -> std::result::Result<Response, reqwest::Error>;
}
#[async_trait(?Send)]
pub trait Update {
type Output;
async fn update(mut self) -> Result<Self::Output>;
async fn refresh_time(&mut self) -> Result<()>;
async fn fetch_status(mut self, response: Response) -> Result<Self::Output>;
async fn into_upper(self, response: Response) -> Result<Self::Output>;
}
#[doc(hidden)]
fn default<T: Default>() -> T {
Default::default()
}