use serde_json;
use chrono::{DateTime, Utc};
use error::HelpScoutError;
use client::Client;
use envelope::{Collection, Item};
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Mailbox {
pub id: i32,
pub name: String,
pub slug: String,
pub email: String,
pub created_at: DateTime<Utc>,
pub modified_at: DateTime<Utc>,
pub custom_fields: Option<Vec<CustomField>>,
pub folders: Option<Vec<Folder>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MailboxRef {
pub id: i32,
pub name: String,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum CustomFieldType {
SingleLine,
MultiLine,
Data,
Number,
Dropdown,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomField {
pub id: i32,
pub field_name: String,
pub field_type: CustomFieldType,
pub required: bool,
pub order: i32,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Folder {
pub id: i32,
pub name: String,
#[serde(rename = "type")]
pub folder_type: String,
pub user_id: i32,
pub total_count: i32,
pub active_count: i32,
pub modified_at: DateTime<Utc>,
}
pub fn list(client: &Client) -> Result<Collection<Mailbox>, HelpScoutError> {
let res = client.get("mailboxes.json", ())?;
let mailboxes = serde_json::from_value(res.clone())?;
Ok(mailboxes)
}
pub fn get(client: &Client, id: i32) -> Result<Item<Mailbox>, HelpScoutError> {
let res = client.get(&format!("mailboxes/{}.json", id), ())?;
let mailbox = serde_json::from_value(res.clone())?;
Ok(mailbox)
}
pub fn get_folders(client: &Client, mailbox_id:i32) -> Result<Collection<Folder>, HelpScoutError>{
let res = client.get(&format!("mailboxes/{}/folders.json", mailbox_id), ())?;
let folders = serde_json::from_value(res.clone())?;
Ok(folders)
}