diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-08-20 16:03:24 +0200 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-08-20 16:03:24 +0200 |
commit | 0ad87fec2d09cfc65bc3674732d501e683e7ce63 (patch) | |
tree | 7f773a11a42ce39f95241b6d1f422766ba5550ea /src | |
parent | 77763e9ce22c9eb173dec7aa29cf379d3b6b47ea (diff) | |
download | rust-zotapi-0ad87fec2d09cfc65bc3674732d501e683e7ce63.tar.gz rust-zotapi-0ad87fec2d09cfc65bc3674732d501e683e7ce63.tar.bz2 rust-zotapi-0ad87fec2d09cfc65bc3674732d501e683e7ce63.zip |
Refactoring: Make Client::post_data helper.
This means we don't need to access internal data members of the Client
in builder structs an the like.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 42 |
1 files changed, 25 insertions, 17 deletions
@@ -15,12 +15,14 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. extern crate reqwest; +extern crate serde; use reqwest::{ header::{Accept, ContentType, qitem}, mime, StatusCode, }; +use serde::Serialize; use std::io::Read; const ZOTAPI_CHANNEL_STREAM_PATH : &str = "/api/z/1.0/channel/stream"; @@ -65,23 +67,7 @@ impl<'a> ItemBuilder<'a> { } 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) - } + self.client.post_data(ZOTAPI_ITEM_UPDATE_PATH, &[("body", self.body)]) } } @@ -126,6 +112,28 @@ impl Client { _ => Err(Error::Unknown) } } + + fn post_data<T>(&self, path: &str, data: &T) -> Result<String, Error> + where T: Serialize, + { + let url = self.url(path); + let mut res = self.inner.post(&url) + .header(Accept(vec![qitem(mime::APPLICATION_JSON)])) + .header(ContentType::form_url_encoded()) + .basic_auth(self.user.clone(), Some(self.pw.clone())) + .form(&data) + .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 fn client(url: &str, user: &str, pw: &str) -> Client |