feat(nextcloud): DeckClient hold reference to FullCtx

given a full context
This commit is contained in:
Paul Campbell 2024-12-07 18:11:24 +00:00
parent 841a5b93f1
commit 750bcd7fe2
2 changed files with 61 additions and 34 deletions

View file

@ -1,9 +1,5 @@
// //
use kxio::net::Net; use crate::{api_result::APIResult, f, FullCtx};
use kxio::print::Printer;
use crate::api_result::APIResult;
use crate::{config::NextcloudConfig, f};
use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername}; use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername};
use model::{Board, Card, NextcloudBoardId, Stack}; use model::{Board, Card, NextcloudBoardId, Stack};
@ -13,22 +9,20 @@ pub mod model;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
pub struct DeckClient<'cfg> { pub struct DeckClient<'ctx> {
net: Net, ctx: &'ctx FullCtx,
prt: Printer, hostname: &'ctx NextcloudHostname,
hostname: &'cfg NextcloudHostname, username: &'ctx NextcloudUsername,
username: &'cfg NextcloudUsername, password: &'ctx NextcloudPassword,
password: &'cfg NextcloudPassword,
} }
impl<'cfg> DeckClient<'cfg> { impl<'ctx> DeckClient<'ctx> {
pub fn new(cfg: &'cfg NextcloudConfig, net: Net, prt: Printer) -> Self { pub fn new(ctx: &'ctx FullCtx) -> Self {
Self { Self {
net, ctx,
prt, hostname: &ctx.cfg.nextcloud.hostname,
hostname: &cfg.hostname, username: &ctx.cfg.nextcloud.username,
username: &cfg.username, password: &ctx.cfg.nextcloud.password,
password: &cfg.password,
} }
} }
@ -42,33 +36,36 @@ impl<'cfg> DeckClient<'cfg> {
pub async fn get_boards(&self) -> APIResult<Vec<Board>> { pub async fn get_boards(&self) -> APIResult<Vec<Board>> {
APIResult::new( APIResult::new(
self.net self.ctx
.net
.get(self.url("boards")) .get(self.url("boards"))
.basic_auth(self.username.as_str(), Some(self.password.as_str())) .basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json") .header("accept", "application/json")
.send() .send()
.await, .await,
&self.prt, &self.ctx.prt,
) )
.await .await
} }
pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> { pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
APIResult::new( APIResult::new(
self.net self.ctx
.net
.get(self.url(f!("boards/{board_id}"))) .get(self.url(f!("boards/{board_id}")))
.basic_auth(self.username.as_str(), Some(self.password.as_str())) .basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json") .header("accept", "application/json")
.send() .send()
.await, .await,
&self.prt, &self.ctx.prt,
) )
.await .await
} }
pub async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> { pub async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
APIResult::new( APIResult::new(
self.net self.ctx
.net
.post(self.url("boards")) .post(self.url("boards"))
.basic_auth(self.username.as_str(), Some(self.password.as_str())) .basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json") .header("accept", "application/json")
@ -81,20 +78,21 @@ impl<'cfg> DeckClient<'cfg> {
) )
.send() .send()
.await, .await,
&self.prt, &self.ctx.prt,
) )
.await .await
} }
pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> { pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
APIResult::new( APIResult::new(
self.net self.ctx
.net
.get(self.url(f!("boards/{board_id}/stacks"))) .get(self.url(f!("boards/{board_id}/stacks")))
.basic_auth(self.username.as_str(), Some(self.password.as_str())) .basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json") .header("accept", "application/json")
.send() .send()
.await, .await,
&self.prt, &self.ctx.prt,
) )
.await .await
} }
@ -120,14 +118,15 @@ impl<'cfg> DeckClient<'cfg> {
} }
APIResult::new( APIResult::new(
self.net self.ctx
.net
.post(&url) .post(&url)
.basic_auth(self.username.as_str(), Some(self.password.as_str())) .basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json") .header("accept", "application/json")
.body(json.to_string()) .body(json.to_string())
.send() .send()
.await, .await,
&self.prt, &self.ctx.prt,
) )
.await .await
} }

View file

@ -147,8 +147,9 @@ mod client {
) )
.expect("mock request"); .expect("mock request");
let cfg = given::a_nextcloud_config(); let fs = given::a_filesystem();
let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
//when //when
let result = deck_client.get_boards().await.result.expect("get boards"); let result = deck_client.get_boards().await.result.expect("get boards");
@ -189,12 +190,13 @@ mod client {
.body(include_str!("../tests/responses/nextcloud-stack-list.json")) .body(include_str!("../tests/responses/nextcloud-stack-list.json"))
.expect("mock request"); .expect("mock request");
let cfg = given::a_nextcloud_config(); let fs = given::a_filesystem();
let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); let ctx = given::a_full_context(mock_net, fs);
let deck_client = DeckClient::new(&ctx);
//when //when
let result = deck_client let result = deck_client
.get_stacks(cfg.board_id) .get_stacks(ctx.cfg.nextcloud.board_id)
.await .await
.result .result
.expect("get stacks"); .expect("get stacks");
@ -229,7 +231,9 @@ mod client {
} }
mod given { mod given {
use kxio::print::Printer; use kxio::{fs::TempFileSystem, net::MockNet, print::Printer};
use crate::{config::TrelloConfig, s, AppConfig, FullCtx};
use super::*; use super::*;
@ -249,4 +253,28 @@ mod given {
pub fn a_printer() -> Printer { pub fn a_printer() -> Printer {
kxio::print::test() 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(),
},
}
}
} }