use std::error::Error;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Page {
pub id: Option<u64>,
pub name: Option<String>,
pub parent_id: Option<u64>,
pub owner_id: Option<u64>,
pub locked: Option<bool>,
pub collection_ids: Option<Vec<u64>>,
pub card_ids: Option<Vec<u64>>,
pub children: Option<Vec<Page>>,
pub visibility: Option<Visibility>,
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Visibility {
pub user_ids: Option<Vec<u64>>,
pub group_ids: Option<Vec<u64>>,
}
impl Page {
pub fn new() -> Self {
Self {
id: None,
name: None,
parent_id: None,
owner_id: None,
locked: None,
collection_ids: None,
card_ids: None,
children: None,
visibility: None,
}
}
pub fn template() -> Self {
Self {
id: Some(0),
name: Some(String::from("Page Name")),
parent_id: Some(0),
owner_id: Some(0),
locked: Some(false),
collection_ids: Some(vec![1, 2, 3]),
card_ids: Some(vec![1, 2, 3]),
children: Some(vec![]),
visibility: Some(Visibility {
user_ids: Some(vec![1, 2, 3]),
group_ids: Some(vec![1, 2, 3]),
}),
}
}
}
#[derive(Serialize, Deserialize, Debug, Default)]
#[serde(default, rename_all = "camelCase")]
pub struct Collection {
pub id: Option<u64>,
pub title: Option<String>,
pub description: Option<String>,
pub card_ids: Option<Vec<u64>>,
}
impl Collection {
pub fn new() -> Self {
Self {
id: None,
title: None,
description: None,
card_ids: None,
}
}
pub fn template() -> Self {
Self {
id: Some(0),
title: Some(String::from("Collection Title")),
description: Some(String::from("Collection Description")),
card_ids: Some(vec![1, 2, 3]),
}
}
}
impl super::Client {
pub async fn get_pages(
&self,
limit: Option<u32>,
offset: Option<u32>,
) -> Result<Vec<Page>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").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/pages"))
.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_page(
&self,
page: Page,
) -> Result<Page, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::post(&format!("{}{}", self.host, "/v1/pages"))
.header("Authorization", at)
.body(surf::Body::from_json(&page)?)
.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_page(&self, id: u64) -> Result<Page, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::get(&format!("{}{}{}", self.host, "/v1/pages/", 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_page(
&self,
id: u64,
page: Page,
) -> Result<Page, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::put(&format!("{}{}{}", self.host, "/v1/pages/", id))
.header("Authorization", at)
.body(surf::Body::from_json(&page)?)
.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_page(&self, id: u64) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::delete(&format!("{}{}{}", self.host, "/v1/pages/", 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 get_page_collections(
&self,
id: u64,
) -> Result<Vec<Collection>, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::get(&format!(
"{}{}{}{}",
self.host, "/v1/pages/", id, "/collections"
))
.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_page_collection(
&self,
id: u64,
collection: Collection,
) -> Result<Collection, Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::post(&format!(
"{}{}{}{}",
self.host, "/v1/pages/", id, "/collections"
))
.header("Authorization", at)
.body(surf::Body::from_json(&collection)?)
.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_page_collection(
&self,
id: u64,
collection_id: u64,
collection: Collection,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::put(&format!(
"{}{}{}{}{}",
self.host, "/v1/pages/", id, "/collections/", collection_id
))
.header("Authorization", at)
.body(surf::Body::from_json(&collection)?)
.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_page_collection(
&self,
id: u64,
collection_id: u64,
) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let at = self.get_access_token("dashboard").await?;
let mut response = surf::delete(&format!(
"{}{}{}{}{}",
self.host, "/v1/pages/", id, "/collections/", collection_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?)
}
}