42 lines
1 KiB
Rust
42 lines
1 KiB
Rust
|
|
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<String>) -> Self {
|
||
|
|
Self { uri, id: id.into() }
|
||
|
|
}
|
||
|
|
|
||
|
|
pub(super) fn add_headers<B>(&self, req: RequestBuilder<B>) -> RequestBuilder<B> {
|
||
|
|
req.header("Sharry-Alias", &self.id)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn create_share(&self, http: &Agent, data: &NewShareRequest) -> Result<Share, ureq::Error> {
|
||
|
|
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!("response: {:?}", res);
|
||
|
|
|
||
|
|
if !(res.success && (res.message == "Share created.")) {
|
||
|
|
return Err(ureq::Error::Other("unexpected response json".into()));
|
||
|
|
}
|
||
|
|
|
||
|
|
Ok(Share::new(self, res.id))
|
||
|
|
}
|
||
|
|
}
|