Closed owenthereal closed 10 years ago
I prefer something like the RoundTripper interface to a middleware system.
What kind of stuff do you imagine setting in this in Octokit? I figure Octokit should support for various custom headers that the GitHub API needs.
The intention is to being able to set extra headers so that the cukes of the new Hub are happy. For example, I need to set a HOST_NAME
header to make the test pass.
RoundTripper
looks pretty cool. But the doc says:
RoundTrip should not modify the request, except for consuming and closing the Body. The request's URL and Header fields are guaranteed to be initialized.
I wonder if there's any problem if I mutate the headers. I'll do more research on that though.
Good call on adding more support on the custom headers that GitHub API requires in go-octokit
. Currently it only sets the mandatory headers. For those are optional, e.g. If-Modified-Since
, we don't provide options for that. So the outcome of this PR will be able to fix it.
@technoweenie I just read the source of how RoundTripper
is used. I didn't see any side effect of mutating the headers. But I'll ask in the Google group to confirm.
To use RoundTripper
as the middleware system, are you looking to use it like this? http://play.golang.org/p/pem5CZKInL
I don't think RoundTripper is really intended to modify a header like that. You typically have the actual request object while using the HTTP client, so you can just add your headers there.
RoundTripper might be better if you want a specific header on ALL requests. But go-sawyer should give you this capability already.
client := sawyer.NewFromString("https://api.github.com")
// header set on EVERY request
client.Headers.Set("Accept", "application/vnd.github+json")
apierr := &ApiError{} // decoded from response body on non-20x responses
user := &User{}
req := client.NewRequest("user/21", apierr)
// header set on just this request
req.Headers.Set("X-Foo", "Bar")
@technoweenie What do you think of 25a2655? I removed the middleware system and prefer Client.Header
instead. Unfortunately we don't expose the Request
object since go-octokit
was designed to be a higher level abstraction. There's one caveat to pay attention to though as we discussed in https://github.com/github/hub/pull/535#discussion_r11182262. Details see https://code.google.com/p/go/issues/detail?id=7682.
:+1:
Wohoo, merging
The changes allow us to have callbacks before sending requests and after receiving response (before the JSON deserialization). Previously the request/response cycle is all hidden and this'll open up an extension point. A use case in the new Hub:
And the cuke would be able to receive some custom headers.