Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove use of futures and tokio from within client #1

Merged
merged 1 commit into from
Apr 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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