use super::api::{MailchimpApi, MailchimpApiUpdate};
use super::internal::request::MailchimpResult;
use super::iter::{BuildIter, MalchimpIter, ResourceFilter};
use crate::types::{AuthorizedAppType, AuthorizedAppsType, CreatedAuthorizedAppType};
use log::error;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(Debug, Clone)]
pub struct AuthorizedFilter {
pub fields: Option<String>,
pub exclude_fields: Option<String>,
pub count: Option<u64>,
pub offset: Option<u64>,
}
impl Default for AuthorizedFilter {
fn default() -> Self {
AuthorizedFilter {
fields: None,
exclude_fields: None,
count: Some(50),
offset: Some(0),
}
}
}
impl ResourceFilter for AuthorizedFilter {
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()),
);
}
payload
}
}
#[derive(Debug, Clone)]
pub struct AuthorizedApps {
api: Rc<MailchimpApi>,
}
#[derive(Debug)]
pub struct AuthorizedAppsBuilder {}
impl BuildIter for AuthorizedAppsBuilder {
type Item = AuthorizedAppType;
type FilterItem = AuthorizedFilter;
type Collection = AuthorizedAppsType;
fn update_item(&self, data: &Self::Item, api: Rc<MailchimpApi>) -> Self::Item {
let mut in_data = data.clone();
in_data.set_api(api);
in_data
}
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
}
}
impl AuthorizedApps {
pub fn new(api: MailchimpApi) -> Self {
AuthorizedApps { api: Rc::new(api) }
}
pub fn get_authorized_apps_from_remote(
&self,
filters: Option<&AuthorizedFilter>,
) -> Option<AuthorizedAppsType> {
let mut payload = HashMap::new();
if filters.is_some() {
payload = filters.as_ref().unwrap().build_payload();
}
let response = self
.api
.get::<AuthorizedAppsType>("authorized-apps", payload);
match response {
Ok(value) => Some(value),
Err(e) => {
error!( target: "mailchimp", "Load Authorized Apps from remote: Response Error details: {:?}", e);
None
}
}
}
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),
}
}
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),
}
}
pub fn iter(&self, filters: AuthorizedFilter) -> MalchimpIter<AuthorizedAppsBuilder> {
if let Some(remote) = self.get_authorized_apps_from_remote(Some(&filters)) {
return MalchimpIter {
builder: AuthorizedAppsBuilder {},
data: remote.apps,
cur_filters: filters.clone(),
cur_it: 0,
total_items: remote.total_items,
api: self.api.clone(),
endpoint: "authorized-apps".to_string(),
};
}
MalchimpIter {
builder: AuthorizedAppsBuilder {},
data: Vec::new(),
cur_filters: filters.clone(),
cur_it: 0,
total_items: 0,
api: self.api.clone(),
endpoint: "authorized-apps".to_string(),
}
}
}