使用Akka-Streams HTTP将整个HttpResponse主体作为String获取

时间:2015-07-21 07:29:15

标签: scala http akka akka-stream

我试图了解如何使用新的akka.http库。我想向服务器发送一个http请求,并将整个响应主体作为单个字符串读取,以生成Source[String,?]

这是迄今为止我能够制作的最佳解决方案:

 def get(
   modelID: String,
   pool: Flow[(HttpRequest,Int),(Try[HttpResponse],Int),Http.HostConnectionPool]
 ): Source[String,Unit] = {
   val uri = reactionsURL(modelID)
   val req = HttpRequest(uri = uri)
   Source.single( (req,0) )
     .via( pool )
     .map { 
       case (Success(resp),_) =>
         resp.entity.dataBytes.map( _.decodeString("utf-8") )
     }.flatten(FlattenStrategy.concat)
     .grouped( 1024 )
     .map( _.mkString )

它似乎运行良好(除了缺少错误路径),但对于这样简单的任务来说它有点笨拙。有更聪明的解决方案吗?我可以避开grouped / mkString吗?

2 个答案:

答案 0 :(得分:11)

您可以使用HttpResponse的toStrict方法超时。它收集了整个答案作为未来。

  

def toStrict(超时:FiniteDuration)(隐式ec:ExecutionContext,fm:Materializer):Future [Strict]返回可共享和可序列化的

     

使用严格实体复制此邮件。

示例:

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{HttpResponse, HttpRequest}
import akka.stream.{Materializer, ActorMaterializer}
import akka.stream.scaladsl.{Sink, Flow, Source}
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration._

import scala.util.{Try, Success}

object Main extends App {

  implicit val system = ActorSystem()

  import system.dispatcher

  implicit val materializer = ActorMaterializer()

  val host = "127.0.0.1"
  lazy val pool = Http().newHostConnectionPool[Int](host, 9000)

  FlowBuilder.get("/path", pool).to(Sink.foreach(_.foreach(println))).run()

}

object FlowBuilder {
  def get(modelID: String, pool: Flow[(HttpRequest, Int), (Try[HttpResponse], Int), Http.HostConnectionPool])
         (implicit ec: ExecutionContext, mat: Materializer): Source[Future[String], Unit] = {
    val uri = modelID
    val req = HttpRequest(uri = modelID)
    Source.single((req, 0)).via(pool)
      .map { 
      case (Success(resp), _) => resp.entity.toStrict(5 seconds).map(_.data.decodeString("UTF-8")) 
    }
  }
}

答案 1 :(得分:7)

您可以使用Unmarshall,这也适用于其他类型,例如来自spray-json的json。这也是strict返回Future[_]

示例:

  authedReq.via(authServerReqResFlow).mapAsync(1) { case (tryRes, _) =>
      tryRes match {
        case Failure(exception) => Future.failed[Principal](exception)
        case Success(response @ HttpResponse(StatusCodes.OK,_,_,_)) =>
          val userContext = Unmarshal(response).to[UserContextData]
          userContext.map {
            case UserContextData(UserInfo(_, userName, fullName, email, title), _, _) =>
              Principal(userName, fullName, email, title)
          }
        case Success(response @ HttpResponse(responseCode,_,entity,_)) =>
          Unmarshal(entity).to[String].flatMap(msg => Future.failed(new AuthenticationFailure(s"$responseCode\n$msg")))
      }
    }