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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
use super::empty::EmptyType;
use super::link::LinkType;
use super::list_member_activity::{CollectionListMemberActivity, ListMemberActivityBuilder};
use super::list_member_goals::{CollectionListMemberGoal, ListMemberGoalBuilder};
use super::list_member_notes::{CollectionListMemberNote, ListMemberNote, ListMemberNoteBuilder};
use super::list_member_tags::{
    CollectionListMemberTag, ListMemberTagBuilder, ListMemberTagParam, ListMemberTagType,
};
use crate::api::MailchimpApi;
use crate::internal::error_type::MailchimpErrorType;
use crate::internal::request::MailchimpResult;
use crate::iter::{BuildIter, MailchimpCollection, MalchimpIter, ResourceFilter, SimpleFilter};
use std::collections::HashMap;

use log::error;

///
/// The most recent Note added about this member.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListNote {
    /// The note id.
    #[serde(default)]
    pub note_id: u64,
    /// The date and time the note was created in ISO 8601 format.
    #[serde(default)]
    pub created_at: String,
    /// The author of the note.
    #[serde(default)]
    pub created_by: String,
    /// The content of the note.
    #[serde(default)]
    pub note: String,
}

impl Default for ListNote {
    fn default() -> Self {
        ListNote {
            note_id: 0,
            created_at: "".to_string(),
            created_by: "".to_string(),
            note: "".to_string(),
        }
    }
}

///
/// The marketing permissions for the subscriber.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListMarketingPermision {
    /// The id for the marketing permission on the list
    #[serde(default)]
    pub marketing_permission_id: String,
    /// The text of the marketing permission.
    #[serde(default)]
    pub text: String,
    /// If the subscriber has opted-in to the marketing permission.
    #[serde(default)]
    pub enabled: bool,
}

impl Default for ListMarketingPermision {
    fn default() -> Self {
        ListMarketingPermision {
            marketing_permission_id: "".to_string(),
            text: "".to_string(),
            enabled: false,
        }
    }
}

///
/// Open and click rates for this subscriber.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SubscriberStats {
    /// A subscriber’s average open rate.
    #[serde(default)]
    pub avg_open_rate: f32,
    /// A subscriber’s average clickthrough rate.
    #[serde(default)]
    pub avg_click_rate: f32,
}

impl Default for SubscriberStats {
    fn default() -> Self {
        SubscriberStats {
            avg_open_rate: 0.0,
            avg_click_rate: 0.0,
        }
    }
}

///
/// Subscriber location information.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SubscriberLocation {
    /// The location latitude.
    #[serde(default)]
    pub latitude: f32,
    /// The location longitude.
    #[serde(default)]
    pub longitude: f32,
    /// The time difference in hours from GMT.
    #[serde(default)]
    pub gmtoff: i64,
    /// The offset for timezones where daylight saving time is observed.
    #[serde(default)]
    pub dstoff: i64,
    /// The unique code for the location country.
    #[serde(default)]
    pub country_code: String,
    /// The timezone for the location.
    #[serde(default)]
    pub timezone: String,
}

impl Default for SubscriberLocation {
    fn default() -> Self {
        SubscriberLocation {
            latitude: 0.0,
            longitude: 0.0,
            gmtoff: 0,
            dstoff: 0,
            country_code: "".to_string(),
            timezone: "".to_string(),
        }
    }
}

// ============ List Locations ==============
///
/// Get information about a specific list member, including a currently subscribed,
/// unsubscribed, or bounced member.
///
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListMember {
    /// The MD5 hash of the lowercase version of the list member’s email address.
    #[serde(default)]
    pub id: String,
    /// Email address for a subscriber.
    #[serde(default)]
    pub email_address: String,
    /// An identifier for the address across all of Mailchimp.
    #[serde(default)]
    pub unique_email_id: String,
    /// Type of email this member asked to get (‘html’ or ‘text’).
    #[serde(default)]
    pub email_type: String,
    /// Subscriber’s current status.
    #[serde(default)]
    pub status: String,
    /// A subscriber’s reason for unsubscribing.
    #[serde(default)]
    pub unsubscribe_reason: String,
    /// An individual merge var and value for a member.
    #[serde(default)]
    pub merge_fields: HashMap<String, String>,
    /// The key of this object’s properties is the ID of the interest in question.
    #[serde(default)]
    pub interests: HashMap<String, String>,
    /// Open and click rates for this subscriber.
    #[serde(default)]
    pub stats: SubscriberStats,
    /// The date and time the subscriber signed up for the list in ISO 8601 format.
    #[serde(default)]
    pub timestamp_signup: String,
    /// The IP address the subscriber used to confirm their opt-in status.
    #[serde(default)]
    pub ip_opt: String,
    /// The date and time the subscribe confirmed their opt-in status in ISO 8601 format.
    #[serde(default)]
    pub timestamp_opt: String,
    /// Star rating for this member, between 1 and 5.
    #[serde(default)]
    pub member_rating: u64,
    /// The date and time the member’s info was last changed in ISO 8601 format.
    #[serde(default)]
    pub last_changed: String,
    /// If set/detected, the subscriber’s language.
    #[serde(default)]
    pub language: String,
    /// VIP status for subscriber.
    #[serde(default)]
    pub vip: bool,
    /// The list member’s email client.
    #[serde(default)]
    pub email_client: String,
    /// Subscriber location information.
    #[serde(default)]
    pub location: SubscriberLocation,
    /// The marketing permissions for the subscriber.
    #[serde(default)]
    pub marketing_permissions: Vec<ListMarketingPermision>,
    /// The most recent Note added about this member.
    #[serde(default)]
    pub last_note: ListNote,
    /// The number of tags applied to this member.
    #[serde(default)]
    pub tags_count: u64,
    /// The tags applied to this member.
    #[serde(default)]
    pub tags: Vec<String>,
    /// IP address the subscriber signed up from.
    #[serde(default)]
    pub ip_signup: String,
    /// The list id.
    #[serde(default)]
    pub list_id: String,
    /// A list of link types and descriptions for the API schema documents.
    #[serde(default)]
    pub _links: Vec<LinkType>,

    /// Mailchimp API
    #[serde(skip)]
    _api: MailchimpApi,
    /// Endpoint Base for the instance
    #[serde(skip)]
    _endpoint: String,
}

impl ListMember {
    // ============== Actions ==============
    ///
    /// Permanently delete a list member
    ///
    /// Delete all personally identifiable information related to a list member, and
    /// remove them from a list. This will make it impossible to re-import the list member.
    ///
    ///
    pub fn permanently_delete(&self) -> Option<MailchimpErrorType> {
        // POST /lists/{list_id}/members/{subscriber_hash}/actions/delete-permanent
        let mut b_endpoint = self._endpoint.clone();
        b_endpoint.push_str("/actions/delete-permanent");
        match self
            ._api
            .post::<EmptyType, HashMap<String, String>>(b_endpoint.as_str(), HashMap::new())
        {
            Ok(_) => None,
            Err(e) => Some(e),
        }
    }
    ///
    /// Permanently delete a list member
    ///
    /// Delete a member from a list.
    ///
    pub fn delete(&self) -> Option<MailchimpErrorType> {
        // DELETE /lists/{list_id}/members/{subscriber_hash}
        let b_endpoint = self._endpoint.clone();
        match self
            ._api
            .delete::<EmptyType>(b_endpoint.as_str(), HashMap::new())
        {
            Ok(_) => None,
            Err(e) => Some(e),
        }
    }

    /**
     * Update API
     */
    pub fn set_api(&mut self, n_api: &MailchimpApi) {
        self._api = n_api.clone()
    }

    ///
    /// Set new value for endpoint
    ///
    /// Argumentos:
    ///     n_endpoint: Nuevo Endpoint
    ///
    pub fn set_endpoint<'a>(&mut self, n_endpoint: &'a str) {
        self._endpoint = n_endpoint.to_string();
    }

    ///
    /// Get resource endpoint
    ///
    ///
    pub fn get_base_endpoint(&self) -> String {
        let mut endpoint = self._endpoint.clone() + "/";
        endpoint.push_str(&self.id);
        endpoint
    }

    ///
    /// Get details about subscribers’ recent activity.
    ///
    pub fn get_activity(&self) -> MalchimpIter<ListMemberActivityBuilder> {
        // GET /lists/{list_id}/members/{subscriber_hash}/activity
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/activity");
        let filter_params = SimpleFilter::default();

        match self
            ._api
            .get::<CollectionListMemberActivity>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: ListMemberActivityBuilder {},
                data: collection.activity,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self._api.clone(),
                endpoint: endpoint.clone(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get List Members: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: ListMemberActivityBuilder {},
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self._api.clone(),
                    endpoint: endpoint.clone(),
                }
            }
        }
    }

    ///
    /// Get information about recent goal events for a specific list member.
    ///
    /// Arguments:
    ///     subscriber_hash: The MD5 hash of the lowercase version of the list member’s email address.
    ///
    pub fn get_goals(&self) -> MalchimpIter<ListMemberGoalBuilder> {
        // GET  /lists/{list_id}/members/{subscriber_hash}/goals
        let mut endpoint = self.get_base_endpoint().to_string() + "/";
        endpoint.push_str(&self.id);
        endpoint.push_str("/goals");
        let filter_params = SimpleFilter::default();

        match self
            ._api
            .get::<CollectionListMemberGoal>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: ListMemberGoalBuilder {},
                data: collection.goals,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self._api.clone(),
                endpoint: endpoint.clone(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get List Members: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: ListMemberGoalBuilder {},
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self._api.clone(),
                    endpoint: endpoint.clone(),
                }
            }
        }
    }
    ///
    /// Get the tags on a list member.
    ///
    pub fn get_tags(&self) -> MalchimpIter<ListMemberTagBuilder> {
        // GET /lists/{list_id}/members/{subscriber_hash}/tags
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/tags");
        let filter_params = SimpleFilter::default();

        match self
            ._api
            .get::<CollectionListMemberTag>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: ListMemberTagBuilder {},
                data: collection.tags,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self._api.clone(),
                endpoint: endpoint.clone(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get List Members: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: ListMemberTagBuilder {},
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self._api.clone(),
                    endpoint: endpoint.clone(),
                }
            }
        }
    }

    ///
    /// Post the tags on a list member.
    ///
    /// Add or remove tags from a list member. If a tag that does not exist is passed in
    /// and set as ‘active’, a new tag will be created.
    ///
    pub fn post_tag(&self, tags: Vec<ListMemberTagType>) -> Option<MailchimpErrorType> {
        // POST /lists/{list_id}/members/{subscriber_hash}/tags
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/tags");
        let param = ListMemberTagParam { tags: tags };

        match self
            ._api
            .post::<EmptyType, ListMemberTagParam>(&endpoint, param)
        {
            Ok(_) => None,
            Err(e) => Some(e),
        }
    }

    ///
    /// Get recent notes for a specific list member
    ///
    /// Arguments:
    ///     filters
    ///
    pub fn get_notes(&self, filters: Option<SimpleFilter>) -> MalchimpIter<ListMemberNoteBuilder> {
        // GET /lists/{list_id}/members/{subscriber_hash}/notes
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/notes");

        let filter_params = if let Some(f) = filters {
            f
        } else {
            SimpleFilter::default()
        };

        match self
            ._api
            .get::<CollectionListMemberNote>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: ListMemberNoteBuilder {
                    endpoint: endpoint.clone(),
                },
                data: collection.notes,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self._api.clone(),
                endpoint: endpoint.clone(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get List Members: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: ListMemberNoteBuilder {
                        endpoint: endpoint.clone(),
                    },
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self._api.clone(),
                    endpoint: endpoint.clone(),
                }
            }
        }
    }

    ///
    /// Get details about subscribers’ recent activity.
    ///
    /// Argument:
    ///     note_id: The id for the note.
    ///
    pub fn get_specific_note<'a>(&self, note_id: &'a str) -> MailchimpResult<ListMemberNote> {
        // GET /lists/{list_id}/members/{subscriber_hash}/notes/{note_id}
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/notes/");
        endpoint.push_str(note_id);

        self._api.get::<ListMemberNote>(&endpoint, HashMap::new())
    }

    ///
    /// Add a new note for a specific subscriber.
    ///
    /// Argument:
    ///     note: The content of the note. Note length is limited to 1,000 characters.
    ///
    pub fn create_note<'a>(&self, note: &'a str) -> MailchimpResult<ListMemberNote> {
        // POST /lists/{list_id}/members/{subscriber_hash}/notes
        let mut endpoint = self.get_base_endpoint();
        endpoint.push_str("/notes");
        let mut payload = HashMap::new();
        payload.insert("note".to_string(), note.to_string());
        self._api
            .post::<ListMemberNote, HashMap<String, String>>(&endpoint, payload)
    }
}

///
/// Get information about members in a specific Mailchimp list.
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CollectionListMembers {
    /// An array of objects, each representing a specific list member.
    #[serde(default)]
    pub members: Vec<ListMember>,
    /// The unique id for the list.
    #[serde(default)]
    pub list_id: String,
    /// The total number of items matching the query regardless of pagination.
    #[serde(default)]
    pub total_items: u64,
    /// A list of link types and descriptions for the API schema documents.
    #[serde(default)]
    pub _links: Vec<LinkType>,
}

impl MailchimpCollection<ListMember> for CollectionListMembers {
    /// Total Items
    fn get_total_items(&self) -> u64 {
        self.total_items
    }

    /// Data
    fn get_values(&self) -> Vec<ListMember> {
        self.members.clone()
    }
}

impl Default for CollectionListMembers {
    fn default() -> Self {
        CollectionListMembers {
            list_id: String::new(),
            members: Vec::new(),
            total_items: 0,
            _links: Vec::new(),
        }
    }
}

/// ================================= ITER =====================

#[derive(Debug, Clone)]
pub struct ListMembersFilter {
    /// A comma-separated list of fields to return. Reference
    /// parameters of sub-objects with dot notation.
    pub fields: Option<String>,
    /// A comma-separated list of fields to exclude. Reference
    /// parameters of sub-objects with dot notation.
    pub exclude_fields: Option<String>,
    /// The number of records to return. Default value is 10.
    pub count: Option<u64>,
    /// The number of records from a collection to skip. Iterating over
    /// large collections with this parameter can be slow. Default value is 0..
    pub offset: Option<u64>,
    /// The email type.
    pub email_type: Option<String>,
    /// The subscriber’s status.
    pub status: Option<String>,
    /// Restrict results to subscribers who opted-in after the set timeframe.
    /// We recommend ISO 8601 time format: 2015-10-21T15:41:36+00:00.
    pub since_timestamp_opt: Option<String>,
    /// Restrict results to subscribers who opted-in before the set timeframe.
    /// We recommend ISO 8601 time format: 2015-10-21T15:41:36+00:00.
    pub before_timestamp_opt: Option<String>,
    /// Restrict results to subscribers whose information changed after the set timeframe.
    /// We recommend ISO 8601 time format: 2015-10-21T15:41:36+00:00.
    pub since_last_changed: Option<String>,
    /// Restrict results to subscribers whose information changed before the set timeframe.
    /// We recommend ISO 8601 time format: 2015-10-21T15:41:36+00:00.
    pub before_last_changed: Option<String>,
    /// A unique identifier for the email address across all Mailchimp lists.
    /// This parameter can be found in any links with Ecommerce Tracking enabled.
    pub unique_email_id: Option<String>,
    /// A filter to return only the list’s VIP members. Passing true will restrict results
    /// to VIP list members, passing false will return all list members.
    pub vip_only: Option<String>,
    /// The unique id for the interest category.
    pub interest_category_id: Option<String>,
    /// Used to filter list members by interests. Must be accompanied by interest_category_id
    /// and interest_match. The value must be a comma separated list of interest ids present for
    /// any supplied interest categories.
    pub interest_ids: Option<String>,
    /// Used to filter list members by interests. Must be accompanied by interest_category_id and
    /// interest_ids. “any” will match a member with any of the interest supplied, “all” will only
    /// match members with every interest supplied, and “none” will match members without any of the
    /// interest supplied.
    pub interest_match: Option<String>,
    /// Returns files sorted by the specified field. [timestamp_opt, timestamp_signup]
    pub sort_field: Option<String>,
    /// Determines the order direction for sorted results.
    pub sort_dir: Option<String>,
    /// Filter subscribers by those subscribed/unsubscribed/pending/cleaned since
    /// last email campaign send. Member status is required to use this filter.
    pub since_last_campaign: Option<bool>,
    /// Filter subscribers by those unsubscribed since a specific date. Using any status
    /// other than unsubscribed with this filter will result in an error.
    pub unsubscribed_since: Option<String>,
}

impl Default for ListMembersFilter {
    fn default() -> Self {
        ListMembersFilter {
            fields: None,
            exclude_fields: None,
            count: Some(50),
            offset: Some(0),
            email_type: None,
            status: None,
            since_timestamp_opt: None,
            before_timestamp_opt: None,
            since_last_changed: None,
            before_last_changed: None,
            unique_email_id: None,
            vip_only: None,
            interest_category_id: None,
            interest_ids: None,
            interest_match: None,
            sort_field: None,
            sort_dir: None,
            since_last_campaign: None,
            unsubscribed_since: None,
        }
    }
}

impl ResourceFilter for ListMembersFilter {
    fn build_payload(&self) -> HashMap<String, String> {
        let mut payload = HashMap::new();

        if self.fields.is_some() {
            payload.insert("fields".to_string(), self.fields.as_ref().unwrap().clone());
        }
        if self.exclude_fields.is_some() {
            payload.insert(
                "exclude_fields".to_string(),
                self.exclude_fields.as_ref().unwrap().clone(),
            );
        }
        if self.count.is_some() {
            payload.insert(
                "count".to_string(),
                format!("{:}", self.count.as_ref().unwrap().clone()),
            );
        }
        if self.offset.is_some() {
            payload.insert(
                "offset".to_string(),
                format!("{:}", self.offset.as_ref().unwrap().clone()),
            );
        }
        if self.email_type.is_some() {
            payload.insert(
                "email_type".to_string(),
                format!("{:}", self.email_type.as_ref().unwrap().clone()),
            );
        }
        if self.status.is_some() {
            payload.insert(
                "status".to_string(),
                format!("{:}", self.status.as_ref().unwrap().clone()),
            );
        }
        if self.since_timestamp_opt.is_some() {
            payload.insert(
                "since_timestamp_opt".to_string(),
                format!("{:}", self.since_timestamp_opt.as_ref().unwrap().clone()),
            );
        }
        if self.before_timestamp_opt.is_some() {
            payload.insert(
                "before_timestamp_opt".to_string(),
                format!("{:}", self.before_timestamp_opt.as_ref().unwrap().clone()),
            );
        }
        if self.since_last_changed.is_some() {
            payload.insert(
                "since_last_changed".to_string(),
                format!("{:}", self.since_last_changed.as_ref().unwrap().clone()),
            );
        }
        if self.before_last_changed.is_some() {
            payload.insert(
                "before_last_changed".to_string(),
                format!("{:}", self.before_last_changed.as_ref().unwrap().clone()),
            );
        }
        if self.unique_email_id.is_some() {
            payload.insert(
                "unique_email_id".to_string(),
                format!("{:}", self.unique_email_id.as_ref().unwrap().clone()),
            );
        }
        if self.vip_only.is_some() {
            payload.insert(
                "vip_only".to_string(),
                format!("{:}", self.vip_only.as_ref().unwrap().clone()),
            );
        }
        if self.interest_category_id.is_some() {
            payload.insert(
                "interest_category_id".to_string(),
                format!("{:}", self.interest_category_id.as_ref().unwrap().clone()),
            );
        }
        if self.interest_ids.is_some() {
            payload.insert(
                "interest_ids".to_string(),
                format!("{:}", self.interest_ids.as_ref().unwrap().clone()),
            );
        }
        if self.interest_match.is_some() {
            payload.insert(
                "interest_match".to_string(),
                format!("{:}", self.interest_match.as_ref().unwrap().clone()),
            );
        }
        if self.sort_field.is_some() {
            payload.insert(
                "sort_field".to_string(),
                format!("{:}", self.sort_field.as_ref().unwrap().clone()),
            );
        }
        if self.sort_dir.is_some() {
            payload.insert(
                "sort_dir".to_string(),
                format!("{:}", self.sort_dir.as_ref().unwrap().clone()),
            );
        }
        if self.since_last_campaign.is_some() {
            payload.insert(
                "since_last_campaign".to_string(),
                format!("{:}", self.since_last_campaign.as_ref().unwrap().clone()),
            );
        }
        if self.unsubscribed_since.is_some() {
            payload.insert(
                "unsubscribed_since".to_string(),
                format!("{:}", self.unsubscribed_since.as_ref().unwrap().clone()),
            );
        }
        payload
    }
}

#[derive(Debug)]
pub struct ListMembersBuilder {
    // Endpoint
    pub endpoint: String,
}

impl BuildIter for ListMembersBuilder {
    type Item = ListMember;
    type FilterItem = ListMembersFilter;
    type Collection = CollectionListMembers;

    ///
    /// Crea un recurso a partir del dato pasado por parámetro
    ///
    fn update_item(&self, data: &Self::Item, api: &MailchimpApi) -> Self::Item {
        let mut in_data = data.clone();
        in_data.set_api(api);
        in_data.set_endpoint(&self.endpoint);
        in_data
    }
    ///
    /// Actualiza el offset
    ///
    fn update_filter_offset(&self, filter: &Self::FilterItem) -> Self::FilterItem {
        let mut f = filter.clone();
        f.offset = Some(f.count.unwrap() + f.offset.unwrap());
        f
    }
}

///
/// ListMemberParams
///
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ListMemberParams {
    /// Email address for a subscriber.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email_address: Option<String>,
    /// Subscriber’s status. This value is required only if the email address is not
    /// already present on the list.
    /// Possible Values:
    ///     * subscribed
    ///     * unsubscribed
    ///     * cleaned
    ///     * pending
    ///     * transactional
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status_if_new: Option<String>,
    /// Type of email this member asked to get (‘html’ or ‘text’).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email_type: Option<String>,
    /// Subscriber’s current status.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub status: Option<String>,
    /// An individual merge var and value for a member.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub merge_fields: Option<HashMap<String, String>>,
    /// The key of this object’s properties is the ID of the interest in question.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub interests: Option<HashMap<String, String>>,
    /// If set/detected, the subscriber’s language.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// VIP status for subscriber.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub vip: Option<bool>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub location: Option<SubscriberLocation>,
    /// The marketing permissions for the subscriber.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub marketing_permissions: Option<Vec<ListMarketingPermision>>,
    /// The number of tags applied to this member.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tags_count: Option<u64>,
    /// The tags applied to this member.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tags: Option<Vec<String>>,
    /// IP address the subscriber signed up from.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_signup: Option<String>,
    /// The date and time the subscriber signed up for the list in ISO 8601 format.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp_signup: Option<String>,
    /// The IP address the subscriber used to confirm their opt-in status.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ip_opt: Option<String>,
    /// The date and time the subscribe confirmed their opt-in status in ISO 8601 format.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp_opt: Option<String>,
}