use reqwest::Method;
use crate::error::Result;
use crate::models::{
AccountRole, BatchDeleteNotebooksRequest, BatchDeleteNotebooksResponse, CreateNotebookRequest,
ListRecentlyViewedResponse, Notebook, ShareRequest, ShareResponse,
};
use crate::client::NblmClient;
const PAGE_SIZE_MIN: u32 = 1;
const PAGE_SIZE_MAX: u32 = 500;
impl NblmClient {
pub async fn create_notebook(&self, title: impl Into<String>) -> Result<Notebook> {
let url = self
.url_builder
.build_url(&self.url_builder.notebooks_collection())?;
let request = CreateNotebookRequest {
title: title.into(),
};
self.http
.request_json(Method::POST, url, Some(&request))
.await
}
pub async fn batch_delete_notebooks(
&self,
request: BatchDeleteNotebooksRequest,
) -> Result<BatchDeleteNotebooksResponse> {
let path = format!("{}:batchDelete", self.url_builder.notebooks_collection());
let url = self.url_builder.build_url(&path)?;
self.http
.request_json(Method::POST, url, Some(&request))
.await
}
pub async fn delete_notebooks(
&self,
notebook_names: Vec<String>,
) -> Result<BatchDeleteNotebooksResponse> {
for name in ¬ebook_names {
let request = BatchDeleteNotebooksRequest {
names: vec![name.clone()],
};
self.batch_delete_notebooks(request).await?;
}
Ok(BatchDeleteNotebooksResponse::default())
}
pub async fn share_notebook(
&self,
notebook_id: &str,
accounts: Vec<AccountRole>,
) -> Result<ShareResponse> {
let path = format!("{}:share", self.url_builder.notebook_path(notebook_id));
let url = self.url_builder.build_url(&path)?;
let request = ShareRequest {
account_and_roles: accounts,
};
self.http
.request_json(Method::POST, url, Some(&request))
.await
}
pub async fn list_recently_viewed(
&self,
page_size: Option<u32>,
) -> Result<ListRecentlyViewedResponse> {
let path = format!(
"{}:listRecentlyViewed",
self.url_builder.notebooks_collection()
);
let mut url = self.url_builder.build_url(&path)?;
if let Some(size) = page_size {
let clamped = size.clamp(PAGE_SIZE_MIN, PAGE_SIZE_MAX);
url.query_pairs_mut()
.append_pair("pageSize", &clamped.to_string());
}
self.http
.request_json::<(), _>(Method::GET, url, None::<&()>)
.await
}
}