使用 Go-Github 和 http.Transport 时如何设置 HTTP 请求 headers?
How can I set HTTP request headers when using Go-Github and an http.Transport?
我正在编写一个应用程序,它使用 GitHub API 查看我的 GitHub 组织中的存储库。我正在使用 github.com/google/go-github
库。
我也在使用 github.com/gregjones/httpcache
,这样我就可以进行基于令牌的身份验证以及为 API 调用设置条件 header。我的身份验证工作是这样的:
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
header。例如,这样我就可以确定回购协议是否在过去 24 小时内发生了变化。
我搜索了如何执行此操作,但我遇到的示例显示了如何创建 HTTP 客户端,然后创建请求(因此可以添加 headers)然后执行 Do
对其采取行动。但是,因为我直接使用客户端,所以我没有那个选项。
go-github
的文档指出,对于条件请求:
The GitHub API has good support for conditional requests which will help prevent you from burning through your rate limit, as well as help speed up your application. go-github does not handle conditional requests directly, but is instead designed to work with a caching http.Transport. We recommend using https://github.com/gregjones/httpcache for that.
Learn more about GitHub conditional requests at https://developer.github.com/v3/#conditional-requests.
我不知道如何在我的代码中添加它,非常感谢任何帮助。
就像这些事情一样,在发布我的问题后不久我就找到了答案。
诀窍是在 Oauth2 传输中使用 Base
设置 headers:
transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: gh.tokens.GitHub.Token,
},
),
Base: &transportHeaders{
modifiedSince: modifiedSince,
},
}
结构和方法如下所示:
type transportHeaders struct {
modifiedSince string
}
func (t *transportHeaders) RoundTrip(req *http.Request) (*http.Response, error) {
// Determine the last modified date based on the transportHeader options
// Do not add any headers if blank or zero
if t.modifiedSince != "" {
req.Header.Set("If-Modified-Since", t.modifiedSince)
}
return http.DefaultTransport.RoundTrip(req)
}
所以通过这样做我可以拦截对 RoundTrip 的调用并添加我自己的 header。这意味着我现在可以检查资源并查看它们是否 return 304 HTTP 状态代码。例如:
ERRO[0001] Error retrieving repository error="GET https://api.github.com/repos/chef-partners/camsa-setup: 304 []" name=camsa-setup vcs=github
看到这个页面后,我想出了如何做到这一点 - https://github.com/rmichela/go-reddit/blob/bd882abbb7496c54dbde66d92c35ad95d4db1211/authenticator.go#L117
我正在编写一个应用程序,它使用 GitHub API 查看我的 GitHub 组织中的存储库。我正在使用 github.com/google/go-github
库。
我也在使用 github.com/gregjones/httpcache
,这样我就可以进行基于令牌的身份验证以及为 API 调用设置条件 header。我的身份验证工作是这样的:
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
header。例如,这样我就可以确定回购协议是否在过去 24 小时内发生了变化。
我搜索了如何执行此操作,但我遇到的示例显示了如何创建 HTTP 客户端,然后创建请求(因此可以添加 headers)然后执行 Do
对其采取行动。但是,因为我直接使用客户端,所以我没有那个选项。
go-github
的文档指出,对于条件请求:
The GitHub API has good support for conditional requests which will help prevent you from burning through your rate limit, as well as help speed up your application. go-github does not handle conditional requests directly, but is instead designed to work with a caching http.Transport. We recommend using https://github.com/gregjones/httpcache for that.
Learn more about GitHub conditional requests at https://developer.github.com/v3/#conditional-requests.
我不知道如何在我的代码中添加它,非常感谢任何帮助。
就像这些事情一样,在发布我的问题后不久我就找到了答案。
诀窍是在 Oauth2 传输中使用 Base
设置 headers:
transport = &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: gh.tokens.GitHub.Token,
},
),
Base: &transportHeaders{
modifiedSince: modifiedSince,
},
}
结构和方法如下所示:
type transportHeaders struct {
modifiedSince string
}
func (t *transportHeaders) RoundTrip(req *http.Request) (*http.Response, error) {
// Determine the last modified date based on the transportHeader options
// Do not add any headers if blank or zero
if t.modifiedSince != "" {
req.Header.Set("If-Modified-Since", t.modifiedSince)
}
return http.DefaultTransport.RoundTrip(req)
}
所以通过这样做我可以拦截对 RoundTrip 的调用并添加我自己的 header。这意味着我现在可以检查资源并查看它们是否 return 304 HTTP 状态代码。例如:
ERRO[0001] Error retrieving repository error="GET https://api.github.com/repos/chef-partners/camsa-setup: 304 []" name=camsa-setup vcs=github
看到这个页面后,我想出了如何做到这一点 - https://github.com/rmichela/go-reddit/blob/bd882abbb7496c54dbde66d92c35ad95d4db1211/authenticator.go#L117