message: add a request_events
diff: @@ -68,4 +68,48 @@ impl APIClient {
  })
  }
  }
  +
  + pub fn request_events<T>(&self, request: http::Request<Vec<u8>>) -> Result<Vec<T>, Error>
  + where
  + T: DeserializeOwned,
  + {
  + let (parts, body) = request.into_parts();
  + let uri_str = format!("{}{}", self.configuration.base_path, parts.uri);
  + let req = match parts.method {
  + http::Method::GET => self.configuration.client.get(&uri_str),
  + http::Method::POST => self.configuration.client.post(&uri_str),
  + http::Method::DELETE => self.configuration.client.delete(&uri_str),
  + http::Method::PUT => self.configuration.client.put(&uri_str),
  + other => {
  + return Err(Error::from(format_err!("Invalid method: {}", other)));
  + }
  + }.body(body);
  + let mut res = req.send()?;
  + if !res.status().is_success() {
  + let text = res.text()?;
  + // Print better debug when things do fail
  + if let Ok(errdata) = serde_json::from_str::<ApiError>(&text) {
  + println!("Unsuccessful: {:?}", errdata);
  + } else {
  + // In case some parts of ApiError for some reason don't exist..
  + println!("Unsuccessful data: {}", text);
  + }
  + // Propagate errors properly via reqwest
  + let e = res.error_for_status().unwrap_err();
  + Err(e.into())
  + } else {
  + // Should be able to coerce result into Vec<T> at this point
  + let mut xs : Vec<T> = vec![];
  + let text = res.text()?;
  + for l in text.lines() {
  + let r = serde_json::from_str(&l).map_err(|e| {
  + println!("{}", l);
  + Error::from(e)
  + })?;
  + xs.push(r);
  + }
  + Ok(xs)
  +
  + }
  + }
  }
  