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
use super::api::MailchimpApi;
use super::internal::request::MailchimpResult;
use super::types::{
    CollectionConversations, Conversation, ConversationBuilder, ConversationsFilter,
};
use crate::iter::{MalchimpIter, ResourceFilter};
use log::error;
use std::collections::HashMap;

///
/// Conversations
///
/// Conversation tracking is a paid feature that lets you view subscribers’
/// replies to your campaigns in your Mailchimp account.
///
#[derive(Debug, Clone)]
pub struct Conversations {
    api: MailchimpApi,
}

impl Conversations {
    ///
    /// Argumentos:
    ///     api: MailchimpApi
    ///
    pub fn new(api: MailchimpApi) -> Self {
        Conversations { api: api }
    }

    ///
    /// Get a list of conversations
    ///
    pub fn get_conversations(
        &self,
        filter: Option<ConversationsFilter>,
    ) -> MalchimpIter<ConversationBuilder> {
        // GET /conversations
        let endpoint = "conversations";
        let mut filter_params = ConversationsFilter::default();

        if let Some(f) = filter {
            filter_params = f;
        }

        match self
            .api
            .get::<CollectionConversations>(&endpoint, filter_params.build_payload())
        {
            Ok(collection) => MalchimpIter {
                builder: ConversationBuilder {},
                data: collection.conversations,
                cur_filters: filter_params.clone(),
                cur_it: 0,
                total_items: collection.total_items,
                api: self.api.clone(),
                endpoint: endpoint.to_string(),
            },
            Err(e) => {
                error!( target: "mailchimp",  "Get Activities: Response Error details: {:?}", e);
                MalchimpIter {
                    builder: ConversationBuilder {},
                    data: Vec::new(),
                    cur_filters: filter_params.clone(),
                    cur_it: 0,
                    total_items: 0,
                    api: self.api.clone(),
                    endpoint: endpoint.to_string(),
                }
            }
        }
    }

    ///
    /// Get information about a conversation
    ///
    /// Get details about an individual conversation.
    ///
    pub fn get_conversation<'a>(&self, conversation_id: &'a str) -> MailchimpResult<Conversation> {
        let endpoint = format!("conversations/{}", conversation_id);
        let mut payload = HashMap::new();
        payload.insert("conversation_id".to_string(), conversation_id.to_string());
        self.api.get::<Conversation>(&endpoint, payload)
    }
}