Skip to content

migrate hyper to reqwest #11

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ name = "xml-rpc"
readme = "README.md"
repository = "https://github.com/adnanademovic/xml-rpc-rs"
version = "0.1.0"
edition = "2024"

[dependencies]
base64 = "0.22.1"
error-chain = "0.12.4"
hyper = "0.10.15"
reqwest = { version = "0.12", features = ["blocking"] }
lazy_static = "1.5.0"
regex = "1.11.1"
serde = { version = "1.0.217", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion src/bin/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn main() {
bar: "baz".into(),
};
println!("Sending: {:?}", req);
let uri = "http://localhost:8080/".parse().unwrap();
let uri = "http://localhost:8080/";
let res: Result<Result<TestStruct, _>, _> = client.call(&uri, "echo", req.clone());
println!("Echo Received: {:?}", res);
let res: Result<Result<TestStruct, _>, _> = client.call(&uri, "double", req.clone());
Expand Down
8 changes: 4 additions & 4 deletions src/bin/threadcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,25 @@ fn main() {
std::thread::sleep(Duration::from_secs(1));

let t1 = std::thread::spawn(|| {
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "foo", ())
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "foo", ())
.unwrap()
.unwrap();
});

let t2 = std::thread::spawn(|| {
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "bar", ())
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "bar", ())
.unwrap()
.unwrap();
});

let t3 = std::thread::spawn(|| {
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "foo", ())
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "foo", ())
.unwrap()
.unwrap();
});

let t4 = std::thread::spawn(|| {
call::<_, _, ()>(&"http://127.0.0.1:5000".parse().unwrap(), "bar", ())
call::<_, _, _, ()>(&"http://127.0.0.1:5000", "bar", ())
.unwrap()
.unwrap();
});
Expand Down
36 changes: 17 additions & 19 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
use super::error::{Result, ResultExt};
use super::xmlfmt::{from_params, into_params, parse, Call, Fault, Params, Response};
use hyper::{self, Client as HyperClient};
use serde::{Deserialize, Serialize};
use std;
use Url;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_TYPE};

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

pub fn call_value<Tkey>(uri: &Url, name: Tkey, params: Params) -> Result<Response>
pub fn call_value<URL, Tkey>(uri: &URL, name: Tkey, params: Params) -> Result<Response>
where
URL: reqwest::IntoUrl + Clone,
Tkey: Into<String>,
{
Client::new()?.call_value(uri, name, params)
}

pub fn call<'a, Tkey, Treq, Tres>(
uri: &Url,
pub fn call<'a, URL, Tkey, Treq, Tres>(
uri: &URL,
name: Tkey,
req: Treq,
) -> Result<std::result::Result<Tres, Fault>>
where
URL: reqwest::IntoUrl + Clone,
Tkey: Into<String>,
Treq: Serialize,
Tres: Deserialize<'a>,
Expand All @@ -29,34 +27,33 @@ where
}

pub struct Client {
client: HyperClient,
client: reqwest::blocking::Client,
}

impl Client {
pub fn new() -> Result<Client> {
let client = HyperClient::new();
let client = reqwest::blocking::Client::new();
Ok(Client { client })
}

pub fn call_value<Tkey>(&mut self, uri: &Url, name: Tkey, params: Params) -> Result<Response>
pub fn call_value<URL, Tkey>(&mut self, uri: &URL, name: Tkey, params: Params) -> Result<Response>
where
URL: reqwest::IntoUrl + Clone,
Tkey: Into<String>,
{
use super::xmlfmt::value::ToXml;
let body_str = Call {
let body = Call {
name: name.into(),
params,
}
.to_xml();
let bytes: &[u8] = body_str.as_bytes();
let body = hyper::client::Body::BufBody(bytes, bytes.len());

let mut headers = Headers::new();
headers.set(ContentType("xml".to_owned()));
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/xml"));

let response = self
.client
.post(uri.as_ref())
.post(uri.clone())
.headers(headers)
.body(body)
.send()
Expand All @@ -65,13 +62,14 @@ impl Client {
parse::response(response).map_err(Into::into)
}

pub fn call<'a, Tkey, Treq, Tres>(
pub fn call<'a, URL, Tkey, Treq, Tres>(
&mut self,
uri: &Url,
uri: &URL,
name: Tkey,
req: Treq,
) -> Result<std::result::Result<Tres, Fault>>
where
URL: reqwest::IntoUrl + Clone,
Tkey: Into<String>,
Treq: Serialize,
Tres: Deserialize<'a>,
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ extern crate base64;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate hyper;
#[macro_use]
extern crate lazy_static;
extern crate regex;
#[macro_use]
Expand All @@ -15,13 +13,13 @@ pub extern crate rouille;
extern crate serde_bytes;
extern crate serde_xml_rs;
extern crate xml;
pub extern crate reqwest;

pub mod client;
pub mod error;
pub mod server;
mod xmlfmt;

pub use client::{call, call_value, Client};
pub use hyper::Url;
pub use server::Server;
pub use xmlfmt::{from_params, into_params, Call, Fault, Params, Response, Value};
pub use crate::client::{call, call_value, Client};
pub use crate::server::Server;
pub use crate::xmlfmt::{from_params, into_params, Call, Fault, Params, Response, Value};
1 change: 0 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ where
F: Send + Sync + 'static + Fn(&rouille::Request) -> rouille::Response,
{
server: rouille::Server<F>,
// server: hyper::Server<NewService, hyper::Body>,
}

impl<F> BoundServer<F>
Expand Down