From 5d654d22fd9888ff88eefda24d5bcba4f45cb1a6 Mon Sep 17 00:00:00 2001 From: gondor Date: Fri, 14 Jul 2017 17:18:33 -0700 Subject: [PATCH] update http client to support token auth --- httpclient/client.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/httpclient/client.go b/httpclient/client.go index b6edd4f..5155e94 100644 --- a/httpclient/client.go +++ b/httpclient/client.go @@ -11,6 +11,7 @@ import ( "time" "github.com/ContainX/go-utils/encoding" "github.com/ContainX/go-utils/logger" + "fmt" ) var log = logger.GetLogger("httpclient") @@ -45,6 +46,8 @@ type HttpClientConfig struct { HttpUser string // Http Basic Auth Password HttpPass string + // Access Token will be applied to all requests if set + AccessToken string // Request timeout RequestTimeout int // TLS Insecure Skip Verify @@ -73,7 +76,7 @@ type HttpClient interface { // Post the data against the specified url and unmarshal the // response into the result if it is not nil - Post(url string, data interface{}, result interface{}) + Post(url string, data interface{}, result interface{}) *Response } var ( @@ -98,15 +101,15 @@ func NewDefaultConfig() *HttpClientConfig { } // DefaultHttpClient provides a basic default http client -func DefaultHttpClient() *httpClient { +func DefaultHttpClient() HttpClient { return NewHttpClient(*NewDefaultConfig()) } -func NewHttpClient(config HttpClientConfig) *httpClient { +func NewHttpClient(config HttpClientConfig) HttpClient { hc := &httpClient{ config: config, http: &http.Client{ - Timeout: (time.Duration(config.RequestTimeout) * time.Second), + Timeout: time.Duration(config.RequestTimeout) * time.Second, }, } if config.TLSInsecureSkipVerify { @@ -267,4 +270,8 @@ func addAuthentication(c HttpClientConfig, req *http.Request) { if c.HttpUser != "" { req.SetBasicAuth(c.HttpUser, c.HttpPass) } + + if c.AccessToken != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.AccessToken)) + } }