2024-11-30 11:30:36 +00:00
|
|
|
//
|
2024-12-04 19:37:39 +00:00
|
|
|
use bytes::Bytes;
|
|
|
|
use kxio::net::{Net, ReqBuilder};
|
|
|
|
|
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};
|
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
pub mod board;
|
2024-11-30 11:30:36 +00:00
|
|
|
pub mod model;
|
2024-11-30 18:04:48 +00:00
|
|
|
pub mod stack;
|
2024-11-30 11:30:36 +00:00
|
|
|
|
|
|
|
#[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()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
async fn request<T: for<'a> serde::Deserialize<'a>>(
|
|
|
|
&self,
|
|
|
|
url: impl Into<String>,
|
|
|
|
custom: fn(&Net, String) -> ReqBuilder,
|
|
|
|
) -> APIResult<T> {
|
2024-11-30 11:30:36 +00:00
|
|
|
APIResult::new(
|
2024-12-04 19:37:39 +00:00
|
|
|
custom(&self.ctx.net, self.url(url))
|
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")
|
2024-12-04 19:37:39 +00:00
|
|
|
.header("content-type", "application/json")
|
2024-11-30 11:30:36 +00:00
|
|
|
.send()
|
|
|
|
.await,
|
2024-12-07 18:11:24 +00:00
|
|
|
&self.ctx.prt,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
async fn request_with_body<T: for<'a> serde::Deserialize<'a>>(
|
|
|
|
&self,
|
|
|
|
url: impl Into<String>,
|
|
|
|
body: impl Into<Bytes>,
|
|
|
|
custom: fn(&Net, String) -> ReqBuilder,
|
|
|
|
) -> APIResult<T> {
|
2024-12-07 18:11:24 +00:00
|
|
|
APIResult::new(
|
2024-12-04 19:37:39 +00:00
|
|
|
custom(&self.ctx.net, self.url(url))
|
2024-12-07 18:11:24 +00:00
|
|
|
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
|
|
|
.header("accept", "application/json")
|
2024-12-04 19:37:39 +00:00
|
|
|
.header("content-type", "application/json")
|
|
|
|
.body(body)
|
2024-12-07 18:11:24 +00:00
|
|
|
.send()
|
|
|
|
.await,
|
|
|
|
&self.ctx.prt,
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
pub async fn get_boards(&self) -> APIResult<Vec<Board>> {
|
|
|
|
self.request("boards", |net, url| net.get(url)).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
|
|
|
|
self.request(f!("boards/{board_id}"), |net, url| net.get(url))
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-12-07 18:11:24 +00:00
|
|
|
pub async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
|
2024-12-04 19:37:39 +00:00
|
|
|
self.request_with_body(
|
|
|
|
"boards",
|
|
|
|
serde_json::json!({
|
|
|
|
"title": title,
|
|
|
|
"color": color
|
|
|
|
})
|
|
|
|
.to_string(),
|
|
|
|
|net, url| net.post(url),
|
2024-11-30 11:30:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
|
2024-12-04 19:37:39 +00:00
|
|
|
self.request(f!("boards/{board_id}/stacks"), |net, url| net.get(url))
|
|
|
|
.await
|
2024-12-07 10:05:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn create_card(
|
|
|
|
&self,
|
|
|
|
board_id: i64,
|
|
|
|
stack_id: i64,
|
|
|
|
title: &str,
|
|
|
|
description: Option<&str>,
|
|
|
|
) -> APIResult<Card> {
|
2024-12-04 19:37:39 +00:00
|
|
|
let mut body = serde_json::json!({
|
2024-12-07 10:05:37 +00:00
|
|
|
"title": title,
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Some(desc) = description {
|
2024-12-04 19:37:39 +00:00
|
|
|
body["description"] = serde_json::Value::String(desc.to_string());
|
2024-12-07 10:05:37 +00:00
|
|
|
}
|
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
self.request_with_body(
|
|
|
|
format!("boards/{}/stacks/{}/cards", board_id, stack_id),
|
|
|
|
body.to_string(),
|
|
|
|
|net, url| net.post(url),
|
2024-11-30 11:30:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|