Skip to content

Commit

Permalink
Fixes post_service example by adding TLS
Browse files Browse the repository at this point in the history
Introduces hyper-native-tls and adds a TLS client to the adapter, fixing
an "Invalid scheme for Http" error that was introduced by a breaking
change to hyper 0.10.0 (which no longer includes TLS out-of-the-box).
  • Loading branch information
lispy committed Sep 4, 2019
1 parent 19fb873 commit 87414cd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ features = ["client", "hyper"]

[dev-dependencies]
serde_derive = "1.0"
hyper-native-tls = "0.3.0"

[features]
default = ["json", "xml"]
Expand Down
11 changes: 9 additions & 2 deletions examples/post_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
// instead of `Deserialize` and `Serialize`, respectively.

#[macro_use] extern crate anterofit;
extern crate hyper_native_tls;
#[macro_use] extern crate serde_derive;

// The minimum imports needed to get this example working.
//
// You can glob-import if you like, but know that it will shadow `Result`
// which may cause some confusing type-mismatch errors.
use anterofit::{Adapter, Url};
use anterofit::{Adapter, Url, hyper::{net::HttpsConnector, Client}};
use hyper_native_tls::NativeTlsClient;

#[derive(Debug, Deserialize)]
struct Post {
Expand Down Expand Up @@ -69,8 +71,13 @@ fn main() {
// Navigate to this URL in your browser for details. Very useful test API.
let url = Url::parse("https://jsonplaceholder.typicode.com").unwrap();

let ssl = NativeTlsClient::new().unwrap();
let connector = HttpsConnector::new(ssl);
let client = Client::with_connector(connector);

let adapter = Adapter::builder()
.base_url(url)
.client(client)
// When your REST API uses JSON in both requests and responses
.serialize_json()
.build();
Expand Down Expand Up @@ -116,4 +123,4 @@ fn user_posts(post_service: &PostService) {
// This will be executed asynchronously when the request is completed
.on_complete(|posts| for post in posts { println!("User post: {:?}", post); })
.exec().ignore();
}
}

0 comments on commit 87414cd

Please sign in to comment.