feat(trello): add command 'trello attachement save'
Some checks failed
Test / build (map[name:stable]) (push) Successful in 1m42s
Test / build (map[name:nightly]) (push) Successful in 2m13s
Release Please / Release-plz (push) Failing after 15s

This commit is contained in:
Paul Campbell 2024-12-10 17:47:14 +00:00
parent e05950e09c
commit a43b37c938
7 changed files with 73 additions and 13 deletions

View file

@ -73,6 +73,7 @@ As part of building the import server, the following commands exercise each oper
- [x] trello stack get - includes list of cards - [x] trello stack get - includes list of cards
- [x] trello card get - includes list of attachments - [x] trello card get - includes list of attachments
- [x] trello attachment get - includes download url - [x] trello attachment get - includes download url
- [x] trello attachment save - saves to disk
- [ ] nextcloud deck get (was board list) - [ ] nextcloud deck get (was board list)
- [ ] nextcloud board get (was stack list) - [ ] nextcloud board get (was stack list)
- [ ] nextcloud stack get (was card list) - [ ] nextcloud stack get (was card list)

View file

@ -67,10 +67,10 @@ pub struct Ctx {
pub net: Net, pub net: Net,
pub prt: Printer, pub prt: Printer,
} }
impl Default for Ctx { impl Ctx {
fn default() -> Self { pub fn new(base: impl Into<PathBuf>) -> Self {
Self { Self {
fs: kxio::fs::new(PathBuf::default()), fs: kxio::fs::new(base),
net: kxio::net::new(), net: kxio::net::new(),
prt: kxio::print::standard(), prt: kxio::print::standard(),
} }
@ -79,7 +79,7 @@ impl Default for Ctx {
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct FullCtx { pub(crate) struct FullCtx {
// pub fs: FileSystem, pub fs: FileSystem,
pub net: Net, pub net: Net,
pub prt: Printer, pub prt: Printer,
pub cfg: AppConfig, pub cfg: AppConfig,
@ -121,7 +121,7 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
commands commands
.command .command
.execute(FullCtx { .execute(FullCtx {
// fs: ctx.fs, fs: ctx.fs,
net: ctx.net, net: ctx.net,
prt: ctx.prt, prt: ctx.prt,
cfg, cfg,

View file

@ -6,5 +6,5 @@ use trello_to_deck::{run, Ctx};
#[tokio::main] #[tokio::main]
#[cfg_attr(test, mutants::skip)] #[cfg_attr(test, mutants::skip)]
async fn main() -> Result<()> { async fn main() -> Result<()> {
run(Ctx::default()).await run(Ctx::new(std::env::current_dir()?)).await
} }

View file

@ -132,6 +132,7 @@ pub(crate) mod given {
pub(crate) fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx { pub(crate) fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
Ctx { fs, net, prt } Ctx { fs, net, prt }
} }
pub(crate) fn a_filesystem() -> TempFileSystem { pub(crate) fn a_filesystem() -> TempFileSystem {
kxio::fs::temp().expect("temp fs") kxio::fs::temp().expect("temp fs")
} }
@ -165,10 +166,10 @@ pub(crate) mod given {
} }
} }
pub(crate) fn a_full_context(fs: TempFileSystem, net: MockNet) -> FullCtx { pub(crate) fn a_full_context(fs: TempFileSystem, mock_net: MockNet) -> FullCtx {
FullCtx { FullCtx {
// fs: a_filesystem(), fs: fs.as_real(),
net: net.into(), net: mock_net.into(),
prt: a_printer(), prt: a_printer(),
cfg: AppConfig { cfg: AppConfig {
trello: a_trello_config(), trello: a_trello_config(),

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 KiB

View file

@ -1,3 +1,5 @@
use std::path::PathBuf;
// //
use clap::Parser; use clap::Parser;
use color_eyre::Result; use color_eyre::Result;
@ -15,6 +17,11 @@ pub(crate) enum TrelloAttachmentCommand {
card_id: String, card_id: String,
attachment_id: String, attachment_id: String,
}, },
Save {
card_id: String,
attachment_id: String,
file_name: Option<PathBuf>, // will use file name from attachment if not provided.
},
} }
impl Execute for TrelloAttachmentCommand { impl Execute for TrelloAttachmentCommand {
@ -25,12 +32,11 @@ impl Execute for TrelloAttachmentCommand {
card_id, card_id,
attachment_id, attachment_id,
} => { } => {
let trello_card_id = TrelloCardId::new(card_id);
let trello_attachment_id = TrelloAttachmentId::new(attachment_id);
let api_result = ctx let api_result = ctx
.trello_client() .trello_client()
.card_attachment( .card_attachment(&trello_card_id, &trello_attachment_id)
&TrelloCardId::new(card_id),
&TrelloAttachmentId::new(attachment_id),
)
.await; .await;
if dump { if dump {
p!(ctx.prt, "{}", api_result.text); p!(ctx.prt, "{}", api_result.text);
@ -40,6 +46,20 @@ impl Execute for TrelloAttachmentCommand {
} }
Ok(()) Ok(())
} }
Self::Save {
card_id,
attachment_id,
file_name,
} => {
let trello_card_id = TrelloCardId::new(card_id);
let trello_attachment_id = TrelloAttachmentId::new(attachment_id);
let file_name = ctx
.trello_client()
.save_attachment(&trello_card_id, &trello_attachment_id, file_name)
.await?;
p!(ctx.prt, "Wrote: {}", file_name.display());
Ok(())
}
} }
} }
} }

View file

@ -1,6 +1,8 @@
// //
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf;
use color_eyre::eyre::Context;
use kxio::net::{Net, ReqBuilder}; use kxio::net::{Net, ReqBuilder};
use crate::trello::model::{TrelloAttachment, TrelloAttachmentId}; use crate::trello::model::{TrelloAttachment, TrelloAttachmentId};
@ -26,6 +28,14 @@ impl<'ctx> TrelloClient<'ctx> {
f!("https://api.trello.com/1{path}") f!("https://api.trello.com/1{path}")
} }
fn auth_headers(&self) -> HashMap<String, String> {
let api_key = &self.ctx.cfg.trello.api_key;
let api_secret = &self.ctx.cfg.trello.api_secret;
HashMap::from([(
s!("Authorization"),
f!(r#"OAuth oauth_consumer_key="{api_key}", oauth_token="{api_secret}""#,),
)])
}
fn common_headers(&self) -> HashMap<String, String> { fn common_headers(&self) -> HashMap<String, String> {
let api_key = &self.ctx.cfg.trello.api_key; let api_key = &self.ctx.cfg.trello.api_key;
let api_secret = &self.ctx.cfg.trello.api_secret; let api_secret = &self.ctx.cfg.trello.api_secret;
@ -53,6 +63,34 @@ impl<'ctx> TrelloClient<'ctx> {
) )
.await .await
} }
pub(crate) async fn save_attachment(
&self,
card_id: &TrelloCardId,
attachment_id: &TrelloAttachmentId,
file_name: Option<std::path::PathBuf>,
) -> color_eyre::Result<PathBuf> {
let attachment = self.card_attachment(card_id, attachment_id).await.result?;
let url = attachment.url;
let file_name = file_name.unwrap_or_else(|| attachment.file_name.into());
crate::e!(self.ctx.prt, "file_name: {}", file_name.display());
crate::e!(self.ctx.prt, "base: {}", self.ctx.fs.base().display());
let file_name = self.ctx.fs.base().join(file_name);
crate::e!(self.ctx.prt, "file_name: {}", file_name.display());
let resp = self
.ctx
.net
.get(url)
.headers(self.auth_headers())
.header("accept", "application/octet")
.send()
.await?
.bytes()
.await?;
let file = self.ctx.fs.file(&file_name);
file.write(resp).context("writing to disk")?;
Ok(file_name)
}
} }
impl<'ctx> TrelloClient<'ctx> { impl<'ctx> TrelloClient<'ctx> {