1
0
mirror of https://github.com/actix/examples synced 2025-02-17 15:23:31 +01:00

Update basics/json-validation to v4. (#475)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Luca Palmieri 2022-02-02 01:48:16 +00:00 committed by GitHub
parent da799d58e7
commit 5f3bdfcbe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 20 deletions

View File

@ -4,10 +4,11 @@ version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-web = { version = "3", features = ["openssl"] } actix-web = { version = "4.0.0-beta.21", features = ["openssl"] }
env_logger = "0.8" awc = "3.0.0-beta.19"
env_logger = "0.9"
futures = "0.3.1" futures = "0.3.1"
serde = { version = "1.0.43", features = ["derive"] } serde = { version = "1.0.43", features = ["derive"] }
serde_json = "1.0.16" serde_json = "1.0.16"
validator = "0.10" validator = "0.14"
validator_derive = "0.10" validator_derive = "0.14"

View File

@ -12,18 +12,16 @@
// - POSTing json body // - POSTing json body
// 3. chaining futures into a single response used by an async endpoint // 3. chaining futures into a single response used by an async endpoint
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::io;
use actix_web::{ use actix_web::{
client::Client,
error::ErrorBadRequest, error::ErrorBadRequest,
http::StatusCode,
web::{self, BytesMut}, web::{self, BytesMut},
App, Error, HttpResponse, HttpServer, App, Error, HttpResponse, HttpServer,
}; };
use awc::Client;
use futures::StreamExt; use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::io;
use validator::Validate; use validator::Validate;
use validator_derive::Validate; use validator_derive::Validate;
@ -38,18 +36,14 @@ struct SomeData {
#[allow(dead_code)] // it is debug printed #[allow(dead_code)] // it is debug printed
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct HttpBinResponse { struct HttpBinResponse {
args: HashMap<String, String>,
data: String,
files: HashMap<String, String>,
form: HashMap<String, String>,
headers: HashMap<String, String>,
json: SomeData, json: SomeData,
origin: String,
url: String,
} }
/// validate data, 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
async fn step_x(data: SomeData, client: &Client) -> Result<SomeData, Error> { async fn step_x(
data: SomeData,
client: &Client,
) -> Result<SomeData, actix_web::error::Error> {
// validate data // validate data
data.validate().map_err(ErrorBadRequest)?; data.validate().map_err(ErrorBadRequest)?;
@ -57,7 +51,8 @@ async fn step_x(data: SomeData, client: &Client) -> Result<SomeData, Error> {
.post("https://httpbin.org/post") .post("https://httpbin.org/post")
.send_json(&data) .send_json(&data)
.await .await
.map_err(Error::from)?; // <- convert SendRequestError to an Error // <- convert SendRequestError to an InternalError, a type that implements the ResponseError trait
.map_err(actix_web::error::ErrorInternalServerError)?; // <- convert it into an actix_web::Error
let mut body = BytesMut::new(); let mut body = BytesMut::new();
while let Some(chunk) = res.next().await { while let Some(chunk) = res.next().await {
@ -93,7 +88,7 @@ async fn main() -> io::Result<()> {
println!("Starting server at: {:?}", endpoint); println!("Starting server at: {:?}", endpoint);
HttpServer::new(|| { HttpServer::new(|| {
App::new() App::new()
.data(Client::default()) .app_data(web::Data::new(Client::default()))
.service(web::resource("/something").route(web::post().to(create_something))) .service(web::resource("/something").route(web::post().to(create_something)))
}) })
.bind(endpoint)? .bind(endpoint)?