use serde::{Deserialize, Serialize};
use std::{collections::HashMap, error::Error};
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Account {
pub id: Option<String>,
pub name: Option<String>,
pub valid: Option<bool>,
#[serde(rename = "type")]
pub account_type: Option<AccountType>,
}
impl Account {
pub fn new() -> Self {
Account {
id: None,
name: None,
valid: None,
account_type: None,
}
}
pub fn template() -> Self {
Account {
id: Some(String::from("0")),
name: Some(String::from("Account Name")),
valid: Some(true),
account_type: None,
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct AccountType {
pub id: Option<String>,
pub name: Option<String>,
pub properties: Option<HashMap<String, String>>,
#[serde(rename = "_templates")]
pub templates: Option<HashMap<String, AccountTemplate>>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct AccountTemplate {
pub name: Option<String>,
pub title: Option<String>,
pub content_type: Option<String>,
pub method: Option<String>,
pub properties: Option<Vec<Property>>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Property {
pub name: Option<String>,
pub prompt: Option<String>,
pub regex: Option<String>,
pub required: Option<bool>,
}
#[derive(Serialize)]
struct ListParams {
pub limit: Option<u32>,
pub offset: Option<u32>,
}
impl super::Client {
pub async fn get_accounts(
&self,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<Vec<Account>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let q = ListParams { limit, offset };
let mut response = surf::get(&format!("{}{}", self.host, "/v1/accounts"))
.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_account(
&self,
account: Account,
) -> Result<Account, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let mut response = surf::post(&format!("{}{}", self.host, "/v1/accounts"))
.header("Authorization", at)
.body(surf::Body::from_json(&account)?)
.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_account(
&self,
id: &str,
) -> Result<Account, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let mut response = surf::get(&format!("{}{}{}", self.host, "/v1/accounts/", 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 patch_account(
&self,
id: &str,
account: Account,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let mut response = surf::patch(&format!("{}{}{}", self.host, "/v1/accounts/", id))
.header("Authorization", at)
.body(surf::Body::from_json(&account)?)
.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_account(
&self,
id: &str,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let mut response = surf::delete(&format!("{}{}{}", self.host, "/v1/accounts/", 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 post_account_share(
&self,
account_id: &str,
user_id: u64,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
struct User {
id: u64,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default)]
struct Share {
user: User,
}
let obj: Share = Share {
user: User { id: user_id },
};
let mut response = surf::post(&format!(
"{}{}{}{}",
self.host, "/v1/accounts/", account_id, "/shares"
))
.header("Authorization", at)
.body(surf::Body::from_json(&obj)?)
.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_account_types(
&self,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<Vec<AccountType>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let q = ListParams { limit, offset };
let mut response = surf::get(&format!("{}{}", self.host, "/v1/account-types"))
.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 get_account_type(
&self,
id: &str,
) -> Result<AccountType, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("account").await?;
let mut response = surf::get(&format!("{}{}{}", self.host, "/v1/account-types/", 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?)
}
}