shrupl/src/sharry/share.rs

53 lines
1.3 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)]
pub struct Share<'t> {
2025-05-24 00:22:24 +00:00
pub(super) alias: &'t Alias,
pub(super) id: String,
2025-05-23 22:13:17 +00:00
}
impl<'t> Share<'t> {
2025-05-24 00:22:24 +00:00
pub fn create(
http: &ureq::Agent,
alias: &'t Alias,
data: NewShareRequest,
) -> Result<Self, ureq::Error> {
let res = http
.post(alias.get_endpoint("alias/upload/new"))
.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-24 00:22:24 +00:00
Ok(Self { alias, id: res.id })
2025-05-23 22:13:17 +00:00
}
pub fn notify(&self, http: &ureq::Agent) -> Result<(), ureq::Error> {
let endpoint = self
.alias
2025-05-24 00:22:24 +00:00
.get_endpoint(format!("alias/mail/notify/{}", self.id));
2025-05-23 22:13:17 +00:00
2025-05-24 00:22:24 +00:00
let res = http
.post(endpoint)
.sharry_header(self.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(())
}
}