Skip to content

Commit

Permalink
style: Addressed PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Aug 8, 2023
1 parent 416053e commit 188282b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (c *Client) DoRequest(req *http.Request, resp interface{}) (int, error) {
}{}
body, err := io.ReadAll(httpResp.Body)
if err != nil {
return httpResp.StatusCode, err
return 0, err
}
err = json.Unmarshal(body, &httpError)
if err != nil {
Expand Down
19 changes: 13 additions & 6 deletions config/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package config

import (
"fmt"
"net/http"
"net/url"

"github.com/CircleCI-Public/circleci-cli/api/graphql"
"github.com/CircleCI-Public/circleci-cli/api/rest"
"github.com/CircleCI-Public/circleci-cli/settings"
)

var (
compilePath = "compile-config-with-defaults"
)
const compilePath = "compile-config-with-defaults"

type apiClientVersion string

Expand All @@ -38,17 +37,25 @@ func newAPIClient(config *settings.Config) (APIClient, error) {
}
}

// detectAPIClientVersion returns the highest available version of the config API.
//
// To do that it tries to request the `compilePath` API route.
// If the route returns a 404, this means the route does not exist on the requested host and the function returns
// `v1_string` indicating that the deprecated GraphQL endpoint should be used instead.
// Else if the route returns any other status, this means it is available for request and the function returns
// `v2_string` indicating that the route can be used
func detectAPIClientVersion(restClient *rest.Client) (apiClientVersion, error) {
req, err := restClient.NewRequest("POST", &url.URL{Path: compilePath}, nil)
if err != nil {
return "", err
}

statusCode, err := restClient.DoRequest(req, nil)
if _, ok := err.(*rest.HTTPError); !ok {
_, err = restClient.DoRequest(req, nil)
httpErr, ok := err.(*rest.HTTPError)
if !ok {
return "", err
}
if statusCode == 404 {
if httpErr.Code == http.StatusNotFound {
return v1_string, nil
}
return v2_string, nil
Expand Down

0 comments on commit 188282b

Please sign in to comment.