/
workers_subdomain.go
58 lines (50 loc) · 1.69 KB
/
workers_subdomain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package cloudflare
import (
"context"
"fmt"
"net/http"
"github.com/goccy/go-json"
)
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://developers.cloudflare.com/api/operations/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://developers.cloudflare.com/api/operations/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
}