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