From 9436e2afe9d0e48660826c30a4d658e1a8db567a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Thu, 22 May 2025 21:07:49 +0000 Subject: [PATCH] notify a share --- .vscode/launch.json | 2 +- src/main.rs | 15 ++++++++----- src/sharry/api.rs | 21 +++++++++++++----- src/sharry/mod.rs | 54 +++++++++++++++++++++++++++++++-------------- 4 files changed, 65 insertions(+), 27 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 61c8ce4..0ba1d1b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -21,7 +21,7 @@ }, "args": [], "env": { - "RUST_LOG": "shrupl=info", + "RUST_LOG": "shrupl=debug", }, "cwd": "${workspaceFolder}" }, diff --git a/src/main.rs b/src/main.rs index 0c751c3..006bb38 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ mod sharry; use log::info; use sharry::{ Alias, - api::{NewShareRequest, SharryAPI}, + api::{NewShareRequest, URI}, }; use std::time::Duration; use ureq::Agent; @@ -16,10 +16,15 @@ fn main() { .build() .into(); - let api = SharryAPI::new(None, "sharry.yavook.de"); - - let alias = Alias::new(&api, "G7RYoWME1W7-pcgipemJcr8-39FcMd92gBu-RgufeHc51z6"); + let alias = Alias::new( + URI::new("sharry.yavook.de"), + "G7RYoWME1W7-pcgipemJcr8-39FcMd92gBu-RgufeHc51z6", + ); let share = alias.create_share(&agent, &NewShareRequest::new(Some("foo"), "", 10)); - info!("share: {:?}", share) + + let share = share.unwrap(); + info!("share: {:?}", share); + + share.notify(&agent).unwrap(); } diff --git a/src/sharry/api.rs b/src/sharry/api.rs index 6c28822..1a66779 100644 --- a/src/sharry/api.rs +++ b/src/sharry/api.rs @@ -2,20 +2,24 @@ use log::debug; use serde::{Deserialize, Serialize}; #[derive(Debug)] -pub struct SharryAPI { +pub struct URI { protocol: String, base_url: String, } -impl SharryAPI { - pub fn new(protocol: Option<&str>, base_url: &str) -> Self { +impl URI { + pub fn new_protocol(base_url: &str, protocol: &str) -> Self { Self { - protocol: protocol.unwrap_or("https").into(), + protocol: protocol.into(), base_url: base_url.into(), } } - pub fn get_uri(&self, endpoint: &str) -> String { + pub fn new(base_url: &str) -> Self { + Self::new_protocol(base_url, "https") + } + + pub fn get_endpoint(&self, endpoint: &str) -> String { let uri = format!("{}://{}/api/v2/{}", self.protocol, self.base_url, endpoint); debug!("uri: {:?}", uri); @@ -51,3 +55,10 @@ pub struct NewShareResponse { pub message: String, pub id: String, } + +#[derive(Deserialize, Debug)] +#[allow(dead_code)] +pub struct NotifyShareResponse { + pub success: bool, + pub message: String, +} diff --git a/src/sharry/mod.rs b/src/sharry/mod.rs index 388e541..a0ebc4c 100644 --- a/src/sharry/mod.rs +++ b/src/sharry/mod.rs @@ -1,21 +1,22 @@ pub mod api; -use api::{NewShareRequest, NewShareResponse, SharryAPI}; +use api::{NewShareRequest, NewShareResponse, NotifyShareResponse, URI}; use log::debug; -use ureq; +use ureq::{self, RequestBuilder}; #[derive(Debug)] -pub struct Alias<'t> { - api: &'t SharryAPI, +pub struct Alias { + uri: URI, id: String, } -impl<'t> Alias<'t> { - pub fn new(api: &'t SharryAPI, id: &str) -> Self { - Self { - api: api, - id: id.into(), - } +impl Alias { + pub fn new(uri: URI, id: &str) -> Self { + Self { uri, id: id.into() } + } + + fn add_headers(&self, req: RequestBuilder) -> RequestBuilder { + req.header("Sharry-Alias", &self.id) } pub fn create_share( @@ -23,28 +24,49 @@ impl<'t> Alias<'t> { http: &ureq::Agent, data: &NewShareRequest, ) -> Result { - let foo = http - .post(self.api.get_uri("alias/upload/new")) - .header("Sharry-Alias", &self.id) + let endpoint = self.uri.get_endpoint("alias/upload/new"); + + let res = self + .add_headers(http.post(endpoint)) .send_json(data)? .body_mut() .read_json::()?; - debug!("foo: {:?}", foo); + debug!("response: {:?}", res); + assert!(res.success); + assert_eq!(res.message, "Share created."); Ok(Share { alias: self, - id: foo.id, + id: res.id, }) } } #[derive(Debug)] pub struct Share<'t> { - alias: &'t Alias<'t>, + alias: &'t Alias, id: String, } impl<'t> Share<'t> { pub fn add_file(&self) {} + + pub fn notify(&self, http: &ureq::Agent) -> Result<(), ureq::Error> { + let endpoint = self + .alias + .uri + .get_endpoint(&format!("alias/mail/notify/{}", &self.id)); + + let res = self + .alias + .add_headers(http.post(endpoint)) + .send_empty()? + .body_mut() + .read_json::()?; + + debug!("response: {:?}", res); + + Ok(()) + } }