diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 42 |
1 files changed, 42 insertions, 0 deletions
@@ -47,6 +47,44 @@ impl From<std::io::Error> for Error { } } +pub struct ItemBuilder<'a> { + client : &'a Client, + body: &'a str, +} + +impl<'a> ItemBuilder<'a> { + fn new(client: &'a Client) -> ItemBuilder<'a> { + ItemBuilder { + client: client, + body: "", + } + } + pub fn body(&mut self, text: &'a str) -> &mut ItemBuilder<'a> { + self.body = text; + self + } + + pub fn create(&self) -> Result<String, Error> { + let url = self.client.url(ZOTAPI_ITEM_UPDATE_PATH); + let mut res = self.client.inner.post(&url) + .header(Accept(vec![qitem(mime::APPLICATION_JSON)])) + .header(ContentType::form_url_encoded()) + .basic_auth(self.client.user.clone(), Some(self.client.pw.clone())) + .form(&[("body", self.body)]) + .send()?; + + match res.status() { + StatusCode::Unauthorized => Err(Error::Unauthorized), + StatusCode::Ok => { + let mut body = String::new(); + res.read_to_string(&mut body)?; + Ok(body) + }, + _ => Err(Error::Unknown) + } + } +} + pub struct Client { inner: reqwest::Client, base_url: String, @@ -63,6 +101,10 @@ impl Client { self.fetch_stream(ZOTAPI_NETWORK_STREAM_PATH) } + pub fn item(&self) -> ItemBuilder { + ItemBuilder::new(self) + } + pub fn create_item(&self, body: &str) -> Result<String, Error> { let url = self.url(ZOTAPI_ITEM_UPDATE_PATH); let mut res = self.inner.post(&url) |