Export doJSONWithToken

This commit is contained in:
Juan Carlos Mejías Rodríguez 2019-09-10 19:03:39 -04:00
parent 49eb081130
commit 63c82efd33
12 changed files with 16 additions and 14 deletions

View File

@ -69,6 +69,9 @@ type PortainerClient interface {
// GetUsername returns the user name used by the client
GetUsername() string
// DoJSONWithToken makes an HTTP request with a JSON body and an auth token
DoJSONWithToken(uri, method string, headers http.Header, request interface{}, response interface{}) (err error)
}
type portainerClientImp struct {
@ -178,8 +181,7 @@ func (n *portainerClientImp) doJSON(uri, method string, headers http.Header, req
return nil
}
// Do a JSON http request with an auth token
func (n *portainerClientImp) doJSONWithToken(uri, method string, headers http.Header, request interface{}, response interface{}) (err error) {
func (n *portainerClientImp) DoJSONWithToken(uri, method string, headers http.Header, request interface{}, response interface{}) (err error) {
// Ensure there is an auth token
if n.token == "" {
n.token, err = n.AuthenticateUser(AuthenticateUserOptions{

View File

@ -398,7 +398,7 @@ func Test_portainerClientImp_doJSONWithToken(t *testing.T) {
token: tt.fields.token,
}
err := n.doJSONWithToken(tt.args.uri, tt.args.method, tt.args.headers, &tt.args.requestBody, &tt.args.responseBody)
err := n.DoJSONWithToken(tt.args.uri, tt.args.method, tt.args.headers, &tt.args.requestBody, &tt.args.responseBody)
assert.Equal(t, tt.wantErr, err != nil)
assert.Equal(t, tt.wantRespBody, tt.args.responseBody)
})

View File

@ -7,6 +7,6 @@ import (
)
func (n *portainerClientImp) EndpointGroupList() (endpointGroups []portainer.EndpointGroup, err error) {
err = n.doJSONWithToken("endpoint_groups", http.MethodGet, http.Header{}, nil, &endpointGroups)
err = n.DoJSONWithToken("endpoint_groups", http.MethodGet, http.Header{}, nil, &endpointGroups)
return
}

View File

@ -8,6 +8,6 @@ import (
)
func (n *portainerClientImp) EndpointDockerInfo(endpointID portainer.EndpointID) (info map[string]interface{}, err error) {
err = n.doJSONWithToken(fmt.Sprintf("endpoints/%v/docker/info", endpointID), http.MethodGet, http.Header{}, nil, &info)
err = n.DoJSONWithToken(fmt.Sprintf("endpoints/%v/docker/info", endpointID), http.MethodGet, http.Header{}, nil, &info)
return
}

View File

@ -7,6 +7,6 @@ import (
)
func (n *portainerClientImp) EndpointList() (endpoints []portainer.Endpoint, err error) {
err = n.doJSONWithToken("endpoints", http.MethodGet, http.Header{}, nil, &endpoints)
err = n.DoJSONWithToken("endpoints", http.MethodGet, http.Header{}, nil, &endpoints)
return
}

View File

@ -15,7 +15,7 @@ type StackFileInspectResponse struct {
func (n *portainerClientImp) StackFileInspect(stackID portainer.StackID) (content string, err error) {
var respBody StackFileInspectResponse
err = n.doJSONWithToken(fmt.Sprintf("stacks/%v/file", stackID), http.MethodGet, http.Header{}, nil, &respBody)
err = n.DoJSONWithToken(fmt.Sprintf("stacks/%v/file", stackID), http.MethodGet, http.Header{}, nil, &respBody)
if err != nil {
return
}

View File

@ -39,7 +39,7 @@ func (n *portainerClientImp) StackCreateCompose(options StackCreateComposeOption
StackFileContent: options.StackFileContent,
}
err = n.doJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 2, "string", options.EndpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
err = n.DoJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 2, "string", options.EndpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
return
}
@ -51,6 +51,6 @@ func (n *portainerClientImp) StackCreateSwarm(options StackCreateSwarmOptions) (
StackFileContent: options.StackFileContent,
}
err = n.doJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 1, "string", options.EndpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
err = n.DoJSONWithToken(fmt.Sprintf("stacks?type=%v&method=%s&endpointId=%v", 1, "string", options.EndpointID), http.MethodPost, http.Header{}, &reqBody, &stack)
return
}

View File

@ -8,6 +8,6 @@ import (
)
func (n *portainerClientImp) StackDelete(stackID portainer.StackID) (err error) {
err = n.doJSONWithToken(fmt.Sprintf("stacks/%d", stackID), http.MethodDelete, http.Header{}, nil, nil)
err = n.DoJSONWithToken(fmt.Sprintf("stacks/%d", stackID), http.MethodDelete, http.Header{}, nil, nil)
return
}

View File

@ -23,6 +23,6 @@ func (n *portainerClientImp) StackList(options StackListOptions) (stacks []porta
filterJSONBytes, _ := json.Marshal(options.Filter)
filterJSONString := string(filterJSONBytes)
err = n.doJSONWithToken(fmt.Sprintf("stacks?filters=%s", filterJSONString), http.MethodGet, http.Header{}, nil, &stacks)
err = n.DoJSONWithToken(fmt.Sprintf("stacks?filters=%s", filterJSONString), http.MethodGet, http.Header{}, nil, &stacks)
return
}

View File

@ -30,6 +30,6 @@ func (n *portainerClientImp) StackUpdate(options StackUpdateOptions) (err error)
Prune: options.Prune,
}
err = n.doJSONWithToken(fmt.Sprintf("stacks/%v?endpointId=%v", options.Stack.ID, options.EndpointID), http.MethodPut, http.Header{}, &reqBody, nil)
err = n.DoJSONWithToken(fmt.Sprintf("stacks/%v?endpointId=%v", options.Stack.ID, options.EndpointID), http.MethodPut, http.Header{}, &reqBody, nil)
return
}

View File

@ -7,6 +7,6 @@ import (
)
func (n *portainerClientImp) Status() (status portainer.Status, err error) {
err = n.doJSONWithToken("status", http.MethodGet, http.Header{}, nil, &status)
err = n.DoJSONWithToken("status", http.MethodGet, http.Header{}, nil, &status)
return
}

View File

@ -7,6 +7,6 @@ import (
)
func (n *portainerClientImp) UserList() (users []portainer.User, err error) {
err = n.doJSONWithToken("users", http.MethodGet, http.Header{}, nil, &users)
err = n.DoJSONWithToken("users", http.MethodGet, http.Header{}, nil, &users)
return
}