Skip to content

Commit

Permalink
Merge pull request #1 from astraw/back-to-the-no-future
Browse files Browse the repository at this point in the history
remove use of futures and tokio from within client
  • Loading branch information
adnanademovic authored Apr 28, 2018
2 parents d258ec5 + 586bb10 commit 9891a28
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ version = "0.0.8"
base64 = "0.6.0"
error-chain = "0.10.0"
futures = "0.1.14"
hyper10 = {path="hyper10"}
hyper = "0.11.2"
lazy_static = "1.0.0"
regex = "0.2.3"
serde = "1.0.11"
serde-xml-rs = "0.2.1"
serde_bytes = "0.10.2"
serde_derive = "1.0.11"
tokio-core = "0.1.9"
xml-rs = "0.6.1"
7 changes: 7 additions & 0 deletions hyper10/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hyper10"
version = "0.1.0"
authors = ["Andrew Straw <strawman@astraw.com>"]

[dependencies]
hyper = "0.10"
2 changes: 2 additions & 0 deletions hyper10/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extern crate hyper;
pub use hyper::*;
44 changes: 22 additions & 22 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std;
use futures::{Future, Stream};
use hyper::{self, Client as HyperClient, Method, Request, Uri};
use hyper::Uri;
use hyper10::{self, Client as HyperClient};
use serde::{Deserialize, Serialize};
use tokio_core::reactor::Core;
use super::error::{Result, ResultExt};
use super::xmlfmt::{from_params, into_params, parse, Call, Fault, Params, Response};

use hyper10::header::Headers;
header! { (ContentType, "ContentType") => [String] }

pub fn call_value<Tkey>(uri: &Uri, name: Tkey, params: Params) -> Result<Response>
where
Tkey: Into<String>,
Expand All @@ -27,16 +29,13 @@ where
}

pub struct Client {
core: Core,
client: HyperClient<hyper::client::HttpConnector>,
client: HyperClient,
}

impl Client {
pub fn new() -> Result<Client> {
let core = Core::new().chain_err(|| "Failed to initialize Tokio Core.")?;
let client = HyperClient::new(&core.handle());
let client = HyperClient::new();
Ok(Client {
core: core,
client: client,
})
}
Expand All @@ -46,23 +45,24 @@ impl Client {
Tkey: Into<String>,
{
use super::xmlfmt::value::ToXml;
let body = Call {
let body_str = Call {
name: name.into(),
params,
}.to_xml();
let mut request = Request::new(Method::Post, uri.clone());
request
.headers_mut()
.set(hyper::header::ContentLength(body.len() as u64));
request.headers_mut().set(hyper::header::ContentType::xml());
request.set_body(body);
let work = self.client
.request(request)
.and_then(|res| res.body().concat2().map(|chunk| chunk.to_vec()));
let response = self.core
.run(work)
.chain_err(|| "Failed to run the HTTP request within Tokio Core.")?;
parse::response(response.as_slice()).map_err(Into::into)
let bytes: &[u8] = body_str.as_bytes();
let body = hyper10::client::Body::BufBody(bytes, bytes.len());

let mut headers = Headers::new();
headers.set(ContentType("xml".to_owned()));

let response = self.client
.post(uri.as_ref())
.headers(headers)
.body(body)
.send()
.chain_err(|| "Failed to run the HTTP request within hyper.")?;

parse::response(response).map_err(Into::into)
}

pub fn call<'a, Tkey, Treq, Tres>(
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ extern crate base64;
#[macro_use]
extern crate error_chain;
extern crate futures;
#[macro_use]
extern crate hyper10;
extern crate hyper;
#[macro_use]
extern crate lazy_static;
Expand All @@ -15,7 +17,6 @@ extern crate serde_bytes;
#[macro_use]
extern crate serde_derive;
extern crate serde_xml_rs;
extern crate tokio_core;
extern crate xml;

pub mod client;
Expand Down

0 comments on commit 9891a28

Please sign in to comment.