【发布时间】:2019-09-30 10:53:13
【问题描述】:
我正在编写一个应用程序,它使用 GitHub API 来查看我的 GitHub 组织中的存储库。我正在使用github.com/google/go-github 库。
我也在使用github.com/gregjones/httpcache,这样我就可以进行基于令牌的身份验证以及为 API 调用设置条件标头。我已经进行了身份验证:
ctx := context.Background()
// GitHUb API authentication
transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: gh.tokens.GitHub.Token,
},
),
}
// Configure HTTP memory caching
transport = &httpcache.Transport{
Transport: transport,
Cache: httpcache.NewMemoryCache(),
MarkCachedResponses: true,
}
// Create the http client that GutHUb will use
httpClient := &http.Client{
Transport: transport,
}
// Attempt to login to GitHub
client := github.NewClient(httpClient)
但是,例如,当我使用 client.Repositories.Get 时,我无法弄清楚如何添加必要的 If-Match 标头。这样我就可以确定 repo 在过去 24 小时内是否发生了变化。
我已经搜索了如何执行此操作,但我遇到的示例显示了如何创建 HTTP 客户端,然后创建请求(因此可以添加标头),然后对其执行 Do 操作。但是,由于我直接使用客户端,所以我没有该选项。
go-github 的文档指出,对于条件请求:
GitHub API 对条件请求有很好的支持,这将有助于防止您超过速率限制,并有助于加快您的应用程序。 go-github 不直接处理条件请求,而是设计用于缓存 http.Transport。为此,我们建议使用https://github.com/gregjones/httpcache。
通过https://developer.github.com/v3/#conditional-requests了解有关 GitHub 条件请求的更多信息。
我不知道如何在我的代码中添加它,非常感谢任何帮助。
【问题讨论】: