Akka-http 并将 Future[HttpResponse] 转换为 Future[String]

Akka-http and converting Future[HttpResponse] to Future[String]

我正在使用 Akka HTTP,并且我有一个查询 URL 的客户端。代码是这样的:

def f1(): Future[HttpResponse] =  { ... }

我想编写一个 f2 函数,该函数接受 HttpResponse 但将其转换为 Future[String],其中 String 是消息的正文。所以我在寻找:

def f2(): Future[String] = f1().map(...)

我无法将其转换为正常工作。

问题 #1:我认为这更像是一个基本的 Scala 问题。我将如何转换这个未来?我能得到的最好的是:

def f2:Future[String] = {
 f1().map (_.entity.toStrict(300.milli).map(_.data.utf8String))
}

这是行不通的,因为我最终得到了 Future[Future[String]]

问题 #2:这是我理解的 akka-http 问题。

f1().map(_.toStrict()) 是否意味着 f1().map(_.entity.toStrict())

如果是这样,我如何才能访问该实体而不必对其调用 toStrict?

// 1. 你可以使用 for comprehension (with Option[Option[T]], Future[Future[T]], Try[Try[T]) or flatMap or .map { _ pipeTo self } (如果在演员中)。

val httpResponseF: Future[HttpResponse] = Future.successful(HttpResponse(entity = HttpEntity.Strict(ContentTypes.`text/plain(UTF-8)`, data = ByteString("test"))))
val res: Future[String] = for {
  httpResponse <- httpResponseF
  entity       <- httpResponse.entity.toStrict(300.milli)
} yield {
  entity match {
    case HttpEntity.Strict(contentType, data) => data.utf8String
  }
}
assert(Await.result(res, 1.second) == "test")

// 2. 他们看起来和我不一样: akka.http.scaladsl.model.HttpResponse

def
toStrict(timeout: FiniteDuration)(implicit ec: ExecutionContext, fm: Materializer): Future[Self]

//akka.http.javadsl.model.ResponseEntity

def
toStrict(timeoutMillis: Long, materializer: Materializer): CompletionStage[HttpEntity.Strict]

一个使用隐式。

这是获取字符串作为 http 客户端的一种方法。

object Example {
  import scala.concurrent.Future
  import akka.actor.ActorSystem
  import akka.http.scaladsl.Http
  import akka.stream.ActorMaterializer
  import akka.stream.scaladsl.Sink
  import akka.stream.scaladsl.Source
  import akka.http.scaladsl.unmarshalling._
  import akka.http.scaladsl.model._
  import scala.util.{Try,Success,Failure}
  implicit val system = ActorSystem()
  implicit val materializer = ActorMaterializer()
  implicit val ec = system.dispatcher
  // construct a pool client flow with context type `Int`
  val poolClientFlow = Http().cachedHostConnectionPool[Int]("akka.io")
  val responseFuture: Future[String] =
    Source.single(HttpRequest(uri = "/") -> 1)
      .via(poolClientFlow)
      .mapAsync(1) { 
          case (Success(response), i) => Unmarshal(response.entity).to[String]
          case (Failure(e), i)        => Future.failed(e)
      }
      .runWith(Sink.head)

}

如果有一个更具体的类型(如果使用 Scala 则更惯用)你试图解组而不是字符串你可以使用这个 Json 作为模板创建一个自定义解组器:https://github.com/hseeberger/akka-http-json/blob/master/akka-http-json4s/src/main/scala/de/heikoseeberger/akkahttpjson4s/Json4sSupport.scala otherwise there is Unmarshallers.String http://doc.akka.io/docs/akka/2.4.3/java/http/routing-dsl/marshalling.html#Unmarshalling

有时检查 Spray 邮件列表和文档会有所帮助,因为它的活跃时间更长,即使在 akka-http 中有一种新方法可以做到这一点,它至少提供了一个起点。

你可以这样写 f2()。

def f2[T] = f1.mapTo[T]

第 1 季度:

如果在需要 Future[_] 的地方得到 Future[Future[_]],则应将 map 替换为 flatMap。这同样适用于 Option 和集合。

所以这看起来是正确的:

def f1(): Future[HttpResponse]

def f2(): Future[String] = {
 f1().flatMap (_.entity.toStrict(300.milli).map(_.data.utf8String))
}

第 2 季度:

HttpMessage.toStrict 确实在内部调用 HttpEntity.toStrict 所以他们做同样的事情。 (请注意,由于某种原因 HttpMessage.toStrict 未在 Java 界面中公开)。