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
use std::error;

use serde::Serialize;
use serde_json::value::Value;

/// Perform post operations on Zotero items and collections.
/// ```no_run
/// use zotero::ZoteroInit;
/// use zotero::Post;
/// use zotero::data_structure::item::{BookData, BookDataBuilder, Creator, CreatorBuilder};
///
/// let creators : Vec<Creator> = vec![
///     CreatorBuilder::default()
///         .creator_type("author")
///         .first_name("John")
///         .last_name("Doe")
///         .build()
///         .unwrap()
/// ];
///
/// let new_book : BookData = BookDataBuilder::default()
///     .title("A title")
///     .creators(creators)
///     .item_type("book")
///     .build()
///     .unwrap();
///
/// let z = ZoteroInit::set_user("123456789", "bZARysJ579K5SdmYuaAJ");
/// let new_item = z.create_new_item(new_book);
/// ```
pub trait Post<'a> {
    fn post_request<T: Serialize, S: AsRef<str> + std::fmt::Display>(
        &self,
        params: S,
        json_body: T,
    ) -> Result<Value, Box<dyn error::Error>>;
    fn get_id(&self) -> &'a str;

    fn create_new_item<T: Serialize>(&self, item: T) -> Result<Value, Box<dyn error::Error>> {
        let params = format!("/items");
        self.post_request(&params, vec![&item])
    }

    /// Create multiple items
    fn create_new_items<T: Serialize>(&self, item: Vec<T>) -> Result<Value, Box<dyn error::Error>> {
        let params = format!("/items");
        self.post_request(&params, &item)
    }

    /// Create new collection
    fn create_new_collection<T: Serialize>(&self, item: T) -> Result<Value, Box<dyn error::Error>> {
        let params = format!("/collections");
        self.post_request(&params, vec![&item])
    }

    /// Create new collections
    fn create_new_collections<T: Serialize>(
        &self,
        item: Vec<T>,
    ) -> Result<Value, Box<dyn error::Error>> {
        let params = format!("/collections");
        self.post_request(&params, &item)
    }
}