Skip to content

Commit

Permalink
Support http and https. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
keep94 authored Mar 18, 2021
1 parent 3b2e0dd commit 8ee4fdb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 11 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http/httputil"
"net/url"
"reflect"
"strings"
"time"
)

Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
15 changes: 14 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := `
{
Expand Down

0 comments on commit 8ee4fdb

Please sign in to comment.