1
0
mirror of https://github.com/actix/examples synced 2025-02-02 09:39:03 +01:00

Perform unused validation in async_ex1 example (#83)

This commit is contained in:
Allen 2019-03-09 20:10:39 -08:00 committed by Douman
parent 5c8aaba5f9
commit 4d26988edf

View File

@ -29,9 +29,9 @@ extern crate validator;
use actix_web::{ use actix_web::{
client, http::Method, server, App, AsyncResponder, Error, HttpMessage, HttpResponse, client, http::Method, server, App, AsyncResponder, Error, HttpMessage, HttpResponse,
Json, Json, error::ErrorBadRequest,
}; };
use futures::{future::ok as fut_ok, Future}; use futures::{future::ok as fut_ok, future::result as fut_result, Future};
use std::collections::HashMap; use std::collections::HashMap;
use std::time::Duration; use std::time::Duration;
use validator::Validate; use validator::Validate;
@ -63,19 +63,23 @@ struct HttpBinResponse {
/// post json to httpbin, get it back in the response body, return deserialized /// post json to httpbin, get it back in the response body, return deserialized
fn step_x_v1(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> { fn step_x_v1(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> {
Box::new( Box::new(
client::ClientRequest::post("https://httpbin.org/post") fut_result(data.validate()) // <- call .validate() on data to validate the parameters
.json(data).unwrap() .map_err(ErrorBadRequest) // - convert ValidationErrors to an Error
.send() .and_then(|_| {
.conn_timeout(Duration::from_secs(10)) client::ClientRequest::post("https://httpbin.org/post")
.map_err(Error::from) // <- convert SendRequestError to an Error .json(data).unwrap()
.and_then( .send()
|resp| resp.body() // <- this is MessageBody type, resolves to complete body .conn_timeout(Duration::from_secs(10))
.from_err() // <- convert PayloadError to an Error .map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|body| { .and_then(
let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap(); |resp| resp.body() // <- this is MessageBody type, resolves to complete body
fut_ok(resp.json) .from_err() // <- convert PayloadError to an Error
}) .and_then(|body| {
), let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap();
fut_ok(resp.json)
})
)
})
) )
} }
@ -102,19 +106,23 @@ fn create_something_v1(
/// post json to httpbin, get it back in the response body, return deserialized /// post json to httpbin, get it back in the response body, return deserialized
fn step_x_v2(data: SomeData) -> impl Future<Item = SomeData, Error = Error> { fn step_x_v2(data: SomeData) -> impl Future<Item = SomeData, Error = Error> {
client::ClientRequest::post("https://httpbin.org/post") fut_result(data.validate()) // <- call .validate() on data to validate the parameters
.json(data).unwrap() .map_err(ErrorBadRequest) // - convert ValidationErrors to an Error
.send() .and_then(|_| {
.conn_timeout(Duration::from_secs(10)) client::ClientRequest::post("https://httpbin.org/post")
.map_err(Error::from) // <- convert SendRequestError to an Error .json(data).unwrap()
.and_then( .send()
|resp| resp.body() // <- this is MessageBody type, resolves to complete body .conn_timeout(Duration::from_secs(10))
.from_err() // <- convert PayloadError to an Error .map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|body| { .and_then(
let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap(); |resp| resp.body() // <- this is MessageBody type, resolves to complete body
fut_ok(resp.json) .from_err() // <- convert PayloadError to an Error
}) .and_then(|body| {
) let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap();
fut_ok(resp.json)
})
)
})
} }
fn create_something_v2( fn create_something_v2(