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
use crate::domo::activity_log::ActivityLogEntry;
use crate::domo::dataset::Dataset;
use crate::domo::group::GroupInfo;
use crate::domo::page::PageInfo;
use crate::domo::stream::StreamDataset;
use crate::domo::user::User;
use crate::error::{PitchforkError, PitchforkErrorKind};
use lazy_static::lazy_static;
use reqwest::Client;
use reqwest::Method;
use serde::de::DeserializeOwned;
use std::marker::PhantomData;

macro_rules! impl_domo_requests {
    ($i: ident) => {
        impl<'t, T> BaseRequest for $i<'t, T>
        where
            T: DeserializeOwned,
        {
            fn auth(&self) -> &str {
                self.auth
            }
            fn url(&self) -> &str {
                &self.url[..]
            }
            fn method(&self) -> Method {
                self.method.clone()
            }
            fn body(&self) -> Option<String> {
                self.body.clone()
            }
        }
        impl<'t, T> From<DomoRequestBuilder<'t, T>> for $i<'t, T>
        where
            for<'de> T: DeserializeOwned,
        {
            fn from(drb: DomoRequestBuilder<'t, T>) -> Self {
                Self {
                    auth: drb.auth,
                    method: drb.method,
                    url: drb.url,
                    resp_t: PhantomData,
                    body: drb.body,
                }
            }
        }
        impl<'t, T> DomoRequest<T> for $i<'t, T> where for<'de> T: DeserializeOwned {}
    };
}
lazy_static! {
    /// Static HTTP Client for Domo API
    #[doc(hidden)]
    pub static ref CLIENT: Client = Client::new();
}

/// `DomoPitchfork` is the top-level object to use to interact with the various Domo APIs
#[derive(Clone)]
pub struct DomoPitchfork<'t> {
    /// Domo Auth Token
    auth: &'t str,
}

impl<'t> DomoPitchfork<'t> {
    /// Create a new DomoPitchfork with a Domo Auth token
    pub fn with_token(token: &'t str) -> Self {
        Self { auth: token }
    }
    /// Interact with Domo Datasets API
    pub fn datasets(&self) -> DatasetsRequestBuilder<'t, Dataset> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/datasets/").into()
    }
    /// Interact with Domo Streams API
    pub fn streams(&self) -> StreamsRequestBuilder<'t, StreamDataset> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/streams/").into()
    }
    /// Interact with Domo Users API
    pub fn users(&self) -> UsersRequestBuilder<'t, User> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/users/").into()
    }
    /// Interact with Domo Groups API
    pub fn groups(&self) -> GroupsRequestBuilder<'t, GroupInfo> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/groups/").into()
    }
    /// Interact with Domo Pages API
    pub fn pages(&self) -> PagesRequestBuilder<'t, PageInfo> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/pages/").into()
    }
    /// Interact with Domo Activity Log API.
    pub fn audit(&self) -> ActivitiesRequestBuilder<'t, ActivityLogEntry> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/audit/").into()
    }
    /// Interact with Domo Projects API
    pub fn projects(&self) -> ProjectsRequestBuilder<'t, ()> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/projects/").into()
    }
    /// Interact with Domo Accounts API
    pub fn accounts(&self) -> AccountsRequestBuilder<'t, ()> {
        DomoRequestBuilder::new(self.auth, "https://api.domo.com/v1/accounts/").into()
    }
}

/// Request Builder for all Dataset API interactions
pub struct DatasetsRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}

/// Request Builder for all Stream API interactions
pub struct StreamsRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all User API interactions
pub struct UsersRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all Group API interactions
pub struct GroupsRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all Page API interactions
pub struct PagesRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all Activity Log API interactions
pub struct ActivitiesRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all Account API interactions
pub struct AccountsRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
/// Request Builder for all Project and Task API interactions
pub struct ProjectsRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}
impl_domo_requests!(StreamsRequestBuilder);
impl_domo_requests!(DatasetsRequestBuilder);
impl_domo_requests!(UsersRequestBuilder);
impl_domo_requests!(GroupsRequestBuilder);
impl_domo_requests!(PagesRequestBuilder);
impl_domo_requests!(ActivitiesRequestBuilder);
impl_domo_requests!(AccountsRequestBuilder);
impl_domo_requests!(ProjectsRequestBuilder);
pub struct DomoRequestBuilder<'t, T: 't>
where
    for<'de> T: DeserializeOwned,
{
    pub auth: &'t str,
    pub method: Method,
    pub url: String,
    pub resp_t: PhantomData<*const T>,
    pub body: Option<String>,
}

impl<'t, T> DomoRequestBuilder<'t, T>
where
    T: DeserializeOwned,
{
    pub fn new<S>(auth: &'t str, url: S) -> DomoRequestBuilder<'t, T>
    where
        for<'de> S: Into<String>,
        T: DeserializeOwned,
    {
        DomoRequestBuilder {
            auth,
            method: Method::GET,
            url: url.into(),
            resp_t: PhantomData,
            body: None,
        }
    }
}

impl<'t, T> BaseRequest for DomoRequestBuilder<'t, T>
where
    T: DeserializeOwned,
{
    fn url(&self) -> &str {
        &self.url[..]
    }
    fn auth(&self) -> &str {
        self.auth
    }
    fn method(&self) -> Method {
        self.method.clone()
    }
    fn body(&self) -> Option<String> {
        self.body.clone()
    }
}

impl<'t, T> DomoRequest<T> for DomoRequestBuilder<'t, T> where for<'de> T: DeserializeOwned {}

/// Base level request info.
pub trait BaseRequest {
    fn url(&self) -> &str;
    fn auth(&self) -> &str;
    fn method(&self) -> Method;
    fn body(&self) -> Option<String>;
}

/// Defines Domo Requests
pub trait DomoRequest<T>: BaseRequest {
    fn run(&self) -> Result<T, PitchforkError>
    where
        for<'de> T: DeserializeOwned,
    {
        let mut response = CLIENT
            .request(self.method(), self.url())
            .bearer_auth(self.auth())
            .header("Content-Type", "application/json")
            .body(self.body().take().unwrap_or_default())
            .send()
            .expect("ಠ_ಠ you just got Domo'd");

        if response.status().is_success() {
            let res: T = response.json()?;
            Ok(res)
        } else {
            eprintln!("response: {:?}", &response);
            let code = response.status().as_u16();
            Err(PitchforkErrorKind::DomoBadRequest(code, response.text()?).into())
        }
    }
    fn retrieve_and_deserialize_json(&self) -> Result<T, PitchforkError>
    where
        for<'de> T: DeserializeOwned,
    {
        let mut response = CLIENT
            .request(self.method(), self.url())
            .bearer_auth(self.auth())
            .header("Content-Type", "application/json")
            .body(self.body().take().unwrap_or_default())
            .send()
            .expect("ಠ_ಠ you just got Domo'd");

        if response.status().is_success() {
            //            let mut body = vec![];
            //            std::io::copy(&mut response, &mut body);
            //            println!("{}", String::from_utf8(body).unwrap());
            let res: T = response.json()?;
            Ok(res)
        } else {
            let code = response.status().as_u16();
            Err(PitchforkErrorKind::DomoBadRequest(code, response.text()?).into())
        }
    }

    fn send_csv(&self) -> Result<reqwest::Response, PitchforkError> {
        let mut response = CLIENT
            .request(self.method(), self.url())
            .bearer_auth(self.auth())
            .header("Content-Type", "text/csv")
            .body(self.body().take().unwrap_or_default())
            .send()?;
        if response.status().is_success() {
            Ok(response)
        } else {
            let code = response.status().as_u16();
            Err(PitchforkErrorKind::DomoBadRequest(code, response.text()?).into())
        }
    }
    fn send_json(&self) -> Result<reqwest::Response, PitchforkError> {
        let mut response = CLIENT
            .request(self.method(), self.url())
            .bearer_auth(self.auth())
            .header("Content-Type", "application/json")
            .body(self.body().take().unwrap_or_default())
            .send()?;
        if response.status().is_success() {
            Ok(response)
        } else {
            let code = response.status().as_u16();
            Err(PitchforkErrorKind::DomoBadRequest(code, response.text()?).into())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::DomoClientAppCredentials;
    use std::env;
    #[test]
    fn test_dataset_list() {
        let domo_client_id = env::var("DOMO_CLIENT_ID").expect("No DOMO_CLIENT_ID env var found");
        let domo_secret = env::var("DOMO_SECRET").expect("No DOMO_SECRET env var found");
        let client_creds = DomoClientAppCredentials::default()
            .client_id(&domo_client_id)
            .client_secret(&domo_secret)
            .build();
        let token = client_creds.get_access_token();
        let domo = DomoPitchfork::with_token(&token);
        let ds_list = domo.datasets().list(5, 0);
        match ds_list {
            Ok(ds) => {
                println!("{:?}", ds);
                assert_eq!(ds.len(), 5);
            }
            Err(e) => println!("{}", e),
        };
    }

    #[test]
    fn test_dataset_query() {
        let domo_client_id = env::var("DOMO_CLIENT_ID").expect("No DOMO_CLIENT_ID env var found");
        let domo_secret = env::var("DOMO_SECRET").expect("No DOMO_SECRET env var found");
        let client_creds = DomoClientAppCredentials::default()
            .client_id(&domo_client_id)
            .client_secret(&domo_secret)
            .build();
        let token = client_creds.get_access_token();
        let domo = DomoPitchfork::with_token(&token);
        let dq = domo.datasets().query_data(
            "9e325a09-e7da-42b3-a34f-f96a25928d81",
            "SELECT * FROM table WHERE `Order Priority` = 'High'",
        );
        match dq {
            Ok(ds) => {
                println!("{:?}", ds);
                assert_eq!(ds.columns.len(), 3);
                assert_eq!(ds.num_rows, 4);
            }
            Err(e) => println!("{}", e),
        };
    }
}