use std::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct User {
pub id: Option<u64>,
pub name: Option<String>,
pub email: Option<String>,
pub alternate_email: Option<String>,
pub employee_id: Option<String>,
pub employee_number: Option<u64>,
pub title: Option<String>,
pub phone: Option<String>,
pub location: Option<String>,
pub department: Option<String>,
pub timezone: Option<String>,
pub locale: Option<String>,
pub role: Option<String>,
pub role_id: Option<u64>,
pub deleted: Option<bool>,
}
impl User {
pub fn new() -> Self {
User {
id: None,
name: None,
email: None,
alternate_email: None,
employee_number: None,
employee_id: None,
title: None,
phone: None,
location: None,
department: None,
timezone: None,
locale: None,
role: None,
role_id: None,
deleted: None,
}
}
pub fn template() -> Self {
User {
id: Some(0),
name: Some(String::from("First Last")),
email: Some(String::from("First.Last@company.com")),
alternate_email: Some(String::from("first.last@gmail.com")),
employee_number: Some(0),
employee_id: Some(String::from("employee id")),
title: Some(String::from("Title")),
phone: Some(String::from("+1 (800) 700-6000")),
location: Some(String::from("CA")),
department: Some(String::from("department")),
timezone: Some(String::from("America/Los_Angeles")),
locale: Some(String::from("en-US")),
role: Some(String::from("Admin - Match roles defined in instance")),
role_id: Some(0),
deleted: Some(false),
}
}
}
impl super::Client {
pub async fn get_users(
&self,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<Vec<User>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
#[derive(Serialize)]
struct QueryParams {
pub limit: Option<u32>,
pub offset: Option<u32>,
}
let q = QueryParams { limit, offset };
let mut response = surf::get(&format!("{}{}", self.host, "/v1/users"))
.query(&q)?
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn post_bulk_user_emails(
&self,
emails: &[String],
) -> Result<Vec<User>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
let mut response = surf::post(&format!("{}{}", self.host, "/v1/users/bulk/emails"))
.header("Authorization", at)
.body(surf::Body::from_json(&emails)?)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn post_user(
&self,
user: User,
) -> Result<User, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
let mut response = surf::post(&format!("{}{}", self.host, "/v1/users"))
.header("Authorization", at)
.body(surf::Body::from_json(&user)?)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn get_user(&self, id: &str) -> Result<User, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
let mut response = surf::get(&format!("{}{}{}", self.host, "/v1/users/", id))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn put_user(
&self,
id: &str,
user: User,
) -> Result<User, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
let mut response = surf::put(&format!("{}{}{}", self.host, "/v1/users/", id))
.header("Authorization", at)
.body(surf::Body::from_json(&user)?)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
pub async fn delete_user(
&self,
id: &str,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("user").await?;
let mut response = surf::delete(&format!("{}{}{}", self.host, "/v1/users/", id))
.header("Authorization", at)
.await?;
if !response.status().is_success() {
let e: Box<super::PubAPIError> = response.body_json().await?;
return Err(e);
}
Ok(response.body_json().await?)
}
}