【问题标题】:How can I use a Hyper client from a gRPC function?如何通过 gRPC 函数使用 Hyper 客户端?
【发布时间】:2019-06-13 01:33:13
【问题描述】:

我有一个 gRPC 服务器,需要创建一个 HTTP GET。我正在努力在电话中正确把握未来。

我正在尝试这个

fn current(
    &self,
    _: ::grpc::RequestOptions,
    p: quote::CurrentRequest,
) -> ::grpc::SingleResponse<quote::CurrentResponse> {
    let symbol = p.get_symbol();

    let client = Client::new();
    let fut: grpc::GrpcFuture<quote::CurrentResponse> = Box::new(
        client
            .get(Uri::from_static(AlphaFunction::base_url()))
            .and_then(|res| res.into_body().concat2())
            .and_then(|body| {
                info!("body {:?}", body);
                let mut r = quote::CurrentResponse::new();
                // TODO: Parse body
                r.set_symbol(symbol.to_string());
                Ok(r)
            })
            .map_err(|e| e),
    );

    grpc::SingleResponse::new(fut)
}

但是我得到了一堆错误:

expected struct `hyper::error::Error`, found enum `grpc::error::Error`

    77 |         grpc::SingleResponse::new(fut)
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `protos::quote::CurrentResponse`, found tuple

【问题讨论】:

  • 很难回答您的问题,因为它不包含minimal reproducible example。我们无法分辨代码中存在哪些 crate(及其版本)、类型、特征、字段等。如果您尝试在 Rust Playground 上重现您的错误,如果可能的话,这将使我们更容易为您提供帮助,否则在全新的 Cargo 项目中,然后在 edit 您的问题中包含附加信息。您可以使用Rust-specific MCVE tips 来减少您在此处发布的原始代码。谢谢!
  • 吸取教训,谢谢!

标签: rust hyper


【解决方案1】:

我想通了:

let fut = client
    .get(Uri::from_static(AlphaFunction::base_url()))
    .and_then(|res| res.into_body().concat2())
    .and_then(move |body| {
        info!("body {:?}", body);
        let mut r = quote::CurrentResponse::new();
        r.set_symbol(symbol.to_string());
        Ok(r)
    })
    .map_err(|e| grpc::Error::Panic(format!("{}", e)));

【讨论】:

  • 回答没有任何描述这些变化是什么以及为什么需要这些变化的答案对那些后来尝试使用它们的人来说只是微不足道的帮助。
猜你喜欢
  • 2021-07-31
  • 2020-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
相关资源
最近更新 更多