aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib.rs
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-08-20 15:23:35 +0200
committerHarald Eilertsen <haraldei@anduin.net>2018-08-20 15:23:35 +0200
commite3161f2e8561089d6db72f2a36011c7a5b54f320 (patch)
treea31194f28092a00bd4411fa8d1fe267aee7c9e1f /src/lib.rs
parent321ed90f9091e3e65b1bb8102dded76355d1c544 (diff)
downloadrust-zotapi-e3161f2e8561089d6db72f2a36011c7a5b54f320.tar.gz
rust-zotapi-e3161f2e8561089d6db72f2a36011c7a5b54f320.tar.bz2
rust-zotapi-e3161f2e8561089d6db72f2a36011c7a5b54f320.zip
Use builder pattern to construct items.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7520475..4852a94 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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)