mediawiki_rest_api 0.2.1

A Rust client for the MediaWiki REST API.
Documentation
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use crate::{error::RestApiError, prelude::*};
use serde_json::{Value, from_value, json};
use std::collections::HashMap;
use urlencoding::encode;

#[derive(Clone, Debug)]
pub struct Page {
    title: String,
}

impl Page {
    /// Creates a new page object with the given title.
    pub fn new<S: Into<String>>(title: S) -> Self {
        Self {
            title: title.into(),
        }
    }

    /// Retrieves basic page information and wikitext.
    pub async fn get(
        &self,
        api: &RestApi,
        follow_redirect: bool,
    ) -> Result<(PageInfo, String), RestApiError> {
        let path = format!("/page/{}", encode(&self.title));
        let mut params = HashMap::new();
        params.insert("redirect".to_string(), follow_redirect.to_string());
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let j: Value = response.json().await?;
        let wikitext = j["source"]
            .as_str()
            .ok_or(RestApiError::MissingResults)?
            .to_string();
        let ret = from_value::<PageInfo>(j)?;
        Ok((ret, wikitext))
    }

    /// Retrieves basic page information and the URL for HTML retrieval.
    pub async fn get_bare(
        &self,
        api: &RestApi,
        follow_redirect: bool,
    ) -> Result<(PageInfo, String), RestApiError> {
        let path = format!("/page/{}/bare", encode(&self.title));
        let mut params = HashMap::new();
        params.insert("redirect".to_string(), follow_redirect.to_string());
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let j: Value = response.json().await?;
        let html_url = j["html_url"]
            .as_str()
            .ok_or(RestApiError::MissingResults)?
            .to_string();
        let ret = from_value::<PageInfo>(j)?;
        Ok((ret, html_url))
    }

    /// Retrieves the HTML for the page.
    pub async fn get_html(
        &self,
        api: &RestApi,
        follow_redirect: bool,
        stash: bool,
        flavor: HtmlFlavor,
    ) -> Result<String, RestApiError> {
        let path = format!("/page/{}/html", encode(&self.title));
        let mut params = HashMap::new();
        params.insert("redirect".to_string(), follow_redirect.to_string());
        params.insert("stash".to_string(), stash.to_string());
        params.insert("flavor".to_string(), flavor.to_string());
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret = response.text().await?;
        Ok(ret)
    }

    /// Retrieves basic page information and the HTML for the page.
    pub async fn get_with_html(
        &self,
        api: &RestApi,
        follow_redirect: bool,
        stash: bool,
        flavor: HtmlFlavor,
    ) -> Result<(PageInfo, String), RestApiError> {
        let path = format!("/page/{}/with_html", encode(&self.title));
        let mut params = HashMap::new();
        params.insert("redirect".to_string(), follow_redirect.to_string());
        params.insert("stash".to_string(), stash.to_string());
        params.insert("flavor".to_string(), flavor.to_string());
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let j: Value = response.json().await?;
        let html = j["html"]
            .as_str()
            .ok_or(RestApiError::MissingResults)?
            .to_string();
        let ret = from_value::<PageInfo>(j)?;
        Ok((ret, html))
    }

    /// Retrieves the language links.
    pub async fn get_links_language(
        &self,
        api: &RestApi,
    ) -> Result<Vec<LanguageLink>, RestApiError> {
        let path = format!("/page/{}/links/language", encode(&self.title));
        let params = HashMap::new();
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret: Vec<LanguageLink> = response.json().await?;
        Ok(ret)
    }

    /// Retrieves the used media.
    pub async fn get_links_media(&self, api: &RestApi) -> Result<MediaResult, RestApiError> {
        let path = format!("/page/{}/links/media", encode(&self.title));
        let params = HashMap::new();
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret: MediaResult = response.json().await?;
        Ok(ret)
    }

    /// Retrieves lint data for the page.
    pub async fn get_lint(
        &self,
        api: &RestApi,
        follow_redirect: bool,
    ) -> Result<Vec<Lint>, RestApiError> {
        let path = format!("/page/{}/lint", encode(&self.title));
        let mut params = HashMap::new();
        params.insert("redirect".to_string(), follow_redirect.to_string());
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret: Vec<Lint> = response.json().await?;
        Ok(ret)
    }

    /// Retrieves history data for the page.
    pub async fn get_history(
        &self,
        api: &RestApi,
        filter: Option<Filter>,
        older_than: Option<usize>,
        newer_than: Option<usize>,
    ) -> Result<History, RestApiError> {
        let path = format!("/page/{}/history", encode(&self.title));
        let mut params = HashMap::new();
        if let Some(older_than) = older_than {
            params.insert("older_than".to_string(), older_than.to_string());
        }
        if let Some(newer_than) = newer_than {
            params.insert("newer_than".to_string(), newer_than.to_string());
        }
        if let Some(filter) = filter {
            params.insert("filter".to_string(), filter.to_string());
        }
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret: History = response.json().await?;
        Ok(ret)
    }

    /// Retrieves history counts for the page.
    pub async fn get_history_counts(
        &self,
        api: &RestApi,
        filter: HistoryFilterExtended,
        from: Option<usize>,
        to: Option<usize>,
    ) -> Result<HistoryCounts, RestApiError> {
        let path = format!("/page/{}/history/counts/{filter}", encode(&self.title));
        let mut params = HashMap::new();
        if let Some(from) = from {
            params.insert("from".to_string(), from.to_string());
        }
        if let Some(to) = to {
            params.insert("to".to_string(), to.to_string());
        }
        let request = api
            .build_request(path, params, reqwest::Method::GET)
            .await?
            .build()?;
        let response = api.execute(request).await?;
        let ret: HistoryCounts = response.json().await?;
        Ok(ret)
    }

    /// Replaces the contents of the page.
    pub async fn edit(
        &self,
        api: &RestApi,
        rt: &RevisionTimestamp,
        source: &str,
        comment: &str,
    ) -> Result<(PageInfo, String), RestApiError> {
        let edit_token = api
            .get_edit_token()
            .await
            .ok_or(RestApiError::AccessTokenRequired)?;
        let path = format!("/page/{}", encode(&self.title));
        let payload = json!({
            "source": source,
            "comment": comment,
            "token": edit_token,
            "latest": rt,
            "content_model": "wikitext"
        });
        let payload = serde_json::to_string(&payload)?;
        let params = HashMap::new();
        let request = api
            .build_request(path, params, reqwest::Method::PUT)
            .await?
            .body(payload)
            .build()?;
        let response = api.execute(request).await?;
        let j: Value = response.json().await?;
        let wikitext = j["source"]
            .as_str()
            .ok_or(RestApiError::MissingResults)?
            .to_string();
        let ret = from_value::<PageInfo>(j)?;
        Ok((ret, wikitext))
    }

    /// Creates the page.
    pub async fn create(
        &self,
        api: &RestApi,
        source: &str,
        comment: &str,
    ) -> Result<(PageInfo, String), RestApiError> {
        let edit_token = api
            .get_edit_token()
            .await
            .ok_or(RestApiError::AccessTokenRequired)?;
        let path = "/page";
        let payload = json!({
            "source": source,
            "comment": comment,
            "title": self.title,
            "token": edit_token,
            "content_model": "wikitext"
        });
        let payload = serde_json::to_string(&payload)?;
        let params = HashMap::new();
        let request = api
            .build_request(path, params, reqwest::Method::POST)
            .await?
            .body(payload)
            .build()?;
        let response = api.execute(request).await?;
        let j: Value = response.json().await?;
        let wikitext = j["source"]
            .as_str()
            .ok_or(RestApiError::MissingResults)?
            .to_string();
        let ret = from_value::<PageInfo>(j)?;
        Ok((ret, wikitext))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn get_mock_api(test_file: &str, test_path: &str) -> (RestApi, MockServer) {
        let mock_path = format!("w/rest.php/v1{test_path}");
        let mock_server = MockServer::start().await;

        let test_text: String =
            std::fs::read_to_string(format!("test_data/{test_file}")).expect("Test file missing");
        if test_file.ends_with(".json") {
            let json: Value = serde_json::from_str(&test_text).expect("Failed to parse JSON");
            Mock::given(method("GET"))
                .and(path(&mock_path))
                .respond_with(ResponseTemplate::new(200).set_body_json(&json))
                .mount(&mock_server)
                .await;
        } else {
            Mock::given(method("GET"))
                .and(path(&mock_path))
                .respond_with(ResponseTemplate::new(200).set_body_string(&test_text))
                .mount(&mock_server)
                .await;
        }

        let api = RestApi::builder(&(mock_server.uri() + "/w/rest.php"))
            .expect("Failed to create RestApi")
            .with_access_token("foobar")
            .build();
        (api, mock_server)
    }

    #[tokio::test]
    async fn test_get() {
        let (api, _mock_server) = get_mock_api(
            "page_get.json",
            &format!("/page/{}", encode("Rust (programming language)")),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let (page_info, wikitext) = page
            .get(&api, false)
            .await
            .expect("Failed to get page content");
        assert_eq!(page_info.id, 29414838);
        assert!(wikitext.contains("Mozilla sponsorship"));
    }

    #[tokio::test]
    async fn test_get_bare() {
        let (api, _mock_server) = get_mock_api(
            "page_get_bare.json",
            &format!("/page/{}/bare", encode("Rust (programming language)")),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let (page_info, html_url) = page
            .get_bare(&api, false)
            .await
            .expect("Failed to get page content");
        assert_eq!(page_info.id, 29414838);
        assert_eq!(
            html_url,
            "https://en.wikipedia.org/w/rest.php/v1/page/Rust%20%28programming%20language%29/html"
        );
    }

    #[tokio::test]
    async fn test_get_html() {
        let (api, _mock_server) = get_mock_api(
            "page_get_html.html",
            &format!("/page/{}/html", encode("Rust (programming language)")),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let result = page
            .get_html(&api, false, false, HtmlFlavor::View)
            .await
            .expect("Failed to get page content");
        assert!(result.contains("<title>Rust (programming language)</title>"));
    }

    #[tokio::test]
    async fn test_get_with_html() {
        let (api, _mock_server) = get_mock_api(
            "page_get_with_html.json",
            &format!("/page/{}/with_html", encode("Rust (programming language)")),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let (page_info, html) = page
            .get_with_html(&api, false, false, HtmlFlavor::View)
            .await
            .expect("Failed to get page content");
        assert_eq!(page_info.id, 29414838);
        assert!(html.contains("<title>Rust (programming language)</title>"));
    }

    #[tokio::test]
    async fn test_get_links_language() {
        let (api, _mock_server) = get_mock_api(
            "page_links_language.json",
            &format!(
                "/page/{}/links/language",
                encode("Rust (programming language)")
            ),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let language_links = page
            .get_links_language(&api)
            .await
            .expect("Failed to get page content");
        assert!(
            language_links.iter().any(
                |link| link.code == "it" && link.title == "Rust (linguaggio di programmazione)"
            )
        );
    }

    #[tokio::test]
    async fn test_get_links_media() {
        let (api, _mock_server) =
            get_mock_api("page_links_media.json", "/page/Cambridge/links/media").await;
        let page = Page::new("Cambridge");
        let media_links = page
            .get_links_media(&api)
            .await
            .expect("Failed to get page content");
        assert!(
            media_links
                .files
                .iter()
                .any(|file| file.title == "Flag of England.svg")
        );
    }

    #[tokio::test]
    async fn test_get_lint() {
        let (api, _mock_server) = get_mock_api("page_lint.json", "/page/Cambridge/lint").await;
        let page = Page::new("Cambridge");
        let lints = page
            .get_lint(&api, false)
            .await
            .expect("Failed to get page content");
        assert_eq!(lints.len(), 9);
        assert!(lints.iter().any(|lint| lint.type_name == "duplicate-ids"
            && lint.template_info.as_ref().unwrap().name == "Template:Cite_book"));
    }

    #[tokio::test]
    async fn test_get_history() {
        let (api, _mock_server) = get_mock_api(
            "page_history.json",
            &format!("/page/{}/history", encode("Rust (programming language)")),
        )
        .await;
        let page = Page::new("Rust (programming language)");
        let history = page
            .get_history(&api, None, None, None)
            .await
            .expect("Failed to get page content");
        assert_eq!(history.revisions.len(), 20);
    }

    #[tokio::test]
    async fn test_get_history_counts() {
        let (api, _mock_server) = get_mock_api(
            "page_history_counts.json",
            "/page/Cambridge/history/counts/anonymous",
        )
        .await;
        let page = Page::new("Cambridge");
        let hc = page
            .get_history_counts(&api, HistoryFilterExtended::Anonymous, None, None)
            .await
            .expect("Failed to get page content");
        assert_eq!(hc.count, 1289);
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_edit_enwiki() {
        let page_title = "User:Magnus Manske/mediawiki rest api test1";
        let page = Page::new(page_title);

        let mock_path = format!("w/rest.php/v1/page/{}", encode(page_title));
        let mock_server = MockServer::start().await;

        let test_text: String =
            std::fs::read_to_string("test_data/page_edit.json").expect("Test file missing");
        let json: Value = serde_json::from_str(&test_text).expect("Failed to parse JSON");
        Mock::given(method("PUT"))
            .and(path(&mock_path))
            .respond_with(ResponseTemplate::new(200).set_body_json(&json))
            .mount(&mock_server)
            .await;

        let api_url = mock_server.uri() + "/w/rest.php";
        let api = RestApi::builder(&api_url)
            .expect("Failed to create RestApi")
            .with_access_token("foobar")
            .build();

        // Dummy
        let latest = RevisionTimestamp {
            id: 0,
            timestamp: String::new(),
        };

        let source = "test123";
        let comments = "test edit";
        let (page_info, wikitext) = page
            .edit(&api, &latest, source, comments)
            .await
            .expect("Failed to edit page");
        assert_eq!(page_info.id, 81442549);
        assert_eq!(wikitext, source);
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_create_enwiki() {
        let page_title = "User:Magnus Manske/mediawiki rest api test2";
        let page = Page::new(page_title);

        let mock_path = "w/rest.php/v1/page";
        let mock_server = MockServer::start().await;

        let test_text: String =
            std::fs::read_to_string("test_data/page_create.json").expect("Test file missing");
        let json: Value = serde_json::from_str(&test_text).expect("Failed to parse JSON");
        Mock::given(method("POST"))
            .and(path(mock_path))
            .respond_with(ResponseTemplate::new(200).set_body_json(&json))
            .mount(&mock_server)
            .await;

        let api_url = mock_server.uri() + "/w/rest.php";
        let api = RestApi::builder(&api_url)
            .expect("Failed to create RestApi")
            .with_access_token("foobar")
            .build();

        let source = "test123";
        let comments = "test edit";
        let (page_info, wikitext) = page
            .create(&api, source, comments)
            .await
            .expect("Failed to edit page");
        assert_eq!(page_info.id, 81447676);
        assert_eq!(wikitext, source);
    }
}