Scala,Hammock-检索http响应标头并将JSON转换为自定义对象

问题描述 投票:0回答:1

我已经创建了一个使用Hammockhttps://github.com/pepegar/hammock)的简单程序,现在我想从带有回购邮件头的github API中获取响应。我创建了这样的代码:

object GitHttpClient extends App {
  implicit val decoder = jsonOf[IO, List[GitRepository]]
  implicit val interpreter = ApacheInterpreter.instance[IO]

  val response = Hammock
    .request(Method.GET, uri"https://api.github.com/orgs/github/repos?per_page=3", Map())
    .as[List[GitRepository]]
    .exec[IO]
    .unsafeRunSync()

  println(response)
}

case class GitRepository(full_name: String, contributors_url: String)

并且工作正常,我将Git数据映射到我的对象。但是现在我也想从headers中获取response,而我无法通过简单的response.headers来做到这一点。仅当我删除.as[List[GitRepository]]行并且具有整个HttpResponse时,我才能访问headers。是否可以在不解析整个headers的情况下获取HttpResponse

scala scala-cats hammock
1个回答
0
投票

我收到回复后通过使用Decoder解决了这个问题:

 val response = Hammock
    .request(Method.GET, uri"https://api.github.com/orgs/github/repos?per_page=3", Map())
    .exec[IO]
    .unsafeRunSync()

    println(response.headers("Link") contains ("next"))
    println(HammockDecoder[List[GitRepository]].decode(response.entity))
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.