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": [],
"env": {
"RUST_LOG": "shrupl=info",
"RUST_LOG": "shrupl=debug",
},
"cwd": "${workspaceFolder}"
},

View file

@ -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();
}

View file

@ -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,
}

View file

@ -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<B>(&self, req: RequestBuilder<B>) -> RequestBuilder<B> {
req.header("Sharry-Alias", &self.id)
}
pub fn create_share(
@ -23,28 +24,49 @@ impl<'t> Alias<'t> {
http: &ureq::Agent,
data: &NewShareRequest,
) -> Result<Share, ureq::Error> {
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::<NewShareResponse>()?;
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::<NotifyShareResponse>()?;
debug!("response: {:?}", res);
Ok(())
}
}