Repository: cloudflare/cloudflare-go
Pull request #1031 (2022-08-10 02:12:05)

Add support for Workers subdomain API
## Description
Adds support for Workers Subdomain API

I also updated the JSON marshal error for https://github.com/cloudflare/cloudflare-go/pull/1027 and https://github.com/cloudflare/cloudflare-go/pull/1026

## Has your change been tested?

Unit tested added and all pass

## Screenshots (if appropriate):

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [X] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [X] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [X] I have added tests to cover my changes.
- [X] All new and existing tests passed.
- [X] This change is using publicly documented (api.cloudflare.com or developers.cloudflare.com) and stable APIs.




Patch:
diff --git a/.changelog/1031.txt b/.changelog/1031.txt
new file mode 100644
index 000000000..3d039fe45
--- /dev/null
+++ b/.changelog/1031.txt
@@ -0,0 +1,3 @@
+```release-note:enhancement
+workers-subdomain: Add in support Workers Subdomain API
+```
\ No newline at end of file
diff --git a/workers_account_settings.go b/workers_account_settings.go
index 942db6dd0..d8e58516a 100644
--- a/workers_account_settings.go
+++ b/workers_account_settings.go
@@ -45,7 +45,7 @@ func (api *API) CreateWorkersAccountSettings(ctx context.Context, rc *ResourceCo
 
 	var workersAccountSettingsResponse CreateWorkersAccountSettingsResponse
 	if err := json.Unmarshal(res, &workersAccountSettingsResponse); err != nil {
-		return WorkersAccountSettings{}, err
+		return WorkersAccountSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
 	return workersAccountSettingsResponse.Result, nil
@@ -67,7 +67,7 @@ func (api *API) WorkersAccountSettings(ctx context.Context, rc *ResourceContaine
 
 	var workersAccountSettingsResponse CreateWorkersAccountSettingsResponse
 	if err := json.Unmarshal(res, &workersAccountSettingsResponse); err != nil {
-		return WorkersAccountSettings{}, err
+		return WorkersAccountSettings{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
 	return workersAccountSettingsResponse.Result, nil
diff --git a/workers_subdomain.go b/workers_subdomain.go
new file mode 100644
index 000000000..f67e7ad02
--- /dev/null
+++ b/workers_subdomain.go
@@ -0,0 +1,57 @@
+package cloudflare
+
+import (
+	"context"
+	"encoding/json"
+	"fmt"
+	"net/http"
+)
+
+type WorkersSubdomain struct {
+	Name string `json:"name,omitempty"`
+}
+
+type WorkersSubdomainResponse struct {
+	Response
+	Result WorkersSubdomain
+}
+
+// WorkersCreateSubdomain Creates a Workers subdomain for an account.
+//
+// API reference: https://api.cloudflare.com/#worker-subdomain-create-subdomain
+func (api *API) WorkersCreateSubdomain(ctx context.Context, rc *ResourceContainer, params WorkersSubdomain) (WorkersSubdomain, error) {
+	if rc.Identifier == "" {
+		return WorkersSubdomain{}, ErrMissingAccountID
+	}
+
+	uri := fmt.Sprintf("/accounts/%s/workers/subdomain", rc.Identifier)
+	res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
+	if err != nil {
+		return WorkersSubdomain{}, err
+	}
+	var r WorkersSubdomainResponse
+	if err := json.Unmarshal(res, &r); err != nil {
+		return WorkersSubdomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+	return r.Result, nil
+}
+
+// WorkersGetSubdomain Creates a Workers subdomain for an account.
+//
+// API reference: https://api.cloudflare.com/#worker-subdomain-get-subdomain
+func (api *API) WorkersGetSubdomain(ctx context.Context, rc *ResourceContainer) (WorkersSubdomain, error) {
+	if rc.Identifier == "" {
+		return WorkersSubdomain{}, ErrMissingAccountID
+	}
+
+	uri := fmt.Sprintf("/accounts/%s/workers/subdomain", rc.Identifier)
+	res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
+	if err != nil {
+		return WorkersSubdomain{}, err
+	}
+	var r WorkersSubdomainResponse
+	if err := json.Unmarshal(res, &r); err != nil {
+		return WorkersSubdomain{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
+	}
+	return r.Result, nil
+}
diff --git a/workers_tail.go b/workers_tail.go
index 672f82191..478112b4a 100644
--- a/workers_tail.go
+++ b/workers_tail.go
@@ -54,7 +54,7 @@ func (api *API) StartWorkersTail(ctx context.Context, rc *ResourceContainer, scr
 
 	var workerstailResponse StartWorkersTailResponse
 	if err := json.Unmarshal(res, &workerstailResponse); err != nil {
-		return WorkersTail{}, err
+		return WorkersTail{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
 	return workerstailResponse.Result, nil
@@ -80,7 +80,7 @@ func (api *API) ListWorkersTail(ctx context.Context, rc *ResourceContainer, para
 
 	var workerstailResponse ListWorkersTailResponse
 	if err := json.Unmarshal(res, &workerstailResponse); err != nil {
-		return WorkersTail{}, err
+		return WorkersTail{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
 	}
 
 	return workerstailResponse.Result, nil


Test patch:
diff --git a/workers_subdomain_test.go b/workers_subdomain_test.go
new file mode 100644
index 000000000..33b12886e
--- /dev/null
+++ b/workers_subdomain_test.go
@@ -0,0 +1,71 @@
+package cloudflare
+
+import (
+	"context"
+	"fmt"
+	"github.com/stretchr/testify/assert"
+	"net/http"
+	"testing"
+)
+
+func TestWorkersSubdomain_CreateSubdomain(t *testing.T) {
+	setup()
+	defer teardown()
+
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/workers/subdomain", testAccountID), func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": {
+    "name": "example-subdomain"
+  }
+}`)
+	})
+
+	_, err := client.WorkersCreateSubdomain(context.Background(), AccountIdentifier(""), WorkersSubdomain{})
+	if assert.Error(t, err) {
+		assert.Equal(t, ErrMissingAccountID, err)
+	}
+
+	res, err := client.WorkersCreateSubdomain(context.Background(), AccountIdentifier(testAccountID), WorkersSubdomain{Name: "example-subdomain"})
+
+	want := WorkersSubdomain{Name: "example-subdomain"}
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, res)
+	}
+}
+
+func TestWorkersSubdomain_GetSubdomain(t *testing.T) {
+	setup()
+	defer teardown()
+
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/workers/subdomain", testAccountID), func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+  "success": true,
+  "errors": [],
+  "messages": [],
+  "result": {
+    "name": "example-subdomain"
+  }
+}`)
+	})
+
+	_, err := client.WorkersGetSubdomain(context.Background(), AccountIdentifier(""))
+	if assert.Error(t, err) {
+		assert.Equal(t, ErrMissingAccountID, err)
+	}
+
+	res, err := client.WorkersGetSubdomain(context.Background(), AccountIdentifier(testAccountID))
+
+	want := WorkersSubdomain{Name: "example-subdomain"}
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, want, res)
+	}
+}