/*
* Elastic Email REST API
*
* This API is based on the REST API architecture, allowing the user to easily manage their data with this resource-based approach. Every API call is established on which specific request type (GET, POST, PUT, DELETE) will be used. The API has a limit of 20 concurrent connections and a hard timeout of 600 seconds per request. To start using this API, you will need your Access Token (available <a target='_blank' href='https://app.elasticemail.com/marketing/settings/new/manage-api'>here</a>). Remember to keep it safe. Required access levels are listed in the given request’s description. Downloadable library clients can be found in our Github repository <a target='_blank' href='https://github.com/ElasticEmail?tab=repositories&q=%22rest+api%22+in%3Areadme'>here</a>
*
* The version of the OpenAPI document: 4.0.0
* Contact: support@elasticemail.com
* Generated by: https://openapi-generator.tech
*/
use reqwest;
use serde::{Deserialize, Serialize};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration};
/// struct for typed errors of method [`contacts_by_email_delete`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsByEmailDeleteError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_by_email_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsByEmailGetError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_by_email_put`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsByEmailPutError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_delete_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsDeletePostError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_export_by_id_status_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsExportByIdStatusGetError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_export_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsExportPostError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsGetError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_import_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsImportPostError {
UnknownValue(serde_json::Value),
}
/// struct for typed errors of method [`contacts_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ContactsPostError {
UnknownValue(serde_json::Value),
}
/// Deletes the provided contact. Required Access Level: ModifyContacts
pub async fn contacts_by_email_delete(configuration: &configuration::Configuration, email: &str) -> Result<(), Error<ContactsByEmailDeleteError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_email = email;
let uri_str = format!("{}/contacts/{email}", configuration.base_path, email=crate::apis::urlencode(p_email));
let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<ContactsByEmailDeleteError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Load detailed contact information for specified email. Required Access Level: ViewContacts
pub async fn contacts_by_email_get(configuration: &configuration::Configuration, email: &str) -> Result<models::Contact, Error<ContactsByEmailGetError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_email = email;
let uri_str = format!("{}/contacts/{email}", configuration.base_path, email=crate::apis::urlencode(p_email));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsByEmailGetError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Update selected contact. Omitted contact's fields will not be changed. Required Access Level: ModifyContacts
pub async fn contacts_by_email_put(configuration: &configuration::Configuration, email: &str, contact_update_payload: models::ContactUpdatePayload) -> Result<models::Contact, Error<ContactsByEmailPutError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_email = email;
let p_contact_update_payload = contact_update_payload;
let uri_str = format!("{}/contacts/{email}", configuration.base_path, email=crate::apis::urlencode(p_email));
let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
req_builder = req_builder.json(&p_contact_update_payload);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsByEmailPutError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Deletes provided contacts in bulk. Required Access Level: ModifyContacts
pub async fn contacts_delete_post(configuration: &configuration::Configuration, emails_payload: models::EmailsPayload) -> Result<(), Error<ContactsDeletePostError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_emails_payload = emails_payload;
let uri_str = format!("{}/contacts/delete", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
req_builder = req_builder.json(&p_emails_payload);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<ContactsDeletePostError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Check the current status of the export. Required Access Level: Export
pub async fn contacts_export_by_id_status_get(configuration: &configuration::Configuration, id: &str) -> Result<models::ExportStatus, Error<ContactsExportByIdStatusGetError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_id = id;
let uri_str = format!("{}/contacts/export/{id}/status", configuration.base_path, id=crate::apis::urlencode(p_id));
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsExportByIdStatusGetError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Request an Export of specified Contacts. Required Access Level: Export
pub async fn contacts_export_post(configuration: &configuration::Configuration, file_format: Option<models::ExportFileFormats>, rule: Option<&str>, emails: Option<Vec<String>>, compression_format: Option<models::CompressionFormat>, file_name: Option<&str>) -> Result<models::ExportLink, Error<ContactsExportPostError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_file_format = file_format;
let p_rule = rule;
let p_emails = emails;
let p_compression_format = compression_format;
let p_file_name = file_name;
let uri_str = format!("{}/contacts/export", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref param_value) = p_file_format {
req_builder = req_builder.query(&[("fileFormat", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_rule {
req_builder = req_builder.query(&[("rule", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_emails {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("emails".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("emails", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref param_value) = p_compression_format {
req_builder = req_builder.query(&[("compressionFormat", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_file_name {
req_builder = req_builder.query(&[("fileName", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsExportPostError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Returns a list of contacts. Required Access Level: ViewContacts
pub async fn contacts_get(configuration: &configuration::Configuration, limit: Option<i32>, offset: Option<i32>) -> Result<Vec<models::Contact>, Error<ContactsGetError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_limit = limit;
let p_offset = offset;
let uri_str = format!("{}/contacts", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
if let Some(ref param_value) = p_limit {
req_builder = req_builder.query(&[("limit", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_offset {
req_builder = req_builder.query(&[("offset", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsGetError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Upload contacts from a file. Required Access Level: ModifyContacts
pub async fn contacts_import_post(configuration: &configuration::Configuration, list_name: Option<&str>, encoding_name: Option<&str>, file_url: Option<&str>, file: Option<std::path::PathBuf>) -> Result<(), Error<ContactsImportPostError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_list_name = list_name;
let p_encoding_name = encoding_name;
let p_file_url = file_url;
let p_file = file;
let uri_str = format!("{}/contacts/import", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref param_value) = p_list_name {
req_builder = req_builder.query(&[("listName", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_encoding_name {
req_builder = req_builder.query(&[("encodingName", ¶m_value.to_string())]);
}
if let Some(ref param_value) = p_file_url {
req_builder = req_builder.query(&[("fileUrl", ¶m_value.to_string())]);
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
let mut multipart_form = reqwest::multipart::Form::new();
// TODO: support file upload for 'file' parameter
req_builder = req_builder.multipart(multipart_form);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
Ok(())
} else {
let content = resp.text().await?;
let entity: Option<ContactsImportPostError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}
/// Add new Contacts to your Lists. Up to 1000 can be added (for more please refer to the import request). Required Access Level: ModifyContacts
pub async fn contacts_post(configuration: &configuration::Configuration, contact_payload: Vec<models::ContactPayload>, listnames: Option<Vec<String>>) -> Result<Vec<models::Contact>, Error<ContactsPostError>> {
// add a prefix to parameters to efficiently prevent name collisions
let p_contact_payload = contact_payload;
let p_listnames = listnames;
let uri_str = format!("{}/contacts", configuration.base_path);
let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
if let Some(ref param_value) = p_listnames {
req_builder = match "multi" {
"multi" => req_builder.query(¶m_value.into_iter().map(|p| ("listnames".to_owned(), p.to_string())).collect::<Vec<(std::string::String, std::string::String)>>()),
_ => req_builder.query(&[("listnames", ¶m_value.into_iter().map(|p| p.to_string()).collect::<Vec<String>>().join(",").to_string())]),
};
}
if let Some(ref user_agent) = configuration.user_agent {
req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
}
if let Some(ref apikey) = configuration.api_key {
let key = apikey.key.clone();
let value = match apikey.prefix {
Some(ref prefix) => format!("{} {}", prefix, key),
None => key,
};
req_builder = req_builder.header("X-ElasticEmail-ApiKey", value);
};
req_builder = req_builder.json(&p_contact_payload);
let req = req_builder.build()?;
let resp = configuration.client.execute(req).await?;
let status = resp.status();
if !status.is_client_error() && !status.is_server_error() {
let content = resp.text().await?;
serde_json::from_str(&content).map_err(Error::from)
} else {
let content = resp.text().await?;
let entity: Option<ContactsPostError> = serde_json::from_str(&content).ok();
Err(Error::ResponseError(ResponseContent { status, content, entity }))
}
}