1
0
mirror of https://github.com/actix/examples synced 2025-01-22 22:05:57 +01:00

Merge pull request #136 from fairingrey/fix_validation

add unused validation to async_ex1 example
This commit is contained in:
Nikolay Kim 2019-06-08 10:02:29 +06:00 committed by GitHub
commit 6fbbacc077
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,7 +10,7 @@
// 2. validating user-submitted parameters using the 'validator' crate // 2. validating user-submitted parameters using the 'validator' crate
// 2. actix-web client features: // 2. actix-web client features:
// - POSTing json body // - 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] #[macro_use]
extern crate validator_derive; extern crate validator_derive;
@ -20,9 +20,12 @@ extern crate serde_derive;
use std::collections::HashMap; use std::collections::HashMap;
use std::io; use std::io;
use actix_web::client::Client; use actix_web::{
use actix_web::web::BytesMut; client::Client,
use actix_web::{web, App, Error, HttpResponse, HttpServer}; error::ErrorBadRequest,
web::{self, BytesMut},
App, Error, HttpResponse, HttpServer,
};
use futures::{Future, Stream}; use futures::{Future, Stream};
use validator::Validate; use validator::Validate;
@ -46,12 +49,13 @@ struct HttpBinResponse {
url: String, 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( fn step_x(
data: SomeData, data: SomeData,
client: &Client, client: &Client,
) -> impl Future<Item = SomeData, Error = Error> { ) -> impl Future<Item = SomeData, Error = Error> {
client let validation = futures::future::result(data.validate()).map_err(ErrorBadRequest);
let post_response = client
.post("https://httpbin.org/post") .post("https://httpbin.org/post")
.send_json(&data) .send_json(&data)
.map_err(Error::from) // <- convert SendRequestError to an Error .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(); let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
body.json body.json
}) })
}) });
validation.and_then(|_| post_response)
} }
fn create_something( fn create_something(