// use bytes::Bytes; use kxio::net::{Net, ReqBuilder}; use crate::{api_result::APIResult, f, FullCtx}; use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername}; use model::{Board, Card, NextcloudBoardId, Stack}; pub mod board; pub mod model; pub mod stack; // #[cfg(test)] // mod tests; pub(crate) struct DeckClient<'ctx> { ctx: &'ctx FullCtx, hostname: &'ctx NextcloudHostname, username: &'ctx NextcloudUsername, password: &'ctx NextcloudPassword, } impl<'ctx> DeckClient<'ctx> { pub fn new(ctx: &'ctx FullCtx) -> Self { Self { ctx, hostname: &ctx.cfg.nextcloud.hostname, username: &ctx.cfg.nextcloud.username, password: &ctx.cfg.nextcloud.password, } } fn url(&self, path: impl Into) -> String { f!( "https://{}/index.php/apps/deck/api/v1.0/{}", self.hostname, path.into() ) } async fn request serde::Deserialize<'a>>( &self, url: impl Into, custom: fn(&Net, String) -> ReqBuilder, ) -> APIResult { APIResult::new( custom(&self.ctx.net, self.url(url)) .basic_auth(self.username.as_str(), Some(self.password.as_str())) .header("accept", "application/json") .header("content-type", "application/json") .send() .await, &self.ctx.prt, ) .await } async fn request_with_body serde::Deserialize<'a>>( &self, url: impl Into, body: impl Into, custom: fn(&Net, String) -> ReqBuilder, ) -> APIResult { APIResult::new( custom(&self.ctx.net, self.url(url)) .basic_auth(self.username.as_str(), Some(self.password.as_str())) .header("accept", "application/json") .header("content-type", "application/json") .body(body) .send() .await, &self.ctx.prt, ) .await } pub async fn get_boards(&self) -> APIResult> { self.request("boards", |net, url| net.get(url)).await } pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult { self.request(f!("boards/{board_id}"), |net, url| net.get(url)) .await } pub async fn create_board(&self, title: &str, color: &str) -> APIResult { self.request_with_body( "boards", serde_json::json!({ "title": title, "color": color }) .to_string(), |net, url| net.post(url), ) .await } pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult> { self.request(f!("boards/{board_id}/stacks"), |net, url| net.get(url)) .await } pub async fn create_card( &self, board_id: i64, stack_id: i64, title: &str, description: Option<&str>, ) -> APIResult { let mut body = serde_json::json!({ "title": title, }); if let Some(desc) = description { body["description"] = serde_json::Value::String(desc.to_string()); } self.request_with_body( format!("boards/{}/stacks/{}/cards", board_id, stack_id), body.to_string(), |net, url| net.post(url), ) .await } }