From 8ee4fdb939607196c193ea8bdf5d5149199e6ad3 Mon Sep 17 00:00:00 2001 From: keep94 Date: Thu, 18 Mar 2021 15:15:50 -0700 Subject: [PATCH] Support http and https. (#41) --- client.go | 13 +++++++++++-- client_test.go | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index b1404883..1acf8301 100644 --- a/client.go +++ b/client.go @@ -14,6 +14,7 @@ import ( "net/http/httputil" "net/url" "reflect" + "strings" "time" ) @@ -26,7 +27,8 @@ type Wavefronter interface { // Config is used to hold configuration used when constructing a Client type Config struct { - // Address is the address of the Wavefront API, of the form example.wavefront.com + // Address is the address of the Wavefront API, of the form + // example.wavefront.com or http://localhost:8080. Address string // Token is an authentication token that will be passed with all requests @@ -59,9 +61,16 @@ type Client struct { debug bool } +func fixAddress(address string) string { + if !strings.HasPrefix(address, "http") { + address = "https://" + address + } + return address + "/api/v2/" +} + // NewClient returns a new Wavefront client according to the given Config func NewClient(config *Config) (*Client, error) { - baseURL, err := url.Parse("https://" + config.Address + "/api/v2/") + baseURL, err := url.Parse(fixAddress(config.Address)) if err != nil { return nil, err } diff --git a/client_test.go b/client_test.go index 495f4012..9900ddd3 100644 --- a/client_test.go +++ b/client_test.go @@ -280,12 +280,25 @@ func TestConfigDefensiveCopy(t *testing.T) { Token: "123456789", SkipTLSVerify: true, } - client, _ := NewClient(config) + client, err := NewClient(config) + assert.NoError(err) assert.NotSame(config, client.Config) assert.Equal("somehost.wavefront.com", client.Config.Address) assert.Equal("123456789", client.Config.Token) } +func TestHttpConnection(t *testing.T) { + assert := asserts.New(t) + config := &Config{ + Address: "http://localhost:8080", + Token: "987654321", + SkipTLSVerify: true, + } + client, err := NewClient(config) + assert.NoError(err) + assert.Equal("http://localhost:8080/api/v2/", client.BaseURL.String()) +} + func TestDoRest_DirectResponse(t *testing.T) { responseStr := ` {