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

View File

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

View File

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