Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change *http.Client transport

Tags:

go

The Status Quo

Having picked a side project (building a wrapper around a third party API), I'm stuck. I am using sling to compose my HTTP requests.

So parts of the Client are composed as follows:

type Client struct {
    // some services etc..
    sling *sling.Sling <-- this is initialized with *http.Client
}

func NewClient(httpClient *http.Client) *Client {
    sling := sling.New().Client(httpClient).Base(BaseURL)
}

//....

Things I can't wrap my head around

I am following the same principle as go-github and go-twitter that authentication should not be handled by my library, but rather by golangs oauth1/2 package.

As the the API provides application and user level authentication and some of the workflows require initial application level authentication and then user level authentication, my question is, if there is any way to change the *http.Transport in order to change the authentication header on a client basis.

So far, I haven't found a way to do so.

like image 869
Deniz Saner Avatar asked Apr 17 '17 07:04

Deniz Saner


1 Answers

The http.Client has a Transport field that you could use to "change the authentication header on a client basis" if that's what you want. The Transport field has type http.RoundTripper which is a one method interface, so all you need to do is to define your transport with an implementation of the RoundTrip method.

type MyTransport struct {
    apiKey string
    // keep a reference to the client's original transport
    rt http.RoundTripper
}

func (t *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    // set your auth headers here
    r.Header.Set("Auth", t.apiKey)
    return t.rt.RoundTrip(r)
}

Now you can use an instance of this type to set the Transport field on an http.Client.

var client *http.Client = // get client from somewhere...
// set the transport to your type
client.Transport = &MyTransport{apiKey: "secret", tr: client.Transport}

Depending on how and where from you got the client, it's possible that its Transport field is not yet set, so it might be a good idea to ensure that your type uses the default transport in such a case.

func (t *MyTransport) transport() http.RoundTripper {
    if t.rt != nil {
        return t.rt
    }
    return http.DefaultTransport
}

// update your method accordingly
func (t *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
    // set your auth headers here
    r.Header.Set("Auth", t.apiKey)
    return t.transport().RoundTrip(r)
}

It might be worth noting that the Go documentation recommends not to modify the *http.Request inside the RoundTrip method, so what you can do, and what the go-github package you linked to is doing, is to create a copy of the request, set the auth headers on it, and pass that to the underlying Transport. See here: https://github.com/google/go-github/blob/d23570d44313ca73dbcaadec71fc43eca4d29f8b/github/github.go#L841-L875

like image 92
mkopriva Avatar answered Sep 16 '22 11:09

mkopriva