trello-to-deck/src/nextcloud/mod.rs

114 lines
2.9 KiB
Rust
Raw Normal View History

//
use kxio::net::Net;
use crate::api_result::APIResult;
use crate::{config::NextcloudConfig, f};
use model::{Board, Card, NextcloudBoardId, Stack};
pub mod model;
#[cfg(test)]
mod tests;
pub(crate) struct DeckClient {
net: Net,
hostname: String,
username: String,
password: String,
}
impl DeckClient {
pub fn new(cfg: &NextcloudConfig, net: Net) -> Self {
Self {
net,
hostname: cfg.hostname.to_string(),
username: cfg.username.to_string(),
password: cfg.password.to_string(),
}
}
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(
self.net
.get(self.url("boards"))
.basic_auth(&self.username, Some(&self.password))
.header("accept", "application/json")
.send()
.await,
)
.await
}
pub async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
APIResult::new(
self.net
.post(self.url("boards"))
.basic_auth(&self.username, Some(&self.password))
.header("accept", "application/json")
.body(
serde_json::json!({
"title": title,
"color": color
})
.to_string(),
)
.send()
.await,
)
.await
}
pub async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
APIResult::new(
self.net
.get(self.url(f!("boards/{board_id}/stacks")))
.basic_auth(&self.username, Some(&self.password))
.header("accept", "application/json")
.send()
.await,
)
.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(
self.net
.post(&url)
.basic_auth(&self.username, Some(&self.password))
.header("accept", "application/json")
.body(json.to_string())
.send()
.await,
)
.await
}
}