notify a share

This commit is contained in:
Jörn-Michael Miehe 2025-05-22 21:07:49 +00:00
parent 08f27b3644
commit 9436e2afe9
4 changed files with 65 additions and 27 deletions

2
.vscode/launch.json vendored
View file

@ -21,7 +21,7 @@
}, },
"args": [], "args": [],
"env": { "env": {
"RUST_LOG": "shrupl=info", "RUST_LOG": "shrupl=debug",
}, },
"cwd": "${workspaceFolder}" "cwd": "${workspaceFolder}"
}, },

View file

@ -3,7 +3,7 @@ mod sharry;
use log::info; use log::info;
use sharry::{ use sharry::{
Alias, Alias,
api::{NewShareRequest, SharryAPI}, api::{NewShareRequest, URI},
}; };
use std::time::Duration; use std::time::Duration;
use ureq::Agent; use ureq::Agent;
@ -16,10 +16,15 @@ fn main() {
.build() .build()
.into(); .into();
let api = SharryAPI::new(None, "sharry.yavook.de"); let alias = Alias::new(
URI::new("sharry.yavook.de"),
let alias = Alias::new(&api, "G7RYoWME1W7-pcgipemJcr8-39FcMd92gBu-RgufeHc51z6"); "G7RYoWME1W7-pcgipemJcr8-39FcMd92gBu-RgufeHc51z6",
);
let share = alias.create_share(&agent, &NewShareRequest::new(Some("foo"), "", 10)); 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();
} }

View file

@ -2,20 +2,24 @@ use log::debug;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug)] #[derive(Debug)]
pub struct SharryAPI { pub struct URI {
protocol: String, protocol: String,
base_url: String, base_url: String,
} }
impl SharryAPI { impl URI {
pub fn new(protocol: Option<&str>, base_url: &str) -> Self { pub fn new_protocol(base_url: &str, protocol: &str) -> Self {
Self { Self {
protocol: protocol.unwrap_or("https").into(), protocol: protocol.into(),
base_url: base_url.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); let uri = format!("{}://{}/api/v2/{}", self.protocol, self.base_url, endpoint);
debug!("uri: {:?}", uri); debug!("uri: {:?}", uri);
@ -51,3 +55,10 @@ pub struct NewShareResponse {
pub message: String, pub message: String,
pub id: String, pub id: String,
} }
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
pub struct NotifyShareResponse {
pub success: bool,
pub message: String,
}

View file

@ -1,21 +1,22 @@
pub mod api; pub mod api;
use api::{NewShareRequest, NewShareResponse, SharryAPI}; use api::{NewShareRequest, NewShareResponse, NotifyShareResponse, URI};
use log::debug; use log::debug;
use ureq; use ureq::{self, RequestBuilder};
#[derive(Debug)] #[derive(Debug)]
pub struct Alias<'t> { pub struct Alias {
api: &'t SharryAPI, uri: URI,
id: String, id: String,
} }
impl<'t> Alias<'t> { impl Alias {
pub fn new(api: &'t SharryAPI, id: &str) -> Self { pub fn new(uri: URI, id: &str) -> Self {
Self { Self { uri, id: id.into() }
api: api, }
id: id.into(),
} fn add_headers<B>(&self, req: RequestBuilder<B>) -> RequestBuilder<B> {
req.header("Sharry-Alias", &self.id)
} }
pub fn create_share( pub fn create_share(
@ -23,28 +24,49 @@ impl<'t> Alias<'t> {
http: &ureq::Agent, http: &ureq::Agent,
data: &NewShareRequest, data: &NewShareRequest,
) -> Result<Share, ureq::Error> { ) -> Result<Share, ureq::Error> {
let foo = http let endpoint = self.uri.get_endpoint("alias/upload/new");
.post(self.api.get_uri("alias/upload/new"))
.header("Sharry-Alias", &self.id) let res = self
.add_headers(http.post(endpoint))
.send_json(data)? .send_json(data)?
.body_mut() .body_mut()
.read_json::<NewShareResponse>()?; .read_json::<NewShareResponse>()?;
debug!("foo: {:?}", foo); debug!("response: {:?}", res);
assert!(res.success);
assert_eq!(res.message, "Share created.");
Ok(Share { Ok(Share {
alias: self, alias: self,
id: foo.id, id: res.id,
}) })
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub struct Share<'t> { pub struct Share<'t> {
alias: &'t Alias<'t>, alias: &'t Alias,
id: String, id: String,
} }
impl<'t> Share<'t> { impl<'t> Share<'t> {
pub fn add_file(&self) {} 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::<NotifyShareResponse>()?;
debug!("response: {:?}", res);
Ok(())
}
} }