shrupl/src/sharry/share.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2025-05-26 23:44:29 +00:00
use log::debug;
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
use super::{
alias::{Alias, SharryAlias},
api::{NewShareRequest, NewShareResponse, NotifyShareResponse},
};
2025-05-23 22:13:17 +00:00
#[derive(Debug)]
2025-05-28 14:07:29 +00:00
pub struct Share {
2025-05-24 00:22:24 +00:00
pub(super) id: String,
2025-05-23 22:13:17 +00:00
}
2025-05-28 14:07:29 +00:00
impl Share {
2025-05-24 00:22:24 +00:00
pub fn create(
http: &ureq::Agent,
2025-05-28 14:07:29 +00:00
alias: &Alias,
2025-05-24 00:22:24 +00:00
data: NewShareRequest,
) -> Result<Self, ureq::Error> {
2025-05-27 20:00:21 +00:00
let res = (http.post(alias.get_endpoint("alias/upload/new")))
2025-05-24 00:22:24 +00:00
.sharry_header(alias)
.send_json(data)?
.body_mut()
.read_json::<NewShareResponse>()?;
2025-05-23 22:13:17 +00:00
2025-05-26 23:56:31 +00:00
debug!("response: {res:?}");
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
if !(res.success && (res.message == "Share created.")) {
return Err(ureq::Error::Other("unexpected json response".into()));
2025-05-23 22:13:17 +00:00
}
2025-05-28 14:07:29 +00:00
Ok(Self { id: res.id })
2025-05-23 22:13:17 +00:00
}
2025-05-28 14:07:29 +00:00
pub fn notify(&self, http: &ureq::Agent, alias: &Alias) -> Result<(), ureq::Error> {
let endpoint = alias.get_endpoint(format!("alias/mail/notify/{}", self.id));
2025-05-23 22:13:17 +00:00
2025-05-27 20:00:21 +00:00
let res = (http.post(endpoint))
2025-05-28 14:07:29 +00:00
.sharry_header(alias)
2025-05-23 22:13:17 +00:00
.send_empty()?
.body_mut()
.read_json::<NotifyShareResponse>()?;
2025-05-26 23:56:31 +00:00
debug!("response: {res:?}");
2025-05-23 22:13:17 +00:00
Ok(())
}
}