Skip to content

Commit

Permalink
Fix review comments and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
SanojPunchihewa committed Jan 11, 2021
1 parent 81dbd9a commit a0db0eb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 46 deletions.
9 changes: 5 additions & 4 deletions import-export-cli/impl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ package impl
import (
"bytes"
"errors"
"github.com/Jeffail/gabs"
jsoniter "github.com/json-iterator/go"
"io"
"io/ioutil"
"mime/multipart"
Expand All @@ -31,6 +29,9 @@ import (
"strings"
"text/template"

"github.com/Jeffail/gabs"
jsoniter "github.com/json-iterator/go"

"github.com/go-resty/resty"
"github.com/wso2/product-apim-tooling/import-export-cli/box"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
Expand Down Expand Up @@ -75,7 +76,7 @@ func ExecuteNewFileUploadRequest(uri string, params map[string]string, paramName
headers[utils.HeaderAccept] = "application/json"
headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive

resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes())
resp, err := utils.InvokePOSTRequest(uri, headers, body.Bytes())

return resp, err
}
Expand Down Expand Up @@ -168,4 +169,4 @@ func IncludeMetaFileToZip(sourceZipFile, targetZipFile, metaFile string, metaDat
utils.HandleErrorAndExit("Error creating the final zip archive", err)
}
return nil
}
}
2 changes: 1 addition & 1 deletion import-export-cli/impl/importApp.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func NewAppFileUploadRequest(uri string, params map[string]string, paramName, pa
headers[utils.HeaderAccept] = "*/*"
headers[utils.HeaderConnection] = utils.HeaderValueKeepAlive

resp, err := utils.InvokePOSTRequestWithBytes(uri, headers, body.Bytes())
resp, err := utils.InvokePOSTRequest(uri, headers, body.Bytes())

return resp, err
}
18 changes: 11 additions & 7 deletions import-export-cli/mi/impl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"text/template"

"github.com/go-resty/resty"
"github.com/renstrom/dedent"
"github.com/wso2/product-apim-tooling/import-export-cli/credentials"
"github.com/wso2/product-apim-tooling/import-export-cli/formatter"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
Expand All @@ -37,6 +36,11 @@ import (
// miHTTPRetryCount default retry count for HTTP calls
const miHTTPRetryCount = 2

type updateArtifactRequestBody struct {
Name string `json:"name"`
Status string `json:"status"`
}

// unmarshalData unmarshal data from the response to the respective struct
// @param url: url of rest api
// @param params: parameters for the HTTP call
Expand Down Expand Up @@ -144,7 +148,7 @@ func invokePATCHRequestWithRetry(url string, body map[string]string, env string)
})
}

func invokePOSTRequestWithRetry(url, body, env string) (*resty.Response, error) {
func invokePOSTRequestWithRetry(env, url string, body interface{}) (*resty.Response, error) {
return retryHTTPCall(miHTTPRetryCount, env, func(accessToken string) (*resty.Response, error) {
headers := make(map[string]string)
headers[utils.HeaderAuthorization] = utils.HeaderValueAuthBearerPrefix + " " + accessToken
Expand Down Expand Up @@ -240,10 +244,10 @@ func createErrorWithResponseBody(resp string, err error) error {
}

func updateArtifactState(url, artifactName, state, env string) (string, error) {
body := dedent.Dedent(`{
"name": "` + artifactName + `",
"status": "` + state + `"
}`)
resp, err := invokePOSTRequestWithRetry(url, body, env)
body := updateArtifactRequestBody{
Name: artifactName,
Status: state,
}
resp, err := invokePOSTRequestWithRetry(env, url, body)
return handleResponse(resp, err, url, "Message", "Error")
}
24 changes: 14 additions & 10 deletions import-export-cli/mi/impl/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ package impl
import (
"strings"

"github.com/renstrom/dedent"
"github.com/wso2/product-apim-tooling/import-export-cli/utils"
)

type newUserRequestBody struct {
UserID string `json:"userID"`
Password string `json:"password"`
IsAdmin string `json:"isAdmin"`
}

// AddMIUser adds a new user to the micro integrator in a given environment
func AddMIUser(env, userName, password, isAdmin string) (interface{}, error) {
isAdmin = resolveIsAdmin(isAdmin)
body := dedent.Dedent(`{
"userId": "` + userName + `",
"password": "` + password + `",
"isAdmin": "` + isAdmin + `"
}`)

body := newUserRequestBody{
UserID: userName,
Password: password,
IsAdmin: isAdmin,
}
url := utils.GetMIManagementEndpointOfResource(utils.MiManagementUserResource, env, utils.MainConfigFilePath)
resp, err := addNewMIUser(url, body, env)
resp, err := addNewMIUser(env, url, body)
if err != nil {
return nil, createErrorWithResponseBody(resp, err)
}
Expand All @@ -52,8 +56,8 @@ func DeleteMIUser(env, userName string) (interface{}, error) {
return resp, nil
}

func addNewMIUser(url, body, env string) (string, error) {
resp, err := invokePOSTRequestWithRetry(url, body, env)
func addNewMIUser(env, url string, body interface{}) (string, error) {
resp, err := invokePOSTRequestWithRetry(env, url, body)
return handleResponse(resp, err, url, "status", "Error")
}

Expand Down
25 changes: 1 addition & 24 deletions import-export-cli/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,7 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

// Invoke http-post request using go-resty
func InvokePOSTRequest(url string, headers map[string]string, body string) (*resty.Response, error) {
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
} else {
resty.SetTLSClientConfig(GetTlsConfigWithCertificate())
}
if os.Getenv("HTTP_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTP_PROXY"))
} else if os.Getenv("HTTPS_PROXY") != "" {
resty.SetProxy(os.Getenv("HTTPS_PROXY"))
} else if os.Getenv("http_proxy") != "" {
resty.SetProxy(os.Getenv("http_proxy"))
} else if os.Getenv("https_proxy") != "" {
resty.SetProxy(os.Getenv("https_proxy"))
}
resty.SetTimeout(time.Duration(HttpRequestTimeout) * time.Millisecond)
resp, err := resty.R().SetHeaders(headers).SetBody(body).Post(url)

return resp, err
}

// Invoke http-post request using go-resty with byte[] body
func InvokePOSTRequestWithBytes(url string, headers map[string]string, body []byte) (*resty.Response, error) {
func InvokePOSTRequest(url string, headers map[string]string, body interface{}) (*resty.Response, error) {
if Insecure {
resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) // To bypass errors in SSL certificates
} else {
Expand Down

0 comments on commit a0db0eb

Please sign in to comment.