HttpResonse Headers 到 T 的解组器
Unmarshaler for HttpResonse Headers to T
使用 Akka-http 客户端:
将 HttpResponse headers 提取或解组到 case class T 中的最佳方法是什么?我目前的尝试:
class UnmarshallerSpec extends AkkaSpec("unmarshal") with ScalaFutures with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfterEach {
import AmbryPostResponseUnmarshalling._
import akka.http.scaladsl.unmarshalling._
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
}
}
}
object AmbryPostResponseUnmarshalling extends AmbryPostResponseUnmarshalling
trait AmbryPostResponseUnmarshalling {
implicit final val fru: FromResponseUnmarshaller[AmbryPostFileResponse] = {
// val h: Class[Location] = headers.Location.getClass[Location]
def unmarshal(response: HttpResponse) =
AmbryPostFileResponse(response.getHeader("Location").get.asInstanceOf[Location].uri.toString())
Unmarshaller.strict(unmarshal)
}
}
}
我改进了它。我之前的 Unmarshaller 尝试使用 java API 返回 Optional 导致各种问题:
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) }
使用 Akka-http 客户端:
将 HttpResponse headers 提取或解组到 case class T 中的最佳方法是什么?我目前的尝试:
class UnmarshallerSpec extends AkkaSpec("unmarshal") with ScalaFutures with WordSpecLike with Matchers with BeforeAndAfterAll with BeforeAndAfterEach {
import AmbryPostResponseUnmarshalling._
import akka.http.scaladsl.unmarshalling._
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
}
}
}
object AmbryPostResponseUnmarshalling extends AmbryPostResponseUnmarshalling
trait AmbryPostResponseUnmarshalling {
implicit final val fru: FromResponseUnmarshaller[AmbryPostFileResponse] = {
// val h: Class[Location] = headers.Location.getClass[Location]
def unmarshal(response: HttpResponse) =
AmbryPostFileResponse(response.getHeader("Location").get.asInstanceOf[Location].uri.toString())
Unmarshaller.strict(unmarshal)
}
}
}
我改进了它。我之前的 Unmarshaller 尝试使用 java API 返回 Optional 导致各种问题:
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) }