Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
Namespace API support (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
myakhnis-shopify authored and sbfaulkner committed Nov 30, 2022
1 parent 558c2a7 commit 988fe3b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
27 changes: 25 additions & 2 deletions workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import (

// WorkerRequestParams provides parameters for worker requests for both enterprise and standard requests.
type WorkerRequestParams struct {
ZoneID string
ScriptName string
ZoneID string
ScriptName string
NamespaceName string
}

// WorkerScriptParams provides a worker script and the associated bindings.
Expand Down Expand Up @@ -729,6 +730,9 @@ func (api *API) UploadWorkerWithBindings(ctx context.Context, requestParams *Wor
if err != nil {
return WorkerScriptResponse{}, err
}
if requestParams.NamespaceName != "" && requestParams.ScriptName != "" {
return api.uploadWorkerWithNamespace(ctx, requestParams.NamespaceName, requestParams.ScriptName, contentType, body)
}
if requestParams.ScriptName != "" {
return api.uploadWorkerWithName(ctx, requestParams.ScriptName, contentType, body)
}
Expand Down Expand Up @@ -770,6 +774,25 @@ func (api *API) uploadWorkerWithName(ctx context.Context, scriptName, contentTyp
return r, nil
}

func (api *API) uploadWorkerWithNamespace(ctx context.Context, namespaceName, scriptName, contentType string, body []byte) (WorkerScriptResponse, error) {
if api.AccountID == "" {
return WorkerScriptResponse{}, errors.New("account ID required")
}
uri := fmt.Sprintf("/accounts/%s/workers/dispatch/namespaces/%s/scripts/%s", api.AccountID, namespaceName, scriptName)
headers := make(http.Header)
headers.Set("Content-Type", contentType)
res, err := api.makeRequestContextWithHeaders(ctx, http.MethodPut, uri, body, headers)
var r WorkerScriptResponse
if err != nil {
return r, err
}
err = json.Unmarshal(res, &r)
if err != nil {
return r, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r, nil
}

// Returns content-type, body, error.
func formatMultipartBody(params *WorkerScriptParams) (string, []byte, error) {
var buf = &bytes.Buffer{}
Expand Down
24 changes: 24 additions & 0 deletions workers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,30 @@ func TestWorkers_UploadWorkerWithDynamicDispatch(t *testing.T) {
assert.NoError(t, err)
}

func TestWorkers_UploadWorkerToNamespace(t *testing.T) {
setup(UsingAccount("foo"))
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)

mpUpload, err := parseMultipartUpload(r)
assert.NoError(t, err)

assert.Equal(t, workerScript, mpUpload.Script)

w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, uploadWorkerResponseData) //nolint
}
mux.HandleFunc("/accounts/foo/workers/dispatch/namespaces/sports/scripts/bar", handler)

scriptParams := WorkerScriptParams{
Script: workerScript,
}
_, err := client.UploadWorkerWithBindings(context.Background(), &WorkerRequestParams{ScriptName: "bar", NamespaceName: "sports"}, &scriptParams)
assert.NoError(t, err)
}

func TestWorkers_CreateWorkerRoute(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 988fe3b

Please sign in to comment.