wikibase_rest_api 0.1.14

A Rust client for the Wikibase 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
use crate::{
    aliases::Aliases,
    aliases_in_language::AliasesInLanguage,
    descriptions::Descriptions,
    entity::{Entity, EntityType},
    entity_patch::EntityPatch,
    labels::Labels,
    sitelinks::Sitelinks,
    statements::Statements,
    EntityId, FromJson, HeaderInfo, HttpMisc, Patch, RestApi, RestApiError,
};
use derive_where::DeriveWhere;
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde_json::Value;

#[derive(DeriveWhere, Debug, Clone, Default)]
#[derive_where(PartialEq)]
pub struct Item {
    id: EntityId,
    labels: Labels,
    descriptions: Descriptions,
    aliases: Aliases,
    sitelinks: Sitelinks,
    statements: Statements,
    #[derive_where(skip)]
    header_info: HeaderInfo,
}

impl HttpMisc for Item {
    fn get_my_rest_api_path(&self, id: &EntityId) -> Result<String, RestApiError> {
        Ok(format!("/entities/{}/{id}", id.group()?))
    }
}

impl Entity for Item {
    fn id(&self) -> EntityId {
        self.id.to_owned()
    }

    fn set_id(&mut self, id: EntityId) {
        self.id = id;
    }

    fn from_json_header_info(j: Value, header_info: HeaderInfo) -> Result<Self, RestApiError> {
        let id = j["id"]
            .as_str()
            .ok_or(RestApiError::MissingOrInvalidField {
                field: "id".into(),
                j: j.to_owned(),
            })?
            .to_string();
        Ok(Self {
            id: EntityId::Item(id),
            labels: Labels::from_json(&j["labels"])?,
            descriptions: Descriptions::from_json(&j["descriptions"])?,
            aliases: Aliases::from_json(&j["aliases"])?,
            sitelinks: Sitelinks::from_json(&j["sitelinks"])?,
            statements: Statements::from_json(&j["statements"])?,
            header_info,
        })
    }

    async fn post(&self, api: &RestApi) -> Result<Self, RestApiError> {
        self.post_with_type(EntityType::Item, api).await
    }
}

impl Serialize for Item {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // #lizard forgives the complexity
        let mut fields = 5;
        if self.id.is_some() {
            fields += 1;
        }
        if self.labels.is_empty() {
            fields -= 1;
        }
        if self.descriptions.is_empty() {
            fields -= 1;
        }
        if self.aliases.is_empty() {
            fields -= 1;
        }
        if self.sitelinks.is_empty() {
            fields -= 1;
        }
        if self.statements.is_empty() {
            fields -= 1;
        }
        let mut s = serializer.serialize_struct("Item", fields)?;
        if self.id.is_some() {
            let id: String = self.id.to_owned().into();
            s.serialize_field("id", &id)?;
        }
        if !self.labels.is_empty() {
            s.serialize_field("labels", &self.labels)?;
        }
        if !self.descriptions.is_empty() {
            s.serialize_field("descriptions", &self.descriptions)?;
        }
        if !self.aliases.is_empty() {
            s.serialize_field("aliases", &self.aliases)?;
        }
        if !self.sitelinks.is_empty() {
            s.serialize_field("sitelinks", &self.sitelinks)?;
        }
        if !self.statements.is_empty() {
            s.serialize_field("statements", &self.statements)?;
        }
        s.end()
    }
}

impl Item {
    /// Returns the statements of the item.
    pub const fn statements(&self) -> &Statements {
        &self.statements
    }

    /// Returns the statements of the item (mutable).
    pub const fn statements_mut(&mut self) -> &mut Statements {
        &mut self.statements
    }

    /// Returns the labels of the item.
    pub const fn labels(&self) -> &Labels {
        &self.labels
    }

    /// Returns the labels of the item (mutable).
    pub const fn labels_mut(&mut self) -> &mut Labels {
        &mut self.labels
    }

    /// Returns the descriptions of the item.
    pub const fn descriptions(&self) -> &Descriptions {
        &self.descriptions
    }

    /// Returns the descriptions of the item (mutable).
    pub const fn descriptions_mut(&mut self) -> &mut Descriptions {
        &mut self.descriptions
    }

    /// Returns the aliases of the item.
    pub const fn aliases(&self) -> &Aliases {
        &self.aliases
    }

    /// Returns the aliases of the item (mutable).
    pub const fn aliases_mut(&mut self) -> &mut Aliases {
        &mut self.aliases
    }

    /// Returns the aliases of the item as an `Aliases` object.
    pub fn as_aliases<S: Into<String>>(&self, lang: S) -> AliasesInLanguage {
        let lang: String = lang.into();
        let v: Vec<String> = self
            .aliases
            .get_lang(&lang)
            .iter()
            .map(|x| x.to_string())
            .collect();
        AliasesInLanguage::new(lang, v)
    }

    /// Returns the sitelinks of the item.
    pub const fn sitelinks(&self) -> &Sitelinks {
        &self.sitelinks
    }

    /// Returns the sitelinks of the item (mutable).
    pub const fn sitelinks_mut(&mut self) -> &mut Sitelinks {
        &mut self.sitelinks
    }

    /// Returns the header information of the item.
    pub const fn header_info(&self) -> &HeaderInfo {
        &self.header_info
    }

    /// Generates a patch to transform `other` into `self`
    pub fn patch(&self, other: &Self) -> Result<EntityPatch, RestApiError> {
        let labels_patch = self.labels.patch(other.labels())?;
        let descriptions_patch = self.descriptions.patch(other.descriptions())?;
        let aliases_patch = self.aliases.patch(other.aliases())?;
        let sitelinks_patch = self.sitelinks.patch(other.sitelinks())?;
        let statements_patch = self.statements.patch(other.statements())?;

        let mut ret = EntityPatch::item();
        ret.patch_mut().extend(labels_patch.patch().to_owned());
        ret.patch_mut()
            .extend(descriptions_patch.patch().to_owned());
        ret.patch_mut().extend(aliases_patch.patch().to_owned());
        ret.patch_mut().extend(sitelinks_patch.patch().to_owned());
        ret.patch_mut().extend(statements_patch.patch().to_owned());

        Ok(ret)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::language_strings::LanguageStrings;
    use crate::{LanguageString, RestApi, Sitelink, Statement};
    use serde_json::json;
    use wiremock::matchers::{body_partial_json, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    async fn get_test_item(id: &str) -> Result<Item, RestApiError> {
        let v = std::fs::read_to_string("test_data/Q42.json").unwrap();
        let v: Value = serde_json::from_str(&v).unwrap();

        let mock_server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/w/rest.php/wikibase/v1/entities/items/Q42"))
            .respond_with(ResponseTemplate::new(200).set_body_json(&v))
            .mount(&mock_server)
            .await;
        Mock::given(method("GET"))
            .and(path("/w/rest.php/wikibase/v1/entities/items/Q0"))
            .respond_with(ResponseTemplate::new(400).set_body_json(
                json!({"code": "invalid-item-id","message": "Not a valid item ID: Q0"}),
            ))
            .mount(&mock_server)
            .await;
        Mock::given(method("GET"))
            .and(path("/w/rest.php/wikibase/v1/entities/items/Q6"))
            .respond_with(ResponseTemplate::new(404).set_body_json(json!({"code": "item-not-found","message": "Could not find an item with the ID: Q6"})))
            .mount(&mock_server).await;
        let api = RestApi::builder(&(mock_server.uri() + "/w/rest.php"))
            .unwrap()
            .build();

        Item::get(EntityId::item(id), &api).await
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_item_get() {
        let item = get_test_item("Q42").await.unwrap();
        assert_eq!(item.id(), EntityId::item("Q42"));
        assert!(item.labels.has_language("en"));
        assert_eq!(item.labels().get_lang("en").unwrap(), "Douglas Adams");
        assert!(item
            .aliases()
            .get_lang("en")
            .contains(&"Douglas Noël Adams"));
        assert!(item.descriptions.has_language("en"));
        assert!(item.aliases.has_language("en"));
        assert!(item.sitelinks.get_wiki("enwiki").is_some());
        assert!(!item.statements.is_empty());
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_item_post() {
        let mut item = get_test_item("Q42").await.unwrap();
        let v = item.to_owned();

        let mock_server = MockServer::start().await;
        Mock::given(method("POST"))
            .and(path("/w/rest.php/wikibase/v1/entities/items"))
            .and(body_partial_json(
                json!({"item": {"labels": {"en": item.labels().get_lang("en")}}}),
            ))
            .respond_with(ResponseTemplate::new(200).set_body_json(&v))
            .mount(&mock_server)
            .await;
        let api = RestApi::builder(&(mock_server.uri() + "/w/rest.php"))
            .unwrap()
            .build();

        // Check that an error is returned when trying to post an item that already has an ID
        let r0 = item.post(&api).await;
        assert_eq!(r0.err().unwrap().to_string(), "ID already set");

        // Clear the ID and try again
        item.id = EntityId::None;
        let r1 = item.post(&api).await.unwrap();
        assert_eq!(r1.id(), v.id());
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_item_post_404() {
        let item = Item::default();
        let mock_server = MockServer::start().await;
        let api = RestApi::builder(&(mock_server.uri() + "/w/rest.php"))
            .unwrap()
            .build();
        let r = item.post(&api).await;
        assert_eq!(
            r.err().unwrap().to_string(),
            "Method POST not implemented for path /entities/items in REST API"
        );
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_invalid_item() {
        let item = get_test_item("Q0").await;
        // assert_eq!(item.err().unwrap().to_string(), "invalid-item-id");
        let err = item.err().unwrap();
        match err {
            RestApiError::ApiError {
                status,
                status_text,
                payload,
            } => {
                assert_eq!(status, 400);
                assert_eq!(status_text, "Bad Request");
                assert_eq!(payload.code(), "invalid-item-id");
                assert_eq!(payload.message(), "Not a valid item ID: Q0");
                assert_eq!(payload.context().len(), 0);
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_deleted_item() {
        let item = get_test_item("Q6").await;
        let err = item.err().unwrap();
        match err {
            RestApiError::ApiError {
                status,
                status_text,
                payload,
            } => {
                assert_eq!(status, 404);
                assert_eq!(status_text, "Not Found");
                assert_eq!(payload.code(), "item-not-found");
                assert_eq!(payload.message(), "Could not find an item with the ID: Q6");
                assert_eq!(payload.context().len(), 0);
            }
            _ => panic!("Wrong error type"),
        }
    }

    #[tokio::test]
    #[cfg_attr(miri, ignore)]
    async fn test_json_serialize() {
        let item = get_test_item("Q42").await.unwrap();
        let j = serde_json::to_string(&item).unwrap(); // Convert item to JSON text
        let v: Value = serde_json::from_str(&j).unwrap(); // Convert to JSON value
        let item_from_json = Item::from_json(v).unwrap(); // Convert back to Item
        assert_eq!(item, item_from_json); // Check if the reconstituted item is identical to the original
    }

    #[test]
    fn test_labels() {
        let mut item = Item::default();
        assert_eq!(item.labels().len(), 0);
        item.labels_mut().insert(LanguageString::new("en", "label"));
        assert_eq!(item.labels().len(), 1);
    }

    #[test]
    fn test_descriptions() {
        let mut item = Item::default();
        assert_eq!(item.descriptions().len(), 0);
        item.descriptions_mut()
            .insert(LanguageString::new("en", "description"));
        assert_eq!(item.descriptions().len(), 1);
    }

    #[test]
    fn test_aliases() {
        let mut item = Item::default();
        assert_eq!(item.aliases().len(), 0);
        item.aliases_mut()
            .insert(LanguageString::new("en", "alias"));
        assert_eq!(item.aliases().len(), 1);
    }

    #[test]
    fn test_as_aliases() {
        let mut item = Item::default();
        item.aliases_mut()
            .insert(LanguageString::new("en", "alias"));
        let aliases = item.as_aliases("en");
        assert_eq!(aliases.len(), 1);
    }

    #[test]
    fn test_statements() {
        let mut item = Item::default();
        assert_eq!(item.statements().len(), 0);
        item.statements_mut()
            .insert(Statement::new_string("P31", "Q42"));
        assert_eq!(item.statements().len(), 1);
    }

    #[test]
    fn test_sitelinks() {
        let mut item = Item::default();
        assert_eq!(item.sitelinks().len(), 0);
        item.sitelinks_mut()
            .set_wiki(Sitelink::new("enwiki", "Q42"));
        assert_eq!(item.sitelinks().len(), 1);
    }

    #[test]
    fn test_header_info() {
        let hi = HeaderInfo::default();
        let item = Item::default();
        assert_eq!(item.header_info(), &hi);
    }

    #[test]
    fn test_get_rest_api_path() {
        let item = Item::default();
        let id = EntityId::item("Q42");
        let path = item.get_my_rest_api_path(&id).unwrap();
        assert_eq!(path, "/entities/items/Q42");
    }

    #[test]
    fn test_patch() {
        let mut item1 = Item::default();
        let mut item2 = Item::default();
        item1
            .labels_mut()
            .insert(LanguageString::new("en", "label"));
        item2
            .labels_mut()
            .insert(LanguageString::new("en", "label2"));
        let patch = item1.patch(&item2).unwrap();
        assert_eq!(patch.patch().len(), 1);
    }
}