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

101 lines
2.6 KiB
Rust
Raw Normal View History

//
use kxio::net::Net;
2024-12-07 10:05:37 +00:00
use kxio::print::Printer;
use crate::api_result::APIResult;
use crate::{config::NextcloudConfig, f};
use crate::nextcloud::model::{NextcloudHostname, NextcloudPassword, NextcloudUsername};
use model::{Board, Card, NextcloudBoardId, Stack};
pub mod model;
#[cfg(test)]
mod tests;
pub(crate) struct DeckClient<'cfg> {
net: Net,
2024-12-07 10:05:37 +00:00
prt: Printer,
hostname: &'cfg NextcloudHostname,
username: &'cfg NextcloudUsername,
password: &'cfg NextcloudPassword,
}
impl<'cfg> DeckClient<'cfg> {
2024-12-07 10:05:37 +00:00
pub fn new(cfg: &'cfg NextcloudConfig, net: Net, prt: Printer) -> Self {
Self {
net,
2024-12-07 10:05:37 +00:00
prt,
hostname: &cfg.hostname,
username: &cfg.username,
password: &cfg.password,
}
}
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.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.send()
.await,
2024-12-07 10:05:37 +00:00
&self.prt,
)
.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.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.send()
.await,
2024-12-07 10:05:37 +00:00
&self.prt,
)
.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.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.body(json.to_string())
.send()
.await,
&self.prt,
)
.await
}
}