在actix-web中调用异步的reqwest请求

3
在我的 actix-web 服务器中,我正在尝试使用 reqwest 调用外部服务器,然后将响应返回给用户。
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use futures::Future;
use lazy_static::lazy_static;
use reqwest::r#async::Client as HttpClient;
#[macro_use] extern crate serde_json;

#[derive(Debug, Deserialize)]
struct FormData {
    title: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct Response {
    title: String,
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .route("/validate", web::post().to(validator))
    })
    .bind("127.0.0.1:8000")
    .expect("Can not bind to port 8000")
    .run()
    .unwrap();
}

fn validator(form: web::Form<FormData>) -> impl Responder {
    let _resp = validate(form.title.clone());
    HttpResponse::Ok()
}

pub fn validate(title: String) -> impl Future<Item=String, Error=String> {
    let url = "https://jsonplaceholder.typicode.com/posts";
    lazy_static! {
        static ref HTTP_CLIENT: HttpClient = HttpClient::new();
    }
    HTTP_CLIENT.post(url)
        .json(
            &json!({
                "title": title,
            })
        )
        .send()
        .and_then(|mut resp| resp.json())
        .map(|json: Response| {
            println!("{:?}", json);
            json.title
        })
        .map_err(|error| format!("Error: {:?}", error))
}

这里有两个问题:

  1. println!("{:?}", json); 似乎从未运行,或者至少我从未看到任何输出。
  2. 我得到了一个名为 _respFuture,但我不知道如何等待其解决,以便我可以将字符串传回 Responder

参考:

$ curl -data "title=x" "https://jsonplaceholder.typicode.com/posts"
{
  "title": "x",
  "id": 101
}
1个回答

1
要使未来的块在解决之前被阻止,您必须在其上调用wait,但这并不理想。
您可以使您的验证器函数返回一个未来,并在路由中调用to_async而不是to。当未来得到解决时,框架将轮询并发送响应。
此外,您应该考虑使用随附于actix web的http客户端,并从您的应用程序中减少一个依赖项。
fn main() {
    HttpServer::new(|| {
        App::new()
            .route("/validate", web::post().to_async(validator))
    })
    .bind("127.0.0.1:8000")
    .expect("Can not bind to port 8000")
    .run()
    .unwrap();
}

fn validator(form: web::Form<FormData>) -> impl Future<Item=String, Error=String> {
    let url = "https://jsonplaceholder.typicode.com/posts";
    lazy_static! {
        static ref HTTP_CLIENT: HttpClient = HttpClient::new();
    }
    HTTP_CLIENT.post(url)
        .json(
            &json!({
                "title": form.title.clone(),
            })
        )
        .send()
        .and_then(|mut resp| resp.json())
        .map(|json: Response| {
            println!("{:?}", json);
            HttpResponse::Ok().body(Body::from(json.title))
        })
        .map_err(|error| format!("Error: {:?}", error))
}


1
我认为这个例子已经不再适用了。我收到一个关于处理函数未实现“AsyncFactory”的错误。我也对“Body::from”感到困惑,因为它不是原始的“use”语句的一部分。看起来它是“actix_web::body::Body::from”。 - Rokit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接