use log::debug; use ureq::{Agent, RequestBuilder}; use super::{ api::{NewShareRequest, NewShareResponse, URI}, share::Share, }; #[derive(Debug)] pub struct Alias { pub(super) uri: URI, pub(super) id: String, } impl Alias { pub fn new(uri: URI, id: impl Into) -> Self { Self { uri, id: id.into() } } pub(super) fn add_headers(&self, req: RequestBuilder) -> RequestBuilder { req.header("Sharry-Alias", &self.id) } pub fn create_share(&self, http: &Agent, data: &NewShareRequest) -> Result { 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!("response: {:?}", res); if !(res.success && (res.message == "Share created.")) { return Err(ureq::Error::Other("unexpected response json".into())); } Ok(Share::new(self, res.id)) } }