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
#[macro_use]
extern crate rocket;
use mwapi::{Client, Error};
use mwapi_responses::prelude::*;
use rocket::http::Status;
const USER_AGENT: &str = toolforge::user_agent!("newusers");
#[query(
list = "logevents",
leaction = "newusers/create",
leprop = "user",
lelimit = "100"
)]
struct Response;
#[get("/?<project>")]
async fn index(project: Option<String>) -> Result<String, (Status, String)> {
build_index(project)
.await
.map_err(|err| (Status::InternalServerError, format!("Error: {}", err)))
}
async fn build_index(project: Option<String>) -> Result<String, Error> {
let api = Client::builder(&format!(
"https://{}.org/w/api.php",
project.unwrap_or_else(|| "en.wikisource".into())
))
.set_user_agent(USER_AGENT)
.build()
.await?;
let resp: Response = api.get(Response::params()).await?;
let users: Vec<_> = resp
.items()
.iter()
.map(|item| item.user.to_string())
.collect();
Ok(users.join("\n"))
}
#[get("/healthz")]
fn healthz() -> &'static str {
"OK"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index, healthz])
}
#[cfg(test)]
mod test {
use super::rocket;
use rocket::http::Status;
#[test]
fn test_index() {
use rocket::local::blocking::Client;
let client = Client::tracked(rocket()).unwrap();
let resp = client.get("/").dispatch();
let resp2 = client.get("/?project=en.wikipedia").dispatch();
assert_eq!(resp.status(), Status::Ok);
assert_eq!(resp2.status(), Status::Ok);
let string_resp = resp.into_string().unwrap();
let string_resp2 = resp2.into_string().unwrap();
assert_ne!(string_resp, string_resp2);
let split_resp: Vec<&str> = string_resp.split("\n").collect();
assert_eq!(split_resp.len(), 100);
let split_resp2: Vec<&str> = string_resp2.split("\n").collect();
assert_eq!(split_resp2.len(), 100);
}
}