|
| 1 | +package cloudflare |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/goccy/go-json" |
| 10 | +) |
| 11 | + |
| 12 | +type WorkersForPlatformsDispatchNamespace struct { |
| 13 | + NamespaceId string `json:"namespace_id"` |
| 14 | + NamespaceName string `json:"namespace_name"` |
| 15 | + CreatedOn *time.Time `json:"created_on,omitempty"` |
| 16 | + CreatedBy string `json:"created_by"` |
| 17 | + ModifiedOn *time.Time `json:"modified_on,omitempty"` |
| 18 | + ModifiedBy string `json:"modified_by"` |
| 19 | +} |
| 20 | + |
| 21 | +type ListWorkersForPlatformsDispatchNamespaceResponse struct { |
| 22 | + Response |
| 23 | + Result []WorkersForPlatformsDispatchNamespace `json:"result"` |
| 24 | +} |
| 25 | + |
| 26 | +type GetWorkersForPlatformsDispatchNamespaceResponse struct { |
| 27 | + Response |
| 28 | + Result WorkersForPlatformsDispatchNamespace `json:"result"` |
| 29 | +} |
| 30 | + |
| 31 | +type CreateWorkersForPlatformsDispatchNamespaceParams struct { |
| 32 | + Name string `json:"name"` |
| 33 | +} |
| 34 | + |
| 35 | +// ListWorkersForPlatformsDispatchNamespaces lists the dispatch namespaces. |
| 36 | +// |
| 37 | +// API reference: https://developers.cloudflare.com/api/operations/namespace-worker-list |
| 38 | +func (api *API) ListWorkersForPlatformsDispatchNamespaces(ctx context.Context, rc *ResourceContainer) (*ListWorkersForPlatformsDispatchNamespaceResponse, error) { |
| 39 | + if rc.Level != AccountRouteLevel { |
| 40 | + return nil, ErrRequiredAccountLevelResourceContainer |
| 41 | + } |
| 42 | + |
| 43 | + if rc.Identifier == "" { |
| 44 | + return nil, ErrMissingAccountID |
| 45 | + } |
| 46 | + |
| 47 | + uri := fmt.Sprintf("/accounts/%s/workers/dispatch/namespaces", rc.Identifier) |
| 48 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 49 | + |
| 50 | + var r ListWorkersForPlatformsDispatchNamespaceResponse |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + err = json.Unmarshal(res, &r) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("%s: %w", errUnmarshalError, err) |
| 58 | + } |
| 59 | + |
| 60 | + return &r, nil |
| 61 | +} |
| 62 | + |
| 63 | +// GetWorkersForPlatformsDispatchNamespace gets a specific dispatch namespace. |
| 64 | +// |
| 65 | +// API reference: https://developers.cloudflare.com/api/operations/namespace-worker-get-namespace |
| 66 | +func (api *API) GetWorkersForPlatformsDispatchNamespace(ctx context.Context, rc *ResourceContainer, name string) (*GetWorkersForPlatformsDispatchNamespaceResponse, error) { |
| 67 | + if rc.Level != AccountRouteLevel { |
| 68 | + return nil, ErrRequiredAccountLevelResourceContainer |
| 69 | + } |
| 70 | + |
| 71 | + if rc.Identifier == "" { |
| 72 | + return nil, ErrMissingAccountID |
| 73 | + } |
| 74 | + |
| 75 | + uri := fmt.Sprintf("/accounts/%s/workers/dispatch/namespaces/%s", rc.Identifier, name) |
| 76 | + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) |
| 77 | + |
| 78 | + var r GetWorkersForPlatformsDispatchNamespaceResponse |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + err = json.Unmarshal(res, &r) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("%s: %w", errUnmarshalError, err) |
| 86 | + } |
| 87 | + |
| 88 | + return &r, nil |
| 89 | +} |
| 90 | + |
| 91 | +// CreateWorkersForPlatformsDispatchNamespace creates a new dispatch namespace. |
| 92 | +// |
| 93 | +// API reference: https://developers.cloudflare.com/api/operations/namespace-worker-create |
| 94 | +func (api *API) CreateWorkersForPlatformsDispatchNamespace(ctx context.Context, rc *ResourceContainer, params CreateWorkersForPlatformsDispatchNamespaceParams) (*GetWorkersForPlatformsDispatchNamespaceResponse, error) { |
| 95 | + if rc.Level != AccountRouteLevel { |
| 96 | + return nil, ErrRequiredAccountLevelResourceContainer |
| 97 | + } |
| 98 | + |
| 99 | + if rc.Identifier == "" { |
| 100 | + return nil, ErrMissingAccountID |
| 101 | + } |
| 102 | + |
| 103 | + uri := fmt.Sprintf("/accounts/%s/workers/dispatch/namespaces", rc.Identifier) |
| 104 | + res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) |
| 105 | + |
| 106 | + var r GetWorkersForPlatformsDispatchNamespaceResponse |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + err = json.Unmarshal(res, &r) |
| 112 | + if err != nil { |
| 113 | + return nil, fmt.Errorf("%s: %w", errUnmarshalError, err) |
| 114 | + } |
| 115 | + |
| 116 | + return &r, nil |
| 117 | +} |
| 118 | + |
| 119 | +// DeleteWorkersForPlatformsDispatchNamespace deletes a dispatch namespace. |
| 120 | +// |
| 121 | +// API reference: https://developers.cloudflare.com/api/operations/namespace-worker-delete-namespace |
| 122 | +func (api *API) DeleteWorkersForPlatformsDispatchNamespace(ctx context.Context, rc *ResourceContainer, name string) error { |
| 123 | + if rc.Level != AccountRouteLevel { |
| 124 | + return ErrRequiredAccountLevelResourceContainer |
| 125 | + } |
| 126 | + |
| 127 | + if rc.Identifier == "" { |
| 128 | + return ErrMissingAccountID |
| 129 | + } |
| 130 | + |
| 131 | + uri := fmt.Sprintf("/accounts/%s/workers/dispatch/namespaces/%s", rc.Identifier, name) |
| 132 | + _, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
0 commit comments