helpscout 0.0.4

Bindings for the HelpScout 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
//! Customer Endpoints
use serde_json;
use chrono::{DateTime, Utc};
use date_format::*;

use error::HelpScoutError;
use client::Client;
use envelope::{Collection, Item};

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerSocialProfileType {
    Twitter,
    Facebook,
    Linkedin,
    Aboutme,
    Google,
    Googleplus,
    Tungleme,
    Quora,
    Foursquare,
    Youtube,
    Flickr,
    Other,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerEmailLocationType {
    Home,
    Work,
    Other,
}

impl Default for CustomerEmailLocationType {
    fn default() -> CustomerEmailLocationType {
        CustomerEmailLocationType::Work
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerChatType {
    Aim,
    Gtalk,
    Icq,
    Xmpp,
    Msn,
    Skype,
    Yahoo,
    Qq,
    Other,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerPhoneLocationType {
    Home,
    Work,
    Mobile,
    Fax,
    Pager,
    Other,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerPhotoType {
    Unknown,
    Gravatar,
    Twitter,
    Facebook,
    Googleprofile,
    Googleplus,
    Linkedin,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CustomerGender {
    Male,
    Female,
    Unknown,
}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Customer {
    pub id: i32,
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub full_name: Option<String>,
    pub photo_url: Option<String>,
    pub gender: CustomerGender,
    pub age: Option<String>,
    pub organization: Option<String>,
    pub job_title: Option<String>,
    pub location: Option<String>,
    pub created_at: DateTime<Utc>,
    pub modified_at: Option<DateTime<Utc>>,

    // Additional fields that appear only when retrieving a single customer
    pub background: Option<String>,
    pub address: Option<CustomerAddress>,
    pub social_profiles: Option<Vec<CustomerSocialProfile>>,
    pub emails: Option<Vec<CustomerEmail>>,
    pub phones: Option<Vec<CustomerPhone>>, //Always return from get as an empty array for some reason
    pub chats: Option<Vec<CustomerChat>>,
    pub websites: Option<Vec<CustomerWebsite>>,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomerAddress {
    pub id: Option<i32>,
    pub city: String,
    pub state: String,
    pub country: String,
    pub postal_code: String,
    pub lines: Vec<String>, //Street address/apartment numbers, etc
    pub created_at: DateTime<Utc>,
    pub modified_at: Option<DateTime<Utc>>,
}

impl CustomerAddress {
    pub fn new(
        city: &str,
        state: &str,
        country: &str,
        postal_code: &str,
        lines: Vec<String>,
        created_at: DateTime<Utc>,
    ) -> CustomerAddress {
        CustomerAddress {
            id: None,
            city: city.into(),
            state: state.into(),
            country: country.into(),
            postal_code: postal_code.into(),
            lines: lines,
            created_at: created_at,
            modified_at: None,
        }
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomerSocialProfile {
    pub id: Option<i32>,
    pub value: String,
    #[serde(rename = "type")]
    pub social_profile_type: CustomerSocialProfileType,
}

impl CustomerSocialProfile {
    pub fn new(
        value: &str,
        social_profile_type: CustomerSocialProfileType,
    ) -> CustomerSocialProfile {
        CustomerSocialProfile {
            id: None,
            value: value.into(),
            social_profile_type: social_profile_type,
        }
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CustomerEmail {
    pub id: Option<i32>,
    pub value: String,
    pub location: CustomerEmailLocationType,
}

impl CustomerEmail {
    pub fn new(email: &str, location: CustomerEmailLocationType) -> CustomerEmail {
        CustomerEmail {
            id: None,
            value: email.into(),
            location: location,
        }
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CustomerPhone {
    pub id: Option<i32>,
    pub value: String,
    pub location: CustomerPhoneLocationType,
}

impl CustomerPhone {
    pub fn new(value: &str, location: CustomerPhoneLocationType) -> CustomerPhone {
        CustomerPhone {
            id: None,
            value: value.into(),
            location: location,
        }
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CustomerChat {
    pub id: Option<i32>,
    pub value: String,
    #[serde(rename = "type")]
    pub customer_chat_type: CustomerChatType,
}

impl CustomerChat {
    pub fn new(value: &str, customer_chat_type: CustomerChatType) -> CustomerChat {
        CustomerChat {
            id: None,
            value: value.into(),
            customer_chat_type: customer_chat_type,
        }
    }
}

#[derive(Debug, Serialize, Clone, Deserialize)]
pub struct CustomerWebsite {
    pub id: Option<i32>,
    pub value: String,
}

impl CustomerWebsite {
    pub fn new(value: &str) -> CustomerWebsite {
        CustomerWebsite {
            id: None,
            value: value.into(),
        }
    }
}

/// List Customers
///
/// API docs:
/// <https://developer.helpscout.com/help-desk-api/customers/list/>
///
/// ```rust
/// extern crate helpscout;
///
/// use helpscout::{Client, Collection, HelpScoutError};
/// use helpscout::api::customers::{self, Customer};
///
/// fn main() {
///     let mailboxes = list_mailboxes().expect("list mailboxes");
///     assert!(mailboxes.items.len() > 0);
/// }
///
/// fn list_mailboxes() -> Result<Collection<Customer>, HelpScoutError> {
///     let client = helpscout::Client::example();
///
///     //Grab list of customers with no parameters.
///     //You can add parameters to narrow the results through
///     //things such as .first_name() before sending the request.
///     //Check out the customer list api docs for more possible parameters.
///     customers::list().send(&client)
/// }
/// ```
pub fn list() -> CustomersListParamBuilder {
    let param_builder = CustomersListParamBuilder::new();
    param_builder
}

/// List Customers by Mailbox
///
/// API docs:
/// <https://developer.helpscout.com/help-desk-api/customers/list-mailbox/>
///
/// ```rust
/// extern crate helpscout;
///
/// use helpscout::{Client, Collection, HelpScoutError};
/// use helpscout::api::mailboxes::{self, Mailbox};
/// use helpscout::api::customers::{self, Customer};
///
/// fn main() {
///     let customers = list_customers_by_mailbox().expect("Customers for the mailbox to be listed");
///     assert!(customers.items.len() > 0);
/// }
///
/// fn list_customers_by_mailbox() -> Result<Collection<Customer>, HelpScoutError> {
///     let client = helpscout::Client::example();
///
///     //Grab list of mailboxes under an account to provide a mailbox ID for testing.
///     let mailboxes = mailboxes::list(&client);
///
///     //Return list of customers under the specified mailbox.
///     customers::list_by_mailbox(&client, mailboxes.items[0].id)
/// }
/// ```
pub fn list_by_mailbox(
    client: &Client,
    mailbox_id: i32,
) -> Result<Collection<Customer>, HelpScoutError> {
    let res = client.get(&format!("mailboxes/{}/customers.json", mailbox_id), ())?;
    let customers = serde_json::from_value(res.clone())?;
    Ok(customers)
}

/// Get Customer
///
/// API docs:
/// <https://developer.helpscout.com/help-desk-api/customers/get/>
///
/// ```rust
/// extern crate helpscout;
///
/// use helpscout::{Client, Collection, HelpScoutError};
/// use helpscout::api::customers::{self, Customer};
///
/// fn main() {
///     let customer = get_customer().expect("Customers for the mailbox to be listed");
///     assert!(customer.item.id > 0);
/// }
///
/// fn get_customer() -> Result<Collection<Customer>, HelpScoutError> {
///     let client = helpscout::Client::example();
///
///     //Grab list of customers with no parameters to get a specific customer id.
///     let customers = customers::list().send(&client)
///
///     //Return a single customer with the corresponding id, including the
///     //extra optional fields mentioned in the customer object documentation.
///     customers::get(&client, customers.items[0].id)
/// }
/// ```
pub fn get(client: &Client, id: i32) -> Result<Item<Customer>, HelpScoutError> {
    let res = client.get(&format!("customers/{}.json", id), ())?;
    let customer = serde_json::from_value(res.clone())?;
    Ok(customer)
}

/// Create Customer
///
/// API docs:
/// <https://developer.helpscout.com/help-desk-api/customers/create/>
///
/// ```rust
/// extern crate helpscout;
///
/// use uuid::Uuid;
/// use helpscout::{Client, Collection, HelpScoutError};
/// use helpscout::api::customers::{self, Customer, CustomerEmail, CustomerEmailLocationType, CustomerSocialProfile, CustomerSocialProfileType};
///
/// fn main() {
///     let customers = create_customer().expect("Customer to be created, then the new customer to be returned through the list function");
///     assert!(customers.items.len() > 0);
/// }
///
/// fn create_customer() -> Result<Collection<Customer>, HelpScoutError> {
///
///     //Create unique email to run the example multiple times.
///     let random_email_string = format!("guh{}@example.com", Uuid::new_v4());
///     let customer_email = CustomerEmail::new(&random_email_string, CustomerEmailLocationType::Work);
///
///     //Note that for each of these objects we're passing them as a vector.
///     //You can include more than one of these objects in the vec before sending it off as a request.
///     let customer_social_profiles = vec![CustomerSocialProfile::new("https://twitter.com/TwaikuGC", CustomerSocialProfileType::Twitter)];
///
///     //Create new customer object and upload it to HelpScout, then return the customer in a list
///     customers::create("Mega", "Dog", vec![customer_email] ).organization("megadog inc").job_title("MegaDoge").social_profiles(customer_social_profile).send(&c).expect("The new customer to be posted");
///     customers::list().last_name("Dog").page(1).send(&c).expect("Customers to be listed")
///
///
/// }
/// ```
pub fn create(first_name: &str, last_name: &str, emails: Vec<CustomerEmail>) -> NewCustomer {
    let new_customer = NewCustomer::new(first_name, last_name, emails);
    new_customer
}

/// Update Customer
///
/// API docs:
/// <https://developer.helpscout.com/help-desk-api/customers/update/>
///
/// ```rust
/// extern crate helpscout;
///
///
/// use helpscout::{Client, Collection, HelpScoutError};
/// use helpscout::api::customers::{self, Customer, CustomerEmail, CustomerEmailLocationType};
///
/// fn main() {
///     let customers = update_customer().expect("Customer to be created, then the new customer to be returned through the list function");
///     assert!(customers.items.len() > 0);
/// }
///
/// fn update_customer() -> Result<Collection<Customer>, HelpScoutError> {
///
///     //Pull list and get unique customer id and object to run the example.
///     let customers_list = customers::list().send(&c).expect("Customers to be listed");
///     let customer = customers::get(&c, customers_list.items[0].id);
///
///     //Let's build a new test email to add to the existing customer. We can also pull an email object
///     //out of the customer object and change the variables within it such as its type or address.
///     let new_customer_emails = CustomerEmail::new("newtest@email.com", CustomerEmailLocationType::Work);
///
///     //Pull the same customer last name, but update the first name to something new.
///     let customer_name = customers_list.items[0].last_name.clone().unwrap();
///
///     //Update customer based on id.
///     customers::update("UPDATEDTEST", &customer_name, vec![new_customer_emails]).organization("DOGS").background("UPDATEDTEST").send(&c, customers_list.items[0].id).expect("Customer to be updated");
///
///     //Return list of customers that have our updated first name
///     customers::list().first_name("UPDATEDTEST").send(&c).expect("Updated customer to be listed")
///
///
/// }
/// ```
pub fn update(first_name: &str, last_name: &str, emails: Vec<CustomerEmail>) -> UpdatedCustomer {
    let updated_customer = UpdatedCustomer::new(first_name, last_name, emails);
    updated_customer
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NewCustomer {
    #[serde(rename = "type")]
    pub first_name: String,
    pub last_name: String,
    pub emails: Vec<CustomerEmail>,
    pub organization: Option<String>,
    pub job_title: Option<String>,
    pub background: Option<String>,
    pub address: Option<CustomerAddress>,
    pub social_profiles: Option<Vec<CustomerSocialProfile>>,
    pub phones: Option<Vec<CustomerPhone>>,
    pub chats: Option<Vec<CustomerChat>>,
    pub websites: Option<Vec<CustomerWebsite>>,
}

impl NewCustomer {
    pub fn new(first_name: &str, last_name: &str, emails: Vec<CustomerEmail>) -> NewCustomer {
        NewCustomer {
            first_name: first_name.into(),
            last_name: last_name.into(),
            emails: emails,
            organization: None,
            job_title: None,
            background: None,
            address: None,
            social_profiles: None,
            phones: None,
            chats: None,
            websites: None,
        }
    }

    pub fn organization(&mut self, organization: &str) -> &mut NewCustomer {
        self.organization = Some(organization.into());
        self
    }

    pub fn job_title(&mut self, job_title: &str) -> &mut NewCustomer {
        self.job_title = Some(job_title.into());
        self
    }

    pub fn background(&mut self, background: &str) -> &mut NewCustomer {
        self.background = Some(background.into());
        self
    }

    pub fn address(&mut self, address: CustomerAddress) -> &mut NewCustomer {
        self.address = Some(address);
        self
    }

    pub fn social_profiles(
        &mut self,
        social_profiles: Vec<CustomerSocialProfile>,
    ) -> &mut NewCustomer {
        self.social_profiles = Some(social_profiles);
        self
    }

    pub fn phones(&mut self, phones: Vec<CustomerPhone>) -> &mut NewCustomer {
        self.phones = Some(phones);
        self
    }

    pub fn chats(&mut self, chats: Vec<CustomerChat>) -> &mut NewCustomer {
        self.chats = Some(chats);
        self
    }

    pub fn websites(&mut self, websites: Vec<CustomerWebsite>) -> &mut NewCustomer {
        self.websites = Some(websites);
        self
    }

    pub fn send(&self, client: &Client) -> Result<(), HelpScoutError> {
        let body = serde_json::to_value(self)?;
        //println!("{:?}", body);
        let res = client.post("customers.json", (), Some(body.to_string()))?;
        Ok(())
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdatedCustomer {
    pub first_name: String,
    pub last_name: String,
    pub emails: Vec<CustomerEmail>,
    pub organization: Option<String>,
    pub job_title: Option<String>,
    pub background: Option<String>,
    pub address: Option<CustomerAddress>,
    pub social_profiles: Option<Vec<CustomerSocialProfile>>,
    pub phones: Option<Vec<CustomerPhone>>,
    pub chats: Option<Vec<CustomerChat>>,
    pub websites: Option<Vec<CustomerWebsite>>,
}

impl UpdatedCustomer {
    pub fn new(first_name: &str, last_name: &str, emails: Vec<CustomerEmail>) -> UpdatedCustomer {
        UpdatedCustomer {
            first_name: first_name.into(),
            last_name: last_name.into(),
            emails: emails,
            organization: None,
            job_title: None,
            background: None,
            address: None,
            social_profiles: None,
            phones: None,
            chats: None,
            websites: None,
        }
    }

    pub fn first_name(&mut self, first_name: &str) -> &mut UpdatedCustomer {
        self.first_name = first_name.into();
        self
    }

    pub fn last_name(&mut self, last_name: &str) -> &mut UpdatedCustomer {
        self.last_name = last_name.into();
        self
    }

    pub fn email(&mut self, emails: Vec<CustomerEmail>) -> &mut UpdatedCustomer {
        self.emails = emails;
        self
    }

    pub fn organization(&mut self, organization: &str) -> &mut UpdatedCustomer {
        self.organization = Some(organization.into());
        self
    }

    pub fn job_title(&mut self, job_title: &str) -> &mut UpdatedCustomer {
        self.job_title = Some(job_title.into());
        self
    }

    pub fn background(&mut self, background: &str) -> &mut UpdatedCustomer {
        self.background = Some(background.into());
        self
    }

    pub fn address(&mut self, address: CustomerAddress) -> &mut UpdatedCustomer {
        self.address = Some(address);
        self
    }

    pub fn social_profiles(
        &mut self,
        social_profiles: Vec<CustomerSocialProfile>,
    ) -> &mut UpdatedCustomer {
        self.social_profiles = Some(social_profiles);
        self
    }

    pub fn phones(&mut self, phones: Vec<CustomerPhone>) -> &mut UpdatedCustomer {
        self.phones = Some(phones);
        self
    }

    pub fn chats(&mut self, chats: Vec<CustomerChat>) -> &mut UpdatedCustomer {
        self.chats = Some(chats);
        self
    }

    pub fn websites(&mut self, websites: Vec<CustomerWebsite>) -> &mut UpdatedCustomer {
        self.websites = Some(websites);
        self
    }

    pub fn send(&self, client: &Client, id: i32) -> Result<(), HelpScoutError> {
        let body = serde_json::to_value(self)?;
        let res = client.put(
            &format!("customers/{}.json", id),
            (),
            Some(body.to_string()),
        )?;
        Ok(())
    }
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomersListParamBuilder {
    pub first_name: Option<String>,
    pub last_name: Option<String>,
    pub email: Option<String>,
    #[serde(with = "optional_date_format")]
    pub modified_since: Option<DateTime<Utc>>,
    pub page: Option<i32>,
}

impl CustomersListParamBuilder {
    pub fn new() -> CustomersListParamBuilder {
        CustomersListParamBuilder {
            first_name: None,
            last_name: None,
            email: None,
            modified_since: None,
            page: None,
        }
    }

    pub fn first_name(&mut self, first_name: &str) -> &mut CustomersListParamBuilder {
        self.first_name = Some(first_name.into());
        self
    }

    pub fn last_name(&mut self, last_name: &str) -> &mut CustomersListParamBuilder {
        self.last_name = Some(last_name.into());
        self
    }

    pub fn email(&mut self, email: &str) -> &mut CustomersListParamBuilder {
        self.email = Some(email.into());
        self
    }

    pub fn modified_since(&mut self, modified_since: DateTime<Utc>) -> &mut CustomersListParamBuilder {
        self.modified_since = Some(modified_since);
        self
    }

    pub fn page(&mut self, page: i32) -> &mut CustomersListParamBuilder {
        self.page = Some(page);
        self
    }

    pub fn params(&self) -> Option<Vec<(String, String)>> {
        let params: Vec<(String, String)> = vec![];
        Some(params)
    }

    pub fn send(&self, client: &Client) -> Result<Collection<Customer>, HelpScoutError> {
        let res = client.get("customers.json", &self)?;
        let customers = serde_json::from_value(res.clone())?;
        Ok(customers)
    }
}