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
use crate::error::PitchforkError;
use crate::pitchfork::{DomoRequest, PagesRequestBuilder};
use log::debug;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;

// [Page Object](https://developer.domo.com/docs/page-api-reference/page)
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageInfo {
    pub name: String,
    pub id: String,
    #[serde(rename = "parentId")]
    pub parent_id: u64,
    #[serde(rename = "ownerId")]
    pub owner_id: u64,
    pub locked: bool,
    #[serde(rename = "collectionIds")]
    pub collection_ids: Vec<u64>,
    #[serde(rename = "cardIds")]
    pub card_ids: Vec<u64>,
    pub children: Vec<Page>,
    pub visibility: PageVisibility,
    #[serde(rename = "userIds")]
    pub user_ids: Vec<u64>,
    #[serde(rename = "pageIds")]
    pub page_ids: Vec<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Page {
    pub id: u64,
    pub name: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageVisibility {
    #[serde(rename = "userIds")]
    pub user_ids: Vec<u64>,
    #[serde(rename = "pageIds")]
    pub page_ids: Vec<u64>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PageCollection {
    pub id: u64,
    pub title: String,
    pub description: String,
    #[serde(rename = "cardIds")]
    pub card_ids: Vec<u64>,
}
impl<'t> PagesRequestBuilder<'t, PageInfo> {
    /// Info for a given Page
    ///
    /// # Example
    /// ```no_run
    /// # use domo_pitchfork::error::PitchforkError;
    /// use domo_pitchfork::pitchfork::DomoPitchfork;
    /// let domo = DomoPitchfork::with_token("token");
    /// let page_id = 123; // id of page to get details for.
    /// let ds_info = domo.pages().info(page_id);
    /// match ds_info {
    ///     Ok(ds) => println!("{:?}",ds),
    ///     Err(e) => println!("{}", e)
    /// };
    /// ```
    pub fn info(mut self, page_id: u64) -> Result<PageInfo, PitchforkError> {
        self.url.push_str(&page_id.to_string());
        let req = Self {
            method: Method::GET,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: None,
        };
        req.retrieve_and_deserialize_json()
    }

    /// List Pages starting from a given offset up to a given limit.
    /// # Example
    /// ```no_run
    /// # use domo_pitchfork::error::PitchforkError;
    /// use domo_pitchfork::pitchfork::DomoPitchfork;
    /// let domo = DomoPitchfork::with_token("token");
    /// let list = domo.pages().list(5,0)?;
    /// list.iter().map(|p| println!("Page Name: {}", p.name));
    /// # Ok::<(),PitchforkError>(())
    /// ```
    pub fn list(mut self, limit: u32, offset: u32) -> Result<Vec<PageInfo>, PitchforkError> {
        self.url
            .push_str(&format!("?limit={}&offset={}", limit, offset));
        let req = Self {
            method: Method::GET,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: None,
        };
        let ds_list = serde_json::from_reader(req.send_json()?)?;
        Ok(ds_list)
    }

    pub fn create(self, page: &PageInfo) -> Result<PageInfo, PitchforkError> {
        let body = serde_json::to_string(page)?;
        debug!("body: {}", body);
        let req = Self {
            method: Method::POST,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: Some(body),
        };
        req.retrieve_and_deserialize_json()
    }

    /// Delete the Page for the given id.
    /// This is destructive and cannot be reversed.
    /// # Example
    /// ```no_run
    /// # use domo_pitchfork::error::PitchforkError;
    /// # use domo_pitchfork::pitchfork::DomoPitchfork;
    /// # let token = "token_here";
    /// let domo = DomoPitchfork::with_token(&token);
    ///
    /// let page_id = 123; // id of page to delete.
    /// // if it fails to delete, print err msg
    /// if let Err(e) = domo.pages().delete(page_id) {
    ///     println!("{}", e)
    /// }
    /// ```
    pub fn delete(mut self, page_id: u64) -> Result<(), PitchforkError> {
        self.url.push_str(&page_id.to_string());
        let req = Self {
            method: Method::DELETE,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: None,
        };
        req.send_json()?;
        Ok(())
    }

    pub fn modify(mut self, page_id: u64, page: &PageInfo) -> Result<PageInfo, PitchforkError> {
        self.url.push_str(&page_id.to_string());
        let body = serde_json::to_string(page)?;
        debug!("body: {}", body);
        let req = Self {
            method: Method::PUT,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: Some(body),
        };
        let ds = serde_json::from_reader(req.send_json()?)?;
        Ok(ds)
    }

    pub fn collections(mut self, page_id: u64) -> Result<Vec<PageCollection>, PitchforkError> {
        self.url.push_str(&format!("{}/collections", page_id));
        let req = Self {
            method: Method::GET,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: None,
        };
        let ds = serde_json::from_reader(req.send_json()?)?;
        Ok(ds)
    }
    // TODO: check domo does in fact return an empty body here
    pub fn create_collection(
        mut self,
        page_id: u64,
        collection: &PageCollection,
    ) -> Result<(), PitchforkError> {
        self.url.push_str(&format!("{}/collections", page_id));
        let body = serde_json::to_string(collection)?;
        debug!("body: {}", body);
        let req = Self {
            method: Method::POST,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: Some(body),
        };
        let ds = serde_json::from_reader(req.send_json()?)?;
        Ok(ds)
    }
    pub fn modify_collection(
        mut self,
        page_id: u64,
        collection_id: u64,
        collection: &PageCollection,
    ) -> Result<(), PitchforkError> {
        self.url
            .push_str(&format!("{}/collections/{}", page_id, collection_id));
        let body = serde_json::to_string(collection)?;
        debug!("body: {}", body);
        let req = Self {
            method: Method::PUT,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: Some(body),
        };
        req.send_json()?;
        Ok(())
    }
    pub fn delete_collection(
        mut self,
        page_id: u64,
        collection_id: u64,
    ) -> Result<(), PitchforkError> {
        self.url
            .push_str(&format!("{}/collections/{}", page_id, collection_id));
        let req = Self {
            method: Method::DELETE,
            auth: self.auth,
            url: self.url,
            resp_t: PhantomData,
            body: None,
        };
        req.send_json()?;
        Ok(())
    }
}