Return error on 202 responses

- Start implementing the retry/poll flow
This commit is contained in:
Jose Garcia 2021-09-21 09:59:16 -04:00
parent 85f79ed8e8
commit 0b68aaab7e
2 changed files with 24 additions and 2 deletions

View file

@ -6,6 +6,7 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/fatih/camelcase"
@ -87,8 +88,20 @@ func create(opts *createOptions) error {
log.Println("Creating your codespace...")
codespace, err := apiClient.CreateCodespace(ctx, userResult.User, repository, machine, branch, locationResult.Location)
codespace, err := apiClient.CreateCodespace(
ctx, userResult.User, repository, machine, branch, locationResult.Location,
)
if err != nil {
if err == api.ErrCreateAsyncRetry {
createRetryCtx, cancelRetry := context.WithTimeout(ctx, 2*time.Minute)
defer cancelRetry()
codespace, err = pollForProvisionedCodespace(createRetryCtx, codespace)
if err != nil {
return fmt.Errorf("error creating codespace after retry: %w", err)
}
}
return fmt.Errorf("error creating codespace: %w", err)
}
@ -105,6 +118,10 @@ func create(opts *createOptions) error {
return nil
}
func pollForProvisionedCodespace(ctx context.Context, provisioningCodespace *api.Codespace) (*api.Codespace, error) {
return nil, nil
}
// showStatus polls the codespace for a list of post create states and their status. It will keep polling
// until all states have finished. Once all states have finished, we poll once more to check if any new
// states have been introduced and stop polling otherwise.

View file

@ -401,6 +401,8 @@ type createCodespaceRequest struct {
SkuName string `json:"sku_name"`
}
var ErrCreateAsyncRetry = errors.New("initial creation failed, retrying async")
func (a *API) CreateCodespace(ctx context.Context, user *User, repository *Repository, sku, branch, location string) (*Codespace, error) {
requestBody, err := json.Marshal(createCodespaceRequest{repository.ID, branch, location, sku})
if err != nil {
@ -424,8 +426,11 @@ func (a *API) CreateCodespace(ctx context.Context, user *User, repository *Repos
return nil, fmt.Errorf("error reading response body: %w", err)
}
if resp.StatusCode > http.StatusAccepted {
switch {
case resp.StatusCode > http.StatusAccepted:
return nil, jsonErrorResponse(b)
case resp.StatusCode == http.StatusAccepted:
return nil, ErrCreateAsyncRetry
}
var response Codespace