From 10e6bcf982258502f2a941e6d01b5d4a6a4f7688 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 7 Dec 2024 18:11:24 +0000 Subject: [PATCH] feat(nextcloud): DeckClient hold reference to FullCtx given a full context --- src/nextcloud/mod.rs | 80 +++++++++++++++++++++++++++++------------- src/nextcloud/tests.rs | 40 +++++++++++++++++---- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/src/nextcloud/mod.rs b/src/nextcloud/mod.rs index 1abee44..3eb81f0 100644 --- a/src/nextcloud/mod.rs +++ b/src/nextcloud/mod.rs @@ -1,9 +1,5 @@ // -use kxio::net::Net; -use kxio::print::Printer; - -use crate::api_result::APIResult; -use crate::{config::NextcloudConfig, f}; +use crate::{api_result::APIResult, f, FullCtx}; use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername}; use model::{Board, Card, NextcloudBoardId, Stack}; @@ -13,21 +9,19 @@ 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, +pub(crate) struct DeckClient<'ctx> { + ctx: &'ctx FullCtx, + hostname: &'ctx NextcloudHostname, + username: &'ctx NextcloudUsername, + password: &'ctx NextcloudPassword, } -impl<'cfg> DeckClient<'cfg> { - pub fn new(cfg: &'cfg NextcloudConfig, net: Net, prt: Printer) -> Self { +impl<'ctx> DeckClient<'ctx> { + pub fn new(ctx: &'ctx FullCtx) -> Self { Self { - net, - prt, - hostname: &cfg.hostname, - username: &cfg.username, - password: &cfg.password, + ctx, + hostname: &ctx.cfg.nextcloud.hostname, + username: &ctx.cfg.nextcloud.username, + password: &ctx.cfg.nextcloud.password, } } @@ -41,26 +35,63 @@ impl<'cfg> DeckClient<'cfg> { pub async fn get_boards(&self) -> APIResult> { APIResult::new( - self.net + self.ctx + .net .get(self.url("boards")) .basic_auth(self.username.as_str(), Some(self.password.as_str())) .header("accept", "application/json") .send() .await, - &self.prt, + &self.ctx.prt, + ) + .await + } + + pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult { + APIResult::new( + self.ctx + .net + .get(self.url(f!("boards/{board_id}"))) + .basic_auth(self.username.as_str(), Some(self.password.as_str())) + .header("accept", "application/json") + .send() + .await, + &self.ctx.prt, + ) + .await + } + + pub async fn create_board(&self, title: &str, color: &str) -> APIResult { + APIResult::new( + self.ctx + .net + .post(self.url("boards")) + .basic_auth(self.username.as_str(), Some(self.password.as_str())) + .header("accept", "application/json") + .body( + serde_json::json!({ + "title": title, + "color": color + }) + .to_string(), + ) + .send() + .await, + &self.ctx.prt, ) .await } pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult> { APIResult::new( - self.net + self.ctx + .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, + &self.ctx.prt, ) .await } @@ -86,14 +117,15 @@ impl<'cfg> DeckClient<'cfg> { } APIResult::new( - self.net + self.ctx + .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, + &self.ctx.prt, ) .await } diff --git a/src/nextcloud/tests.rs b/src/nextcloud/tests.rs index 4bbff40..a9fd2ec 100644 --- a/src/nextcloud/tests.rs +++ b/src/nextcloud/tests.rs @@ -139,8 +139,9 @@ mod client { ) .expect("mock request"); - let cfg = given::a_nextcloud_config(); - let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); + let fs = given::a_filesystem(); + let ctx = given::a_full_context(mock_net, fs); + let deck_client = DeckClient::new(&ctx); //when let result = deck_client.get_boards().await.result.expect("get boards"); @@ -181,12 +182,13 @@ mod client { .body(include_str!("../tests/responses/nextcloud-stack-list.json")) .expect("mock request"); - let cfg = given::a_nextcloud_config(); - let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); + let fs = given::a_filesystem(); + let ctx = given::a_full_context(mock_net, fs); + let deck_client = DeckClient::new(&ctx); //when let result = deck_client - .get_stacks(cfg.board_id) + .get_stacks(ctx.cfg.nextcloud.board_id) .await .result .expect("get stacks"); @@ -221,7 +223,9 @@ mod client { } mod given { - use kxio::print::Printer; + use kxio::{fs::TempFileSystem, net::MockNet, print::Printer}; + + use crate::{config::TrelloConfig, s, AppConfig, FullCtx}; use super::*; @@ -241,4 +245,28 @@ mod given { pub fn a_printer() -> Printer { kxio::print::test() } + + pub(crate) fn a_filesystem() -> TempFileSystem { + kxio::fs::temp().expect("temp fs") + } + + pub(crate) fn a_trello_config() -> TrelloConfig { + TrelloConfig { + api_key: s!("trello-api-key").into(), + api_secret: s!("trello-api-secret").into(), + board_name: s!("trello-board-name").into(), + } + } + + pub(crate) fn a_full_context(mock_net: MockNet, fs: TempFileSystem) -> FullCtx { + FullCtx { + fs: fs.as_real(), + net: mock_net.into(), + prt: given::a_printer(), + cfg: AppConfig { + trello: given::a_trello_config(), + nextcloud: given::a_nextcloud_config(), + }, + } + } }