Skip to content

Commit

Permalink
pkg/console: attempt request with new API on decode error
Browse files Browse the repository at this point in the history
When using the old graphQL endpoint, if the server responds with a body
that cannot be properly decoded, attempt to use the new http API instead.

Change-Id: Ide20ef407b75200fbf4f8cf3d83a5c20092c1cd5
  • Loading branch information
dlamarmorgan committed Jan 24, 2024
1 parent 65238b5 commit 07582ae
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions pkg/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"storj.io/storj/satellite/console/consoleauth"
)

var errDecodeResponse = errors.New("unable to decode json response")

// ConsoleEndpoint represents a user session to the web console.
type ConsoleEndpoint struct {
client *http.Client
Expand Down Expand Up @@ -250,14 +252,14 @@ func (ce *ConsoleEndpoint) activateUser(ctx context.Context, userID string, emai
// TODO: update this method when the old API is no longer needed.
func (ce *ConsoleEndpoint) GetOrCreateProject(ctx context.Context) (string, string, error) {
projectID, token, err := ce.getGraphqlProject(ctx)
if errors.Is(err, io.EOF) {
if errors.Is(err, io.EOF) || errors.Is(err, errDecodeResponse) {
projectID, token, err = ce.getHttpProject(ctx)
}
if err == nil {
return projectID, token, nil
}
projectID, token, err = ce.createGraphqlProject(ctx)
if errors.Is(err, io.EOF) {
if errors.Is(err, io.EOF) || errors.Is(err, errDecodeResponse) {
projectID, token, err = ce.createHttpProject(ctx)
}
if err == nil {
Expand Down Expand Up @@ -337,7 +339,7 @@ func (ce *ConsoleEndpoint) createGraphqlProject(ctx context.Context) (string, st
// TODO: update this method when the old API is no longer needed.
func (ce *ConsoleEndpoint) CreateAPIKey(ctx context.Context, projectID string) (string, error) {
key, err := ce.createGraphqlAPIKey(ctx, projectID)
if errors.Is(err, io.EOF) {
if errors.Is(err, io.EOF) || errors.Is(err, errDecodeResponse) {
key, err = ce.createAPIKey(ctx, projectID)
}
return key, err
Expand Down Expand Up @@ -531,7 +533,7 @@ func (ce *ConsoleEndpoint) graphqlDo(request *http.Request, jsonResponse interfa
}

if err = json.NewDecoder(bytes.NewReader(b)).Decode(&response); err != nil {
return err
return errDecodeResponse
}

if response.Errors != nil {
Expand Down

0 comments on commit 07582ae

Please sign in to comment.