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
use crate::internal::api::Api;
use crate::internal::error_type::MailchimpErrorType;
use crate::internal::request::MailchimpRequest;
use serde::de::DeserializeOwned;
use serde::ser::Serialize;
use std::collections::HashMap;
///
/// Mailchimp API
///
/// Permite el acceso al API de Mailchimp si conoces bien los diferentes
/// endpoints.
/// Para más información sobre los endpoints ir a la página de desarrollos
/// mailchimp [Developers Mailchimp](https://developer.mailchimp.com/)
///
/// ## Ejemplo
///
/// ```
/// extern crate mailchimp;
/// use mailchimp::MailchimpApi;
///
/// let api = MailchimpApi::new("<API Key>");
/// println!("Api version: {}", api.version());
/// println!("Api domain: {}", api.domain());
/// ```
///
#[derive(Debug, Clone)]
pub struct MailchimpApi {
i_api: Box<Api<MailchimpRequest>>,
}
impl MailchimpApi {
///
/// Crea la nueva instancia del API
///
/// Argumentos
/// api_key: Mailchimp API KEY
/// http_transport: Interfaz por donde se harían las peticiones Get y Post al servicio
pub fn new<'a>(api_key: &'a str) -> Self {
let mut creds = api_key.split('-').collect::<Vec<&str>>();
if creds.len() <= 1 {
creds.push("usX");
}
MailchimpApi {
i_api: Box::new(Api::<MailchimpRequest>::new(
creds[1],
creds[0],
Box::new(MailchimpRequest::new()),
)),
}
}
///
/// Devuelve el dominio
///
pub fn domain(&self) -> String {
self.i_api.domain()
}
///
/// Devuelve la version del API
///
pub fn version(&self) -> String {
self.i_api.api_version()
}
///
/// Realiza una petición de tipo POST
/// ```
/// extern crate mailchimp;
/// use std::collections::HashMap;
/// use mailchimp::MailchimpApi;
/// use mailchimp::types::AuthorizedAppsType;
/// fn main() {
/// let api = MailchimpApi::new("aac1e319006883125e18a89e529b5abb73de4c81-usX");
/// let data = api.post::<AuthorizedAppsType, HashMap<String, String>>("authorized-apps", HashMap::new());
/// match data {
/// Ok(resp) => {
/// for app in resp.apps.iter() {
/// println!("{:?}", app)
/// }
/// },
/// Err(e) => println!("Error Title: {:?} \n Error detail {:?}", e.title, e.detail)
/// }
/// }
/// ```
/// #Argumentos
/// `endpoint`: Cadena de texto con el endpoint de la API al que se requiere acceder, no debe comenzar por "/"
/// `payload`: Dato a enviar al servidor
///
pub fn post<'a, T, P>(&self, endpoint: &'a str, payload: P) -> Result<T, MailchimpErrorType>
where
T: DeserializeOwned,
P: Serialize,
{
self.i_api.post_edge::<T, P>(endpoint, payload)
}
///
/// Función para actualizar los recursos en el servidor
///
///
/// #Argumentos
/// `endpoint`: Cadena de texto con el endpoint de la API al que se requiere acceder, no debe comenzar por "/"
/// `payload`: Dato a enviar al servidor
///
pub fn patch<'a, T, P>(&self, endpoint: &'a str, payload: P) -> Result<T, MailchimpErrorType>
where
T: DeserializeOwned,
P: Serialize,
{
self.i_api.patch_edge::<T, P>(endpoint, payload)
}
///
/// Función para actualizar los recursos en el servidor
///
///
/// #Argumentos
/// `endpoint`: Cadena de texto con el endpoint de la API al que se requiere acceder, no debe comenzar por "/"
/// `payload`: Dato a enviar al servidor
///
pub fn put<'a, T, P>(&self, endpoint: &'a str, payload: P) -> Result<T, MailchimpErrorType>
where
T: DeserializeOwned,
P: Serialize,
{
self.i_api.put_edge::<T, P>(endpoint, payload)
}
///
/// Realiza una petición de tipo GET
/// ```
/// extern crate mailchimp;
/// use std::collections::HashMap;
/// use mailchimp::MailchimpApi;
/// use mailchimp::types::AuthorizedAppType;
///
/// fn main() {
/// let api = MailchimpApi::new("aac1e319006883125e18a89e529b5abb73de4c81-usX");
/// let mut params = HashMap::new();
/// params.insert("client_id".to_string(), "".to_string());
/// params.insert("client_secret".to_string(), "".to_string());
/// let data = api.get::<AuthorizedAppType>("authorized-apps", params);
/// match data {
/// Ok(resp) => {
/// println!("{:?}", resp)
/// },
/// Err(e) => println!("Error Title: {:?} \n Error detail {:?}", e.title, e.detail)
/// }
/// }
/// ```
/// #Argumentos
/// `endpoint`: Cadena de texto con el endpoint de la API al que se requiere acceder, no debe comenzar por "/"
/// `payload`: Listado llave valor de los parametros o data
///
pub fn get<'a, T>(
&self,
endpoint: &'a str,
payload: HashMap<String, String>,
) -> Result<T, MailchimpErrorType>
where
T: DeserializeOwned,
{
self.i_api.get_edge(endpoint, payload)
}
///
/// Realiza una petición de tipo GET
/// ```
/// extern crate mailchimp;
/// use std::collections::HashMap;
/// use mailchimp::MailchimpApi;
/// use mailchimp::types::AuthorizedAppType;
///
/// fn main() {
/// let api = MailchimpApi::new("aac1e319006883125e18a89e529b5abb73de4c81-usX");
/// let mut params = HashMap::new();
/// params.insert("client_id".to_string(), "".to_string());
/// params.insert("client_secret".to_string(), "".to_string());
/// let data = api.get::<AuthorizedAppType>("authorized-apps", params);
/// match data {
/// Ok(resp) => {
/// println!("{:?}", resp)
/// },
/// Err(e) => println!("Error Title: {:?} \n Error detail {:?}", e.title, e.detail)
/// }
/// }
/// ```
/// #Argumentos
/// `endpoint`: Cadena de texto con el endpoint de la API al que se requiere acceder, no debe comenzar por "/"
/// `payload`: Listado llave valor de los parametros o data
///
pub fn delete<'a, T>(
&self,
endpoint: &'a str,
payload: HashMap<String, String>,
) -> Result<T, MailchimpErrorType>
where
T: DeserializeOwned,
{
self.i_api.delete_edge(endpoint, payload)
}
}
impl Default for MailchimpApi {
fn default() -> Self {
MailchimpApi {
i_api: Box::new(Api::<MailchimpRequest>::new(
"",
"",
Box::new(MailchimpRequest::new()),
)),
}
}
}
pub trait MailchimpApiUpdate {
/**
* Update API
*/
fn set_api(&mut self, api: &MailchimpApi);
}