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
use crate::data_structure::collection::Collection;
use crate::data_structure::item::Item;
use serde_json::value::Value;
use std::error;

/// Perform get operations on Zotero items and collections.
/// ```no_run
/// use zotero::ZoteroInit;
/// use zotero::Get;
///
/// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
/// let items = z.get_items(None);
/// ```
pub trait Get<'a> {
    fn get_request<S: AsRef<str> + std::fmt::Display>(
        &self,
        params: S,
        extra_params: Option<&str>,
    ) -> Result<Value, Box<dyn error::Error>>;
    fn get_id(&self) -> &'a str;
    fn get_api_key(&self) -> &'a str;

    /// Retreive information about an api key and it's privileges.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collection = z.get_api_key_info("bZARysJ579K5SdmYuaAJ");
    /// ```
    fn get_api_key_info<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Value, Box<dyn error::Error>> {
        let params = format!("/keys/{}", self.get_api_key());
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive specific item in the library by it's ID.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let items = z.get_item("B8ZNE3GH", None);
    /// ```
    fn get_item<I: Into<Option<&'a str>>>(
        &self,
        item_id: &'a str,
        extra_params: I,
    ) -> Result<Item, Box<dyn error::Error>> {
        let params: String = format!("/items/{}", item_id);
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive all items in the library, excluding trashed items.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let items = z.get_items(None);
    /// ```
    fn get_items<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/items");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive top-level items in the library, excluding trashed items.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let top_items = z.get_top_items(None);
    /// ```
    fn get_top_items<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/items/top");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive items in the trash.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let trashed_items = z.get_trashed_items(None);
    /// ```
    fn get_trashed_items<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/items/trash");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive items in "My publications".
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let my_publications = z.get_publications(None);
    /// ```
    fn get_publications<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/publications/items");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive a collection by it's id.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collection = z.get_collection("AYVWED", None);
    /// ```
    fn get_collection<I: Into<Option<&'a str>>>(
        &self,
        collection_id: &'a str,
        extra_params: I,
    ) -> Result<Collection, Box<dyn error::Error>> {
        let params = format!("/collections/{}", collection_id);
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive a collection by it's name.    
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collection = z.get_collection_by_name("My collection", None);
    /// ```
    fn get_collection_by_name<I: Into<Option<&'a str>>>(
        &self,
        collection_name: &'a str,
        extra_params: I,
    ) -> Result<Option<Collection>, Box<dyn error::Error>> {
        let collections = self.get_collections(extra_params)?;
        let filtered_collections = collections
            .into_iter()
            .filter(|collection| collection.data.name == collection_name)
            .collect::<Vec<Collection>>();

        if filtered_collections.len() >= 1 {
            Ok(Some(filtered_collections[0].clone()))
        } else {
            Ok(None)
        }
    }

    /// Retreive a set of collections by their names.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collections = z.get_collections_by_names(vec!("My collection", "My other collection"), None);
    /// ```
    fn get_collections_by_names<I: Into<Option<&'a str>>>(
        &self,
        collection_names: Vec<&'a str>,
        extra_params: I,
    ) -> Result<Option<Vec<Collection>>, Box<dyn error::Error>> {
        let collections = self.get_collections(extra_params)?;
        let filtered_collections = collections
            .into_iter()
            .filter(|collection| {
                let collection_data_name: &str = &collection.data.name;
                collection_names.contains(&&collection_data_name)
            })
            .collect::<Vec<Collection>>();

        if filtered_collections.len() >= 1 {
            Ok(Some(filtered_collections))
        } else {
            Ok(None)
        }
    }

    /// Retreive all collections.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collections = z.get_collections(None);
    /// ```
    fn get_collections<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Collection>, Box<dyn error::Error>> {
        let params = format!("/collections");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive top level collections.
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let top_collections = z.get_top_collections(None);
    /// ```
    fn get_top_collections<I: Into<Option<&'a str>>>(
        &self,
        extra_params: I,
    ) -> Result<Vec<Collection>, Box<dyn error::Error>> {
        let params = format!("/collections/top");
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive all items for a given collection
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collection_items = z.get_collection_items("AYVWED", None);
    /// ```
    fn get_collection_items<I: Into<Option<&'a str>>>(
        &self,
        collection_id: &'a str,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/collections/{}/items", collection_id);
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive top-level items for a given collection
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let collection_top_items = z.get_collection_top_items("AYVWED", "B8ZNE3GH", None);
    /// ```
    fn get_collection_top_items<I: Into<Option<&'a str>>>(
        &self,
        collection_id: &'a str,
        item_id: &'a str,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!("/collection/{}/items/{}", collection_id, item_id);
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    /// Retreive up to 50 items by their ids
    /// ```no_run
    /// # use zotero::ZoteroInit;
    /// # use zotero::Get;
    /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    /// let items = z.get_subset(vec!("AYVWED", "B8ZNE3GH"), None);
    /// ```
    fn get_subset<I: Into<Option<&'a str>>>(
        &self,
        item_ids: Vec<&'a str>,
        extra_params: I,
    ) -> Result<Vec<Item>, Box<dyn error::Error>> {
        let params = format!(
            "/items?itemKey={}",
            item_ids
                .iter()
                .map(|elem| elem.to_string())
                .collect::<Vec<String>>()
                .join(",")
        );
        let response = self.get_request(&params, extra_params.into())?;
        Ok(serde_json::from_value(response)?)
    }

    // /// Retreive notes.
    // /// ```no_run
    // /// # use zotero::ZoteroInit;
    // /// # use zotero::Get;
    // /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    // /// let items = z.get_all_notes();
    // /// ```
    // fn get_all_notes(&self) -> Result<Vec<Item>, Box<dyn error::Error>> {
    //     let params = format!("/items/");
    //     let response = self.get_request(&params, Some("itemType=note"))?;
    //     Ok(serde_json::from_value(response)?)
    // }

    // ///Retreive all attachment.
    // /// ```no_run
    // /// # use zotero::ZoteroInit;
    // /// # use zotero::Get;
    // /// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
    // /// let items = z.get_all_attachments();
    // /// ```
    // fn get_all_attachments(&self) -> Result<Vec<Item>, Box<dyn error::Error>> {
    //     let params = format!("/items/");
    //     let response = self.get_request(&params, Some("itemType=attachment"))?;
    //     Ok(serde_json::from_value(response)?)
    // }
}