mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
add unused validation to async_ex1 example
This commit is contained in:
parent
aa5750cafc
commit
f1a34d534c
@ -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};
|
web::{self, BytesMut},
|
||||||
|
error::ErrorBadRequest,
|
||||||
|
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(
|
||||||
|
Loading…
Reference in New Issue
Block a user