2024-11-30 11:30:36 +00:00
|
|
|
//
|
2024-12-07 18:11:24 +00:00
|
|
|
use crate::{api_result::APIResult, f, FullCtx};
|
2024-11-30 11:30:36 +00:00
|
|
|
|
2024-12-04 20:31:36 +00:00
|
|
|
use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername};
|
2024-11-30 11:30:36 +00:00
|
|
|
use model::{Board, Card, NextcloudBoardId, Stack};
|
|
|
|
|
|
|
|
pub mod model;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2024-12-07 18:11:24 +00:00
|
|
|
pub(crate) struct DeckClient<'ctx> {
|
|
|
|
ctx: &'ctx FullCtx,
|
|
|
|
hostname: &'ctx NextcloudHostname,
|
|
|
|
username: &'ctx NextcloudUsername,
|
|
|
|
password: &'ctx NextcloudPassword,
|
2024-11-30 11:30:36 +00:00
|
|
|
}
|
2024-12-07 18:11:24 +00:00
|
|
|
impl<'ctx> DeckClient<'ctx> {
|
|
|
|
pub fn new(ctx: &'ctx FullCtx) -> Self {
|
2024-11-30 11:30:36 +00:00
|
|
|
Self {
|
2024-12-07 18:11:24 +00:00
|
|
|
ctx,
|
|
|
|
hostname: &ctx.cfg.nextcloud.hostname,
|
|
|
|
username: &ctx.cfg.nextcloud.username,
|
|
|
|
password: &ctx.cfg.nextcloud.password,
|
2024-11-30 11:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn url(&self, path: impl Into<String>) -> String {
|
|
|
|
f!(
|
|
|
|
"https://{}/index.php/apps/deck/api/v1.0/{}",
|
|
|
|
self.hostname,
|
|
|
|
path.into()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_boards(&self) -> APIResult<Vec<Board>> {
|
|
|
|
APIResult::new(
|
2024-12-07 18:11:24 +00:00
|
|
|
self.ctx
|
|
|
|
.net
|
2024-11-30 11:30:36 +00:00
|
|
|
.get(self.url("boards"))
|
2024-12-04 20:31:36 +00:00
|
|
|
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
2024-11-30 11:30:36 +00:00
|
|
|
.header("accept", "application/json")
|
|
|
|
.send()
|
|
|
|
.await,
|
2024-12-07 18:11:24 +00:00
|
|
|
&self.ctx.prt,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
|
|
|
|
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<Board> {
|
|
|
|
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,
|
2024-11-30 11:30:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
|
|
|
|
APIResult::new(
|
2024-12-07 18:11:24 +00:00
|
|
|
self.ctx
|
|
|
|
.net
|
2024-11-30 11:30:36 +00:00
|
|
|
.get(self.url(f!("boards/{board_id}/stacks")))
|
2024-12-04 20:31:36 +00:00
|
|
|
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
2024-11-30 11:30:36 +00:00
|
|
|
.header("accept", "application/json")
|
|
|
|
.send()
|
|
|
|
.await,
|
2024-12-07 18:11:24 +00:00
|
|
|
&self.ctx.prt,
|
2024-12-07 10:05:37 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_card(
|
|
|
|
&self,
|
|
|
|
board_id: i64,
|
|
|
|
stack_id: i64,
|
|
|
|
title: &str,
|
|
|
|
description: Option<&str>,
|
|
|
|
) -> APIResult<Card> {
|
|
|
|
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(
|
2024-12-07 18:11:24 +00:00
|
|
|
self.ctx
|
|
|
|
.net
|
2024-12-07 10:05:37 +00:00
|
|
|
.post(&url)
|
|
|
|
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
|
|
|
.header("accept", "application/json")
|
|
|
|
.body(json.to_string())
|
|
|
|
.send()
|
|
|
|
.await,
|
2024-12-07 18:11:24 +00:00
|
|
|
&self.ctx.prt,
|
2024-11-30 11:30:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|