我已经创建了一个使用Hammock
(https://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
?
我收到回复后通过使用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))