Move Portainer API URL parsing outside the client

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-08-09 12:39:40 -04:00
parent cf9ecabc7a
commit 7fcdd73066
3 changed files with 28 additions and 22 deletions

View File

@ -9,7 +9,6 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strings"
) )
type StackListFilter struct { type StackListFilter struct {
@ -18,7 +17,7 @@ type StackListFilter struct {
} }
type Config struct { type Config struct {
Url string Url *url.URL
User string User string
Password string Password string
Token string Token string
@ -294,19 +293,12 @@ func (n *portainerClientImp) GetStatus() (status Status, err error) {
} }
// Create a new client // Create a new client
func NewClient(httpClient *http.Client, config Config) (c PortainerClient, err error) { func NewClient(httpClient *http.Client, config Config) PortainerClient {
apiUrl, err := url.Parse(strings.TrimRight(config.Url, "/") + "/api/") return &portainerClientImp{
if err != nil {
return
}
c = &portainerClientImp{
httpClient: httpClient, httpClient: httpClient,
url: apiUrl, url: config.Url,
user: config.User, user: config.User,
password: config.Password, password: config.Password,
token: config.Token, token: config.Token,
} }
return
} }

View File

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -25,11 +26,12 @@ func writeResponseBodyAsJson(w http.ResponseWriter, body map[string]interface{})
} }
func TestNewClient(t *testing.T) { func TestNewClient(t *testing.T) {
validClient, err := NewClient(http.DefaultClient, Config{ apiUrl, _ := url.Parse("http://validurl.com/api")
Url: "http://validurl.com",
validClient := NewClient(http.DefaultClient, Config{
Url: apiUrl,
}) })
assert.NotNil(t, validClient) assert.NotNil(t, validClient)
assert.Nil(t, err)
} }
func TestClientAuthenticates(t *testing.T) { func TestClientAuthenticates(t *testing.T) {
@ -54,12 +56,13 @@ func TestClientAuthenticates(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
customClient, err := NewClient(ts.Client(), Config{ apiUrl, _ := url.Parse(ts.URL + "/api/")
Url: ts.URL,
customClient := NewClient(ts.Client(), Config{
Url: apiUrl,
User: "admin", User: "admin",
Password: "a", Password: "a",
}) })
assert.Nil(t, err)
token, err := customClient.Authenticate() token, err := customClient.Authenticate()
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, token, "somerandomtoken") assert.Equal(t, token, "somerandomtoken")

View File

@ -5,6 +5,8 @@ import (
"crypto/tls" "crypto/tls"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"strings"
"github.com/greenled/portainer-stack-utils/client" "github.com/greenled/portainer-stack-utils/client"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -27,11 +29,13 @@ func GetClient() (c client.PortainerClient, err error) {
// Get the default client // Get the default client
func GetDefaultClient() (c client.PortainerClient, err error) { func GetDefaultClient() (c client.PortainerClient, err error) {
c, err = client.NewClient(GetDefaultHttpClient(), GetDefaultClientConfig()) config, err := GetDefaultClientConfig()
if err != nil { if err != nil {
return return
} }
c = client.NewClient(GetDefaultHttpClient(), config)
c.BeforeRequest(func(req *http.Request) (err error) { c.BeforeRequest(func(req *http.Request) (err error) {
var bodyString string var bodyString string
if req.Body != nil { if req.Body != nil {
@ -77,14 +81,21 @@ func GetDefaultClient() (c client.PortainerClient, err error) {
} }
// Get the default config for a client // Get the default config for a client
func GetDefaultClientConfig() client.Config { func GetDefaultClientConfig() (config client.Config, err error) {
return client.Config{ apiUrl, err := url.Parse(strings.TrimRight(viper.GetString("url"), "/") + "/api/")
Url: viper.GetString("url"), if err != nil {
return
}
config = client.Config{
Url: apiUrl,
User: viper.GetString("user"), User: viper.GetString("user"),
Password: viper.GetString("password"), Password: viper.GetString("password"),
Token: viper.GetString("auth-token"), Token: viper.GetString("auth-token"),
DoNotUseToken: false, DoNotUseToken: false,
} }
return
} }
// Get the default http client for a Portainer client // Get the default http client for a Portainer client