Upgrade reqwest to 0.10 version and use rustls to remove transitive OpenSSL (#677)

- revive `warp_async` example (#659)

Co-authored-by: Allan Calix <contact@allancalix.com>
Co-authored-by: Kai Ren <tyranron@gmail.com>
This commit is contained in:
Joao Neves 2020-06-14 16:26:18 +03:00 committed by tyranron
parent 6dd6abbadc
commit f19d498254
No known key found for this signature in database
GPG key ID: 762E144FB230A4F0
4 changed files with 39 additions and 51 deletions

View file

@ -1,20 +1,16 @@
[package]
name = "warp_async"
version = "0.1.0"
authors = ["Christoph Herzog <chris@theduke.at>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
authors = ["Christoph Herzog <chris@theduke.at>"]
[dependencies]
log = "0.4.8"
juniper = { git = "https://github.com/graphql-rust/juniper" }
juniper_warp = { git = "https://github.com/graphql-rust/juniper" }
env_logger = "0.6.2"
warp = "0.1.19"
futures = { version = "0.3.1", features = ["compat"] }
reqwest = "0.9.19"
log = "0.4.8"
reqwest = { version = "0.10", features = ["rustls-tls"] }
tokio = { version = "0.2", features = ["rt-core", "macros"] }
juniper_codegen = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
juniper = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
juniper_warp = { git = "https://github.com/graphql-rust/juniper", branch = "async-await", features = ["async"] }
warp = "0.2"

View file

@ -1,30 +1,29 @@
//!
//! This example demonstrates async/await usage with warp.
//! NOTE: this uses tokio 0.1 , not the alpha tokio 0.2.
use juniper::{EmptyMutation, EmptySubscription, RootNode, FieldError};
use juniper::{
graphql_object, EmptyMutation, EmptySubscription, FieldError, GraphQLEnum, RootNode,
};
use warp::{http::Response, Filter};
#[derive(Clone)]
struct Context {
}
#[derive(Clone, Copy, Debug)]
struct Context;
impl juniper::Context for Context {}
#[derive(juniper::GraphQLEnum, Clone, Copy)]
#[derive(Clone, Copy, Debug, GraphQLEnum)]
enum UserKind {
Admin,
User,
Guest,
}
#[derive(Clone, Debug)]
struct User {
id: i32,
kind: UserKind,
name: String,
}
#[juniper::graphql_object(Context = Context)]
#[graphql_object(Context = Context)]
impl User {
fn id(&self) -> i32 {
self.id
@ -43,45 +42,38 @@ impl User {
}
}
#[derive(Clone, Copy, Debug)]
struct Query;
#[juniper::graphql_object(Context = Context)]
#[graphql_object(Context = Context)]
impl Query {
async fn users() -> Vec<User> {
vec![
User{
vec![User {
id: 1,
kind: UserKind::Admin,
name: "user1".into(),
},
]
}]
}
/// Fetch a URL and return the response body text.
async fn request(url: String) -> Result<String, FieldError> {
use futures::{ compat::{Stream01CompatExt, Future01CompatExt}, stream::TryStreamExt};
let res = reqwest::r#async::Client::new()
.get(&url)
.send()
.compat()
.await?;
let body_raw = res.into_body().compat().try_concat().await?;
let body = std::str::from_utf8(&body_raw).unwrap_or("invalid utf8");
Ok(body.to_string())
Ok(reqwest::get(&url).await?.text().await?)
}
}
type Schema = RootNode<'static, Query, EmptyMutation<Context>, EmptySubscription<Context>>;
fn schema() -> Schema {
Schema::new(Query, EmptyMutation::<Context>::new(), EmptySubscription::<Context>::new())
Schema::new(
Query,
EmptyMutation::<Context>::new(),
EmptySubscription::<Context>::new(),
)
}
#[tokio::main]
async fn main() {
::std::env::set_var("RUST_LOG", "warp_async");
std::env::set_var("RUST_LOG", "warp_async");
env_logger::init();
let log = warp::log("warp_server");
@ -96,13 +88,13 @@ async fn main() {
log::info!("Listening on 127.0.0.1:8080");
let state = warp::any().map(move || Context{} );
let state = warp::any().map(|| Context);
let graphql_filter = juniper_warp::make_graphql_filter(schema(), state.boxed());
warp::serve(
warp::get()
.and(warp::path("graphiql"))
.and(juniper_warp::graphiql_filter("/graphql"))
.and(juniper_warp::graphiql_filter("/graphql", None))
.or(homepage)
.or(warp::path("graphql").and(graphql_filter))
.with(log),

View file

@ -14,11 +14,11 @@ url = "2"
juniper = { version = "0.14.2", default-features = false, path = "../juniper"}
tokio = "0.2"
hyper = "0.13"
futures = { version = "0.3.1" }
futures = "0.3.1"
[dev-dependencies]
pretty_env_logger = "0.2"
reqwest = "0.9"
reqwest = { version = "0.10", features = ["blocking", "rustls-tls"] }
[dev-dependencies.juniper]
version = "0.14.2"

View file

@ -322,7 +322,7 @@ mod tests {
tests::{model::Database, schema::Query},
EmptyMutation, EmptySubscription, RootNode,
};
use reqwest::{self, Response as ReqwestResponse};
use reqwest::{self, blocking::Response as ReqwestResponse};
use std::{net::SocketAddr, sync::Arc, thread, time::Duration};
struct TestHyperIntegration {
@ -332,12 +332,12 @@ mod tests {
impl http_tests::HttpIntegration for TestHyperIntegration {
fn get(&self, url: &str) -> http_tests::TestResponse {
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
make_test_response(reqwest::get(&url).expect(&format!("failed GET {}", url)))
make_test_response(reqwest::blocking::get(&url).expect(&format!("failed GET {}", url)))
}
fn post_json(&self, url: &str, body: &str) -> http_tests::TestResponse {
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
let client = reqwest::Client::new();
let client = reqwest::blocking::Client::new();
let res = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/json")
@ -349,7 +349,7 @@ mod tests {
fn post_graphql(&self, url: &str, body: &str) -> http_tests::TestResponse {
let url = format!("http://127.0.0.1:{}/graphql{}", self.port, url);
let client = reqwest::Client::new();
let client = reqwest::blocking::Client::new();
let res = client
.post(&url)
.header(reqwest::header::CONTENT_TYPE, "application/graphql")
@ -360,15 +360,15 @@ mod tests {
}
}
fn make_test_response(mut response: ReqwestResponse) -> http_tests::TestResponse {
fn make_test_response(response: ReqwestResponse) -> http_tests::TestResponse {
let status_code = response.status().as_u16() as i32;
let body = response.text().unwrap();
let content_type_header = response.headers().get(reqwest::header::CONTENT_TYPE);
let content_type = if let Some(ct) = content_type_header {
format!("{}", ct.to_str().unwrap())
} else {
String::default()
};
let body = response.text().unwrap();
http_tests::TestResponse {
status_code,