douzhonglong3789 2017-04-17 07:48
浏览 26
已采纳

更改* http.Client传输

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.

  • 写回答

1条回答 默认 最新

  • douduxia1551 2017-04-17 09:26
    关注

    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/master/github/github.go#L841-L875

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算