Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
joanlopez committed Sep 9, 2024
1 parent 2324996 commit b3778a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
55 changes: 21 additions & 34 deletions cloudapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ func (c *Client) TestFinished(referenceID string, thresholds ThresholdResult, ta

// GetTestProgress for the provided referenceID.
func (c *Client) GetTestProgress(referenceID string) (*TestProgressResponse, error) {
url := fmt.Sprintf("%s/test-progress/%s", c.baseURL, referenceID)
req, err := c.NewRequest(http.MethodGet, url, nil)
req, err := c.NewRequest(http.MethodGet, c.baseURL+"/test-progress/"+referenceID, nil)
if err != nil {
return nil, err
}
Expand All @@ -212,47 +211,38 @@ func (c *Client) GetTestProgress(referenceID string) (*TestProgressResponse, err

// StopCloudTestRun tells the cloud to stop the test with the provided referenceID.
func (c *Client) StopCloudTestRun(referenceID string) error {
url := fmt.Sprintf("%s/tests/%s/stop", c.baseURL, referenceID)

req, err := c.NewRequest("POST", url, nil)
req, err := c.NewRequest("POST", c.baseURL+"/tests/"+referenceID+"/stop", nil)
if err != nil {
return err
}

return c.Do(req, nil)
}

type validateOptionsRequest struct {
Options lib.Options `json:"options"`
}

// ValidateOptions sends the provided options to the cloud for validation.
func (c *Client) ValidateOptions(options lib.Options) error {
url := fmt.Sprintf("%s/validate-options", c.baseURL)

data := struct {
Options lib.Options `json:"options"`
}{
options,
}

req, err := c.NewRequest("POST", url, data)
data := validateOptionsRequest{Options: options}
req, err := c.NewRequest("POST", c.baseURL+"/validate-options", data)
if err != nil {
return err
}

return c.Do(req, nil)
}

type loginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}

// Login the user with the specified email and password.
func (c *Client) Login(email string, password string) (*LoginResponse, error) {
url := fmt.Sprintf("%s/login", c.baseURL)

data := struct {
Email string `json:"email"`
Password string `json:"password"`
}{
email,
password,
}

req, err := c.NewRequest("POST", url, data)
data := loginRequest{Email: email, Password: password}
req, err := c.NewRequest("POST", c.baseURL+"/login", data)
if err != nil {
return nil, err
}
Expand All @@ -266,17 +256,14 @@ func (c *Client) Login(email string, password string) (*LoginResponse, error) {
return &lr, nil
}

type validateTokenRequest struct {
Token string `json:"token"`
}

// ValidateToken calls the endpoint to validate the Client's token and returns the result.
func (c *Client) ValidateToken() (*ValidateTokenResponse, error) {
url := fmt.Sprintf("%s/validate-token", c.baseURL)

data := struct {
Token string `json:"token"`
}{
c.token,
}

req, err := c.NewRequest("POST", url, data)
data := validateTokenRequest{Token: c.token}
req, err := c.NewRequest("POST", c.baseURL+"/validate-token", data)
if err != nil {
return nil, err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/cloud_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,12 @@ func (c *cmdCloudLogin) run(cmd *cobra.Command, _ []string) error {
var res *cloudapi.ValidateTokenResponse
res, err = client.ValidateToken()
if err != nil {
return err
return fmt.Errorf("can't validate the API token: %s", err)

Check failure on line 149 in cmd/cloud_login.go

View workflow job for this annotation

GitHub Actions / lint

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}

if !res.IsValid {
return errors.New(`your API token is invalid, please generate a new one at https://app.k6.io/account/api-token`)
return errors.New("your API token is invalid, please, consult the Grafana Cloud k6 documentation for instructions on how to generate a new one:\n" +

Check failure on line 153 in cmd/cloud_login.go

View workflow job for this annotation

GitHub Actions / lint

the line is 151 characters long, which exceeds the maximum of 120 characters. (lll)
"https://grafana.com/docs/grafana-cloud/testing/k6/author-run/tokens-and-cli-authentication")
}
}

Expand Down

0 comments on commit b3778a3

Please sign in to comment.