Как сопоставить вектор с запросами клиента actix-web и запустить их последовательно? - PullRequest
0 голосов
/ 15 ноября 2018

У меня есть вектор, и я хочу сделать несколько запросов и получить вектор значений. Что-то вроде:

use actix_web::client;
let nums = vec![1, 2, 3];
let values = nums.map(|num| {
    client::ClientRequest::get("http://example.com".to_owned() + &num);
});

И я получу [1, 4, 9].

Сказал по-другому:

fn question_data(id: &str) -> Box<Future<Item = Question, Error = actix_web::error::Error>> {
    let f = std::fs::read_to_string("auth_token").unwrap();
    let token = f.trim();
    Box::new(
        client::ClientRequest::get("https://example.com/api/questions/".to_owned() + id)
            .header(
                actix_web::http::header::AUTHORIZATION,
                "Bearer ".to_owned() + token,
            )
            .finish()
            .unwrap()
            .send()
            .timeout(Duration::from_secs(30))
            .map_err(actix_web::error::Error::from) // <- convert SendRequestError to an Error
            .and_then(|resp| {
                resp.body().limit(67_108_864).from_err().and_then(|body| {
                    let resp: QuestionResponse = serde_json::from_slice(&body).unwrap();
                    fut_ok(resp.data)
                })
            }),
    )
}

Тогда я использую это:

let question_ids = vec!["q1", "q2", "q3"];

let mut questions = questions_data().wait().expect("Failed to fetch questions");
let question = question_data("q2")
    .wait()
    .expect("Failed to fetch question");
println!("{:?}", question);

let data = question_ids.map(|id| Box::new(question_data(id)));

Но я получаю ошибку:

245 |     let data = question_ids.map(|id| Box::new(question_data(id)));
    |                             ^^^
    |
    = note: the method `map` exists but the following trait bounds were not satisfied:
            `&mut std::vec::Vec<std::string::String> : futures::Future`
            `&mut std::vec::Vec<std::string::String> : std::iter::Iterator`
            `&mut [std::string::String] : futures::Future`
            `&mut [std::string::String] : std::iter::Iterator`

Они похожи, но не компилируются для меня, а также я хочу использовать actix-web:

...