Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix "actix_web::handler::AsyncFactory<_, _> is not implemented" error?

This is my code. I'm trying to get posts from the Rust subreddit. I would be using Actix's built in client, but it's not working on Windows, hence the use of reqwest instead:

[dependencies]
actix-web = "1.0.8"
futures = "0.1.29"
reqwest = "0.9.21"
use actix_web::{self, middleware, web, App, HttpRequest, HttpServer};
use futures::future::Future;
use reqwest::{self, r#async::Client};

fn get_rust_posts(
    _req: HttpRequest,
    client: web::Data<Client>,
) -> impl Future<Item = String, Error = reqwest::Error> {

    client
        .get("http://www.reddit.com/r/rust.json")
        .send()
        .and_then(|mut resp| resp.text())
        .map_err(|err| {
            println!("Error in get rust posts: {}", err);
            err
        })
}

fn main() {
    let mut server = HttpServer::new(|| {
        App::new()
            .data(Client::new())
            .wrap(middleware::Logger::default())
            .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
    });
    server.bind(("0.0.0.0", 8000)).unwrap().run().unwrap();
}

This is the error:

error[E0277]: the trait bound `fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future 
{get_rust_posts}: actix_web::handler::AsyncFactory<_, _>` is not satisfied
  --> src\main.rs:29:72
   |
29 |             .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
   |                                                                        ^^^^^^^^ the trait `actix_web::handler::AsyncFactory<_, _>` is not implemented for 
`fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future {get_rust_posts}
like image 935
Rokit Avatar asked Nov 06 '22 13:11

Rokit


1 Answers

to_async requires a handler function that implements the trait AsyncFactory. Changing the Future's Item to actix_web::HttpResponse and putting the reqwest request inside actix_web::web::block satisfies that constraint.

I also tested this with slow responses, and despite the word "block" in actix_web::web::block, it seemed to handle concurrent requests, and I think that's because the handler itself is async.

use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use futures::Future;
use reqwest;
use reqwest::Client;

static REDDIT: &str = "http://www.reddit.com/r/rust.json";

fn get_rust_posts(
    _req: actix_web::HttpRequest,
    client: web::Data<Client>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
    let builder = client.get(REDDIT);
    actix_web::web::block(move || builder.send())
        .from_err()
        .and_then(|mut res| match res.text() {
            Ok(body) => HttpResponse::Ok()
                .content_type("application/json")
                .body(body),
            Err(error) => {
                println!("get_request error: {}", error);
                HttpResponse::InternalServerError()
                    .content_type("application/json")
                    .body(format!("{{\"error\": \"Error getting response text.\"}}"))
            }
        })
}

fn main() {
    HttpServer::new(|| {
        App::new()
            .data(Client::new())
            .wrap(middleware::Logger::default())
            .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
    })
    .bind("127.0.0.1:5000")
    .unwrap()
    .run()
    .unwrap();
}
like image 163
Rokit Avatar answered Nov 26 '22 23:11

Rokit