HttpResonse Headers to T的Unmarshaler

时间:2016-12-15 21:55:46

标签: scala akka-http

使用Akka-http客户端:

将HttpResponse标头解压缩或解组为案例类T的最佳方法是什么?我目前的尝试:

类UnmarshallerSpec使用带有WordSpecLike的ScalaFutures扩展AkkaSpec(“unmarshal”),使用带有BeforeAndAfterAll的Matchers和BeforeAndAfterEach {

SIFTEscalationMetadata

1 个答案:

答案 0 :(得分:0)

我改进了它。我以前的Unmarshaller尝试使用返回Optional的java API导致各种问题:

case class AmbryPostFileResponse(ambryId: String)

val ambryBlobInfo = new AmbryPostFileResponse("ambryId")

val testHttpResponse = HttpResponse(status = StatusCodes.OK, headers
    = List(Location("ambryId")))

      "Unmarshaler" should {
        "unmarshal" in {
          val result = Unmarshal(testHttpResponse).to[AmbryPostFileResponse]
          whenReady(result, timeout(10 seconds)) { r =>
            r shouldEqual ambryBlobInfo
          }
        }   }

    implicit final val fromUploadResponse: FromResponseUnmarshaller[AmbryPostFileResponse] = {

      //  val h: Class[Location] = headers.Location.getClass[Location]   def unmarshal(response: HttpResponse) = {

        val locheader = response
          .headers
          .collect {
            case l:Location => l
          }
          .headOption
          .getOrElse(throw new NoSuchElementException("header not found: Location"))

        AmbryPostFileResponse(AmbryId(locheader.uri.toString))   }   Unmarshaller.strict(unmarshal) }