0

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

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数据映射到我的对象。但现在我也想从中得到headersresponse我不能简单地做到这一点response.headers。只有当我删除.as[List[GitRepository]]线并拥有完整的HttpResponse我才能访问headers. 是否有可能在headers不解析整体的情况下获得HttpResponse

4

1 回答 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))
于 2019-11-10T13:32:47.700 回答