1
0
mirror of https://github.com/actix/examples synced 2025-06-28 09:50:36 +02:00

Update to master (#90)

This commit is contained in:
Juan Aguilar
2019-03-26 04:29:00 +01:00
committed by Douman
parent 1779f963d9
commit 53fc2221ef
44 changed files with 139 additions and 143 deletions

View File

@ -8,7 +8,7 @@ workspace = ".."
[dependencies]
actix-rt = "0.2"
actix-http = { git="https://github.com/actix/actix-http.git", features=["ssl"] }
actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0", features=["ssl"] }
actix-web = { git="https://github.com/actix/actix-web.git", features=["ssl"] }
futures = "0.1"
serde = "1.0.43"

View File

@ -24,8 +24,9 @@ use std::collections::HashMap;
use std::io;
use actix_http::client;
use actix_web::{web, App, Error, HttpMessage, HttpResponse, HttpServer};
use futures::future::{ok, Future};
use actix_web::web::BytesMut;
use actix_web::{web, App, Error, HttpResponse, HttpServer};
use futures::{Future, Stream};
use validator::Validate;
#[derive(Debug, Validate, Deserialize, Serialize)]
@ -54,7 +55,7 @@ struct HttpBinResponse {
/// 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>> {
let mut connector = client::Connector::default().service();
let mut connector = client::Connector::new().service();
Box::new(
client::ClientRequest::post("https://httpbin.org/post")
@ -62,13 +63,17 @@ fn step_x_v1(data: SomeData) -> Box<Future<Item = SomeData, Error = Error>> {
.unwrap()
.send(&mut connector)
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|mut resp| {
resp.body() // <- this is MessageBody type, resolves to complete body
.and_then(|resp| {
resp // <- this is MessageBody type, resolves to complete body
.from_err() // <- convert PayloadError to an Error
.and_then(|body| {
let resp: HttpBinResponse =
.fold(BytesMut::new(), |mut acc, chunk| {
acc.extend_from_slice(&chunk);
Ok::<_, Error>(acc)
})
.map(|body| {
let body: HttpBinResponse =
serde_json::from_slice(&body).unwrap();
ok(resp.json)
body.json
})
}),
)
@ -95,19 +100,22 @@ fn create_something_v1(
/// 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> {
let mut connector = client::Connector::default().service();
let mut connector = client::Connector::new().service();
client::ClientRequest::post("https://httpbin.org/post")
.json(data)
.unwrap()
.send(&mut connector)
.map_err(Error::from) // <- convert SendRequestError to an Error
.and_then(|mut resp| {
resp.body() // <- this is MessageBody type, resolves to complete body
.from_err() // <- convert PayloadError to an Error
.and_then(|body| {
let resp: HttpBinResponse = serde_json::from_slice(&body).unwrap();
ok(resp.json)
.and_then(|resp| {
resp.from_err()
.fold(BytesMut::new(), |mut acc, chunk| {
acc.extend_from_slice(&chunk);
Ok::<_, Error>(acc)
})
.map(|body| {
let body: HttpBinResponse = serde_json::from_slice(&body).unwrap();
body.json
})
})
}