From f1a34d534c28700cee021ad3b0b100cb598f5052 Mon Sep 17 00:00:00 2001 From: grey Date: Thu, 6 Jun 2019 15:30:54 -0700 Subject: [PATCH 1/2] add unused validation to async_ex1 example --- async_ex1/src/main.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/async_ex1/src/main.rs b/async_ex1/src/main.rs index 1bb4fd2d..a3acde76 100644 --- a/async_ex1/src/main.rs +++ b/async_ex1/src/main.rs @@ -10,7 +10,7 @@ // 2. validating user-submitted parameters using the 'validator' crate // 2. actix-web client features: // - POSTing json body -// 3. chaining futures into a single response used by an asynch endpoint +// 3. chaining futures into a single response used by an async endpoint #[macro_use] extern crate validator_derive; @@ -20,9 +20,12 @@ extern crate serde_derive; use std::collections::HashMap; use std::io; -use actix_web::client::Client; -use actix_web::web::BytesMut; -use actix_web::{web, App, Error, HttpResponse, HttpServer}; +use actix_web::{ + client::Client, + web::{self, BytesMut}, + error::ErrorBadRequest, + App, Error, HttpResponse, HttpServer, +}; use futures::{Future, Stream}; use validator::Validate; @@ -46,12 +49,13 @@ struct HttpBinResponse { url: String, } -/// post json to httpbin, get it back in the response body, return deserialized +/// validate data, post json to httpbin, get it back in the response body, return deserialized fn step_x( data: SomeData, client: &Client, ) -> impl Future { - client + let validation = futures::future::result(data.validate()).map_err(ErrorBadRequest); + let post_response = client .post("https://httpbin.org/post") .send_json(&data) .map_err(Error::from) // <- convert SendRequestError to an Error @@ -65,7 +69,9 @@ fn step_x( let body: HttpBinResponse = serde_json::from_slice(&body).unwrap(); body.json }) - }) + }); + + validation.and_then(|_| post_response) } fn create_something( From 72a58b7ca8b253cdc214052078526de66123f775 Mon Sep 17 00:00:00 2001 From: grey Date: Fri, 7 Jun 2019 13:30:14 -0700 Subject: [PATCH 2/2] cargo fmt --- async_ex1/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/async_ex1/src/main.rs b/async_ex1/src/main.rs index a3acde76..25fa3bc5 100644 --- a/async_ex1/src/main.rs +++ b/async_ex1/src/main.rs @@ -22,8 +22,8 @@ use std::io; use actix_web::{ client::Client, - web::{self, BytesMut}, error::ErrorBadRequest, + web::{self, BytesMut}, App, Error, HttpResponse, HttpServer, }; use futures::{Future, Stream};