【发布时间】:2016-06-03 02:20:43
【问题描述】:
我有以下功能。
def get(id: Int): Future[Either[String, Item]] = {
request(RequestBuilding.Get(s"/log/$id")).flatMap { response =>
response.status match {
case OK => Unmarshal(response.entity).to[Item].map(Right(_))
case BadRequest => Future.successful(Left(s"Bad Request"))
case _ => Unmarshal(response.entity).to[String].flatMap { entity =>
val error = s"Request failed with status code ${response.status} and entity $entity"
Future.failed(new IOException(error))
}
}
}
}
我正在尝试调用此函数,但我不确定如何知道它返回的是字符串还是项。以下是我失败的尝试。
Client.get(1).onComplete { result =>
result match {
case Left(msg) => println(msg)
case Right(item) => // Do something
}
}
【问题讨论】: