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
use crate::automation_workflow_resource::{
    AutomationWorkflowResource, AutomationWorkflowResources,
};
use crate::internal::request::MailchimpResult;
use crate::internal::types::{
    ApiRootType, AuthorizedAppType, AuthorizedAppsType, AutomationCampaignSettingsType,
    AutomationModifier, AutomationTriggerType, AutomationWorkflowType, AutomationsType,
    CreatedAuthorizedAppType, RecipientType,
};
use crate::MailchimpApi;

use std::collections::HashMap;

///
/// Implementación del cliente para el API
///
/// # Ejemplo
///
/// En este ejemplo se imrpimen el tricker actual de todos los mercados disponibles
///
/// ```
/// extern crate mailchimp;
/// use mailchimp::MailchimpClient;
///
/// const DC: &'static str = "<DC>";
/// const API_KEY: &'static str = "<API KEY>";
///
/// let client = MailchimpClient::new(API_KEY, API_KEY);
/// ```
///
pub struct MailchimpClient {
    api: MailchimpApi,
}

impl MailchimpClient {
    ///
    /// Crea una instancia pasandole como parámetros el Mailchimp Datacentery el API Key
    ///
    /// Argumentos:
    ///     dc: Datacenter
    ///     api_key: API KEY
    ///
    pub fn new<'a>(dc: &'a str, api_key: &'a str) -> Self {
        MailchimpClient {
            api: MailchimpApi::new(dc, api_key),
        }
    }
    ///
    /// Devuelve detalles de la cuenta de usuario, asi como los links a los recursos asociados
    ///
    /// Argumentos:
    ///     filters: Filtros que requieras aplicar a la hora de obtener las aplicaciones
    ///         fields: Una lista de campos separados por comas para devolver.
    ///             Parámetros de referencia de subobjetos con notación de puntos.
    ///         exclude_fields: Una lista de campos separados por comas para excluir.
    ///            Parámetros de referencia de subobjetos con notación de puntos.
    ///
    pub fn get_account_info(
        &self,
        filters: HashMap<String, String>,
    ) -> MailchimpResult<ApiRootType> {
        let resp = self.api.get::<ApiRootType>("", filters);
        match resp {
            Ok(value) => Ok(value),
            Err(e) => Err(e),
        }
    }
    ///
    ///  ===================== AUTHORIZED ==================
    ///
    ///
    /// Devuelve una lista de las aplicaciones conectadas y registradas de una cuenta.
    ///
    /// Argumentos:
    ///     filters: Filtros que requieras aplicar a la hora de obtener las aplicaciones
    ///         fields: Una lista de campos separados por comas para devolver.
    ///             Parámetros de referencia de subobjetos con notación de puntos.
    ///         exclude_fields: Una lista de campos separados por comas para excluir.
    ///            Parámetros de referencia de subobjetos con notación de puntos.
    ///         count: Cantidad de registros a devolver
    ///         offset: El número de registros de una colección a omitir. Por defecto es 0
    ///
    pub fn get_authorized_apps(
        &self,
        filters: HashMap<String, String>,
    ) -> MailchimpResult<Vec<AuthorizedAppType>> {
        let resp = self
            .api
            .get::<AuthorizedAppsType>("authorized-apps", filters);
        match resp {
            Ok(value) => Ok(value.apps.clone()),
            Err(e) => Err(e),
        }
    }
    ///
    /// Obtiene las credenciales basadas en OAuth2 para asociar las llamadas a
    /// la API con su aplicación.
    ///
    /// Argumentos:
    ///     client_id: Id o nombre de usuario único para la autorización
    ///     client_secret: Contraseña del cliente para la autorización
    ///
    pub fn link_authorized_apps<'a>(
        &self,
        client_id: &'a str,
        client_secret: &'a str,
    ) -> MailchimpResult<CreatedAuthorizedAppType> {
        let mut payload = HashMap::new();
        payload.insert("client_id".to_string(), client_id.to_string());
        payload.insert("client_secret".to_string(), client_secret.to_string());

        let resp = self
            .api
            .post::<CreatedAuthorizedAppType, HashMap<String, String>>("authorized-apps", payload);

        match resp {
            Ok(value) => Ok(value.clone()),
            Err(e) => Err(e),
        }
    }
    ///
    ///
    /// Devuelve una lista de las aplicaciones conectadas y registradas de una cuenta.
    ///
    /// Argumentos:
    ///     app_id: identificador de la aplicación autorizada
    ///     filters: Filtros que requieras aplicar a la hora de obtener las aplicaciones
    ///         fields: Una lista de campos separados por comas para devolver.
    ///             Parámetros de referencia de subobjetos con notación de puntos.
    ///         exclude_fields: Una lista de campos separados por comas para excluir.
    ///            Parámetros de referencia de subobjetos con notación de puntos.
    ///
    pub fn get_authorized_app_info<'a>(
        &self,
        app_id: &'a str,
        filters: HashMap<String, String>,
    ) -> MailchimpResult<AuthorizedAppType> {
        let endpoint = String::from("authorized-apps/") + app_id;
        let resp = self
            .api
            .get::<AuthorizedAppType>(endpoint.as_str(), filters);
        match resp {
            Ok(value) => Ok(value.clone()),
            Err(e) => Err(e),
        }
    }

    ///
    ///  ===================== ACCOUNTS AUTOMATION ==================
    ///
    /// Devuelve un resumen de las automatizaciones de una cuenta.
    ///
    /// Argumentos:
    ///     filters: Filtros que se requieran aplicar a la hora de obtener las automatizaciones
    ///         Estos filtros se deben pasar en forma de llave, valor donde las llaves puede ser
    ///         cualquiera de los siguientes:
    ///         fields: Una lista de campos separados por comas para devolver.
    ///             Parámetros de referencia de subobjetos con notación de puntos.
    ///         exclude_fields: Una lista de campos separados por comas para excluir.
    ///            Parámetros de referencia de subobjetos con notación de puntos.
    ///         before_create_time: Restringe la respuesta a las automatizaciones creadas
    ///             antes del tiempo establecido. Recomendamos el formato de hora
    ///             ISO 8601: 2015-10-21T15: 41: 36 + 00: 00.
    ///         since_create_time: Restringe la respuesta a las automatizaciones creadas
    ///             después del tiempo establecido. Recomendamos el formato de hora
    ///             ISO 8601: 2015-10-21T15: 41: 36 + 00: 00.
    ///         before_send_time: Restringe la respuesta a las automatizaciones enviadas
    ///             antes del tiempo establecido. Recomendamos el formato de hora
    ///             ISO 8601: 2015-10-21T15: 41: 36 + 00: 00.
    ///         since_send_time: Restringe la respuesta a las automatizaciones enviadas después
    ///             del tiempo establecido. Recomendamos el formato de hora
    ///             ISO 8601: 2015-10-21T15: 41: 36 + 00: 00.
    ///         status: Restringe los resultados a automatizaciones con el estado especificado.
    ///
    pub fn get_account_automations(
        &self,
        filters: HashMap<String, String>,
    ) -> MailchimpResult<AutomationWorkflowResources> {
        let response = self.api.get::<AutomationsType>("automations", filters);

        match response {
            Ok(value) => {
                let automations = value
                    .automations
                    .iter()
                    .map(move |data| AutomationWorkflowResource::new(self.api.clone(), &data))
                    .collect::<AutomationWorkflowResources>();
                Ok(automations)
            }
            Err(e) => Err(e),
        }
    }
    ///
    /// Devuelve la informacion de la automatizacion especificada
    ///
    /// Argumentos:
    ///     workflow_id: Identificador único de la automatización
    ///     filters: Filtros requeridos a la hora de obtener las automatizaciones
    ///         Estos filtros se deben pasar en forma de llave, valor donde las llaves puede ser
    ///         cualquiera de los siguientes:
    ///         fields: Una lista de campos separados por comas para devolver.
    ///             Parámetros de referencia de subobjetos con notación de puntos.
    ///         exclude_fields: Una lista de campos separados por comas para excluir.
    ///            Parámetros de referencia de subobjetos con notación de puntos.
    ///
    pub fn get_automation_workflow_info<'a>(
        &self,
        workflow_id: &'a str,
        filters: HashMap<String, String>,
    ) -> MailchimpResult<AutomationWorkflowResource> {
        let endpoint = String::from("automations/") + workflow_id;
        let response = self
            .api
            .get::<AutomationWorkflowType>(endpoint.as_str(), filters);

        match response {
            Ok(automation) => Ok(AutomationWorkflowResource::new(
                self.api.clone(),
                &automation,
            )),
            Err(e) => Err(e),
        }
    }
    ///
    ///  Crea una automatización
    ///
    /// Argumentos:
    ///     recipients: Contenedores para esta automatización
    ///     trigger_settings: Configuracion de los disparadores
    ///     settings: Configuracion de la automatización a crear
    ///
    pub fn create_automation<'a>(
        &self,
        recipients: RecipientType,
        trigger_settings: AutomationTriggerType,
        settings: Option<AutomationCampaignSettingsType>,
    ) -> MailchimpResult<AutomationWorkflowResource> {
        let modifier = AutomationModifier {
            settings: settings,
            delay: None,
            recipients: Some(recipients),
            trigger_settings: Some(trigger_settings),
        };
        let response = self
            .api
            .post::<AutomationWorkflowType, AutomationModifier>("automations", modifier);
        match response {
            Ok(automation) => Ok(AutomationWorkflowResource::new(
                self.api.clone(),
                &automation,
            )),
            Err(e) => Err(e),
        }
    }
}