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-08 14:39:03 +00:00
|
|
|
use crate::{api_result::APIResult, f, FullCtx};
|
|
|
|
|
|
|
|
use card::Create;
|
|
|
|
use model::{
|
|
|
|
Board, Card, NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudStackId,
|
|
|
|
NextcloudUsername, Stack,
|
2024-11-30 18:04:48 +00:00
|
|
|
};
|
2024-11-30 11:30:36 +00:00
|
|
|
|
2024-12-04 19:37:39 +00:00
|
|
|
pub mod board;
|
2024-11-30 18:04:48 +00:00
|
|
|
pub mod card;
|
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
|
|
|
|
2024-12-05 20:07:29 +00:00
|
|
|
// #[cfg(test)]
|
|
|
|
// mod tests;
|
2024-11-30 11:30:36 +00:00
|
|
|
|
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-08 14:39:03 +00:00
|
|
|
|
|
|
|
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
|
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
|
|
|
}
|
|
|
|
|
2024-11-30 18:04:48 +00:00
|
|
|
pub async fn get_stack(
|
|
|
|
&self,
|
|
|
|
board_id: NextcloudBoardId,
|
|
|
|
stack_id: NextcloudStackId,
|
|
|
|
) -> APIResult<Stack> {
|
|
|
|
self.request(f!("boards/{board_id}/stacks/{stack_id}"), |net, url| {
|
|
|
|
net.get(url)
|
|
|
|
})
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
2024-12-08 14:39:03 +00:00
|
|
|
pub(crate) async fn create_card(&self, create: &Create) -> APIResult<Card> {
|
2024-12-04 19:37:39 +00:00
|
|
|
let mut body = serde_json::json!({
|
2024-12-08 14:39:03 +00:00
|
|
|
"title": create.title,
|
2024-12-07 10:05:37 +00:00
|
|
|
});
|
|
|
|
|
2024-12-08 14:39:03 +00:00
|
|
|
if let Some(desc) = &create.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(
|
2024-12-08 14:39:03 +00:00
|
|
|
format!(
|
|
|
|
"boards/{}/stacks/{}/cards",
|
|
|
|
self.ctx.cfg.nextcloud.board_id, create.stack_id
|
|
|
|
),
|
2024-12-04 19:37:39 +00:00
|
|
|
body.to_string(),
|
|
|
|
|net, url| net.post(url),
|
2024-11-30 11:30:36 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2024-12-07 09:56:40 +00:00
|
|
|
|
|
|
|
async fn get_card(
|
|
|
|
&self,
|
|
|
|
board_id: NextcloudBoardId,
|
|
|
|
stack_id: NextcloudStackId,
|
|
|
|
card_id: model::NextcloudCardId,
|
|
|
|
) -> APIResult<Card> {
|
|
|
|
self.request(
|
|
|
|
f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}"),
|
|
|
|
|net, url| net.get(url),
|
|
|
|
)
|
|
|
|
.await
|
|
|
|
}
|
2024-11-30 11:30:36 +00:00
|
|
|
}
|