/
dispatchnamespacescript.go
381 lines (333 loc) · 15.9 KB
/
dispatchnamespacescript.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package workers_for_platforms
import (
"bytes"
"context"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"time"
"github.com/cloudflare/cloudflare-go/v2/internal/apiform"
"github.com/cloudflare/cloudflare-go/v2/internal/apijson"
"github.com/cloudflare/cloudflare-go/v2/internal/apiquery"
"github.com/cloudflare/cloudflare-go/v2/internal/param"
"github.com/cloudflare/cloudflare-go/v2/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v2/internal/shared"
"github.com/cloudflare/cloudflare-go/v2/option"
"github.com/cloudflare/cloudflare-go/v2/workers"
)
// DispatchNamespaceScriptService contains methods and other services that help
// with interacting with the cloudflare API. Note, unlike clients, this service
// does not read variables from the environment automatically. You should not
// instantiate this service directly, and instead use the
// [NewDispatchNamespaceScriptService] method instead.
type DispatchNamespaceScriptService struct {
Options []option.RequestOption
Content *DispatchNamespaceScriptContentService
Settings *DispatchNamespaceScriptSettingService
Bindings *DispatchNamespaceScriptBindingService
Secrets *DispatchNamespaceScriptSecretService
Tags *DispatchNamespaceScriptTagService
}
// NewDispatchNamespaceScriptService generates a new service that applies the given
// options to each request. These options are applied after the parent client's
// options (if there is one), and before any request-specific options.
func NewDispatchNamespaceScriptService(opts ...option.RequestOption) (r *DispatchNamespaceScriptService) {
r = &DispatchNamespaceScriptService{}
r.Options = opts
r.Content = NewDispatchNamespaceScriptContentService(opts...)
r.Settings = NewDispatchNamespaceScriptSettingService(opts...)
r.Bindings = NewDispatchNamespaceScriptBindingService(opts...)
r.Secrets = NewDispatchNamespaceScriptSecretService(opts...)
r.Tags = NewDispatchNamespaceScriptTagService(opts...)
return
}
// Upload a worker module to a Workers for Platforms namespace. You can find an
// example of the metadata on our docs:
// https://developers.cloudflare.com/cloudflare-for-platforms/workers-for-platforms/reference/metadata/
func (r *DispatchNamespaceScriptService) Update(ctx context.Context, dispatchNamespace string, scriptName string, params DispatchNamespaceScriptUpdateParams, opts ...option.RequestOption) (res *workers.Script, err error) {
opts = append(r.Options[:], opts...)
var env DispatchNamespaceScriptUpdateResponseEnvelope
path := fmt.Sprintf("accounts/%s/workers/dispatch/namespaces/%s/scripts/%s", params.AccountID, dispatchNamespace, scriptName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, params, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
// Delete a worker from a Workers for Platforms namespace. This call has no
// response body on a successful delete.
func (r *DispatchNamespaceScriptService) Delete(ctx context.Context, dispatchNamespace string, scriptName string, params DispatchNamespaceScriptDeleteParams, opts ...option.RequestOption) (err error) {
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithHeader("Accept", "")}, opts...)
path := fmt.Sprintf("accounts/%s/workers/dispatch/namespaces/%s/scripts/%s", params.AccountID, dispatchNamespace, scriptName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, params, nil, opts...)
return
}
// Fetch information about a script uploaded to a Workers for Platforms namespace.
func (r *DispatchNamespaceScriptService) Get(ctx context.Context, dispatchNamespace string, scriptName string, query DispatchNamespaceScriptGetParams, opts ...option.RequestOption) (res *Script, err error) {
opts = append(r.Options[:], opts...)
var env DispatchNamespaceScriptGetResponseEnvelope
path := fmt.Sprintf("accounts/%s/workers/dispatch/namespaces/%s/scripts/%s", query.AccountID, dispatchNamespace, scriptName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
// Details about a worker uploaded to a Workers for Platforms namespace.
type Script struct {
// When the script was created.
CreatedOn time.Time `json:"created_on" format:"date-time"`
// Name of the Workers for Platforms dispatch namespace.
DispatchNamespace string `json:"dispatch_namespace"`
// When the script was last modified.
ModifiedOn time.Time `json:"modified_on" format:"date-time"`
Script workers.Script `json:"script"`
JSON scriptJSON `json:"-"`
}
// scriptJSON contains the JSON metadata for the struct [Script]
type scriptJSON struct {
CreatedOn apijson.Field
DispatchNamespace apijson.Field
ModifiedOn apijson.Field
Script apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Script) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r scriptJSON) RawJSON() string {
return r.raw
}
type DispatchNamespaceScriptUpdateParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
Body DispatchNamespaceScriptUpdateParamsBodyUnion `json:"body,required"`
}
func (r DispatchNamespaceScriptUpdateParams) MarshalMultipart() (data []byte, contentType string, err error) {
buf := bytes.NewBuffer(nil)
writer := multipart.NewWriter(buf)
err = apiform.MarshalRoot(r, writer)
if err != nil {
writer.Close()
return nil, "", err
}
err = writer.Close()
if err != nil {
return nil, "", err
}
return buf.Bytes(), writer.FormDataContentType(), nil
}
type DispatchNamespaceScriptUpdateParamsBody struct {
AnyPartName param.Field[interface{}] `json:"<any part name>,required"`
Metadata param.Field[interface{}] `json:"metadata,required"`
// Rollback message to be associated with this deployment. Only parsed when query
// param `"rollback_to"` is present.
Message param.Field[string] `json:"message"`
}
func (r DispatchNamespaceScriptUpdateParamsBody) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r DispatchNamespaceScriptUpdateParamsBody) implementsWorkersForPlatformsDispatchNamespaceScriptUpdateParamsBodyUnion() {
}
// Satisfied by
// [workers_for_platforms.DispatchNamespaceScriptUpdateParamsBodyObject],
// [workers_for_platforms.DispatchNamespaceScriptUpdateParamsBodyObject],
// [DispatchNamespaceScriptUpdateParamsBody].
type DispatchNamespaceScriptUpdateParamsBodyUnion interface {
implementsWorkersForPlatformsDispatchNamespaceScriptUpdateParamsBodyUnion()
}
type DispatchNamespaceScriptUpdateParamsBodyObject struct {
// A module comprising a Worker script, often a javascript file. Multiple modules
// may be provided as separate named parts, but at least one module must be present
// and referenced in the metadata as `main_module` or `body_part` by part name.
// Source maps may also be included using the `application/source-map` content
// type.
AnyPartName param.Field[[]io.Reader] `json:"<any part name>" format:"binary"`
// JSON encoded metadata about the uploaded parts and Worker configuration.
Metadata param.Field[DispatchNamespaceScriptUpdateParamsBodyObjectMetadata] `json:"metadata"`
}
func (r DispatchNamespaceScriptUpdateParamsBodyObject) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r DispatchNamespaceScriptUpdateParamsBodyObject) implementsWorkersForPlatformsDispatchNamespaceScriptUpdateParamsBodyUnion() {
}
// JSON encoded metadata about the uploaded parts and Worker configuration.
type DispatchNamespaceScriptUpdateParamsBodyObjectMetadata struct {
// List of bindings available to the worker.
Bindings param.Field[[]interface{}] `json:"bindings"`
// Name of the part in the multipart request that contains the script (e.g. the
// file adding a listener to the `fetch` event). Indicates a
// `service worker syntax` Worker.
BodyPart param.Field[string] `json:"body_part"`
// Date indicating targeted support in the Workers runtime. Backwards incompatible
// fixes to the runtime following this date will not affect this Worker.
CompatibilityDate param.Field[string] `json:"compatibility_date"`
// Flags that enable or disable certain features in the Workers runtime. Used to
// enable upcoming features or opt in or out of specific changes not included in a
// `compatibility_date`.
CompatibilityFlags param.Field[[]string] `json:"compatibility_flags"`
// List of binding types to keep from previous_upload.
KeepBindings param.Field[[]string] `json:"keep_bindings"`
// Whether Logpush is turned on for the Worker.
Logpush param.Field[bool] `json:"logpush"`
// Name of the part in the multipart request that contains the main module (e.g.
// the file exporting a `fetch` handler). Indicates a `module syntax` Worker.
MainModule param.Field[string] `json:"main_module"`
// Migrations to apply for Durable Objects associated with this Worker.
Migrations param.Field[DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrationsUnion] `json:"migrations"`
Placement param.Field[workers.PlacementConfigurationParam] `json:"placement"`
// List of strings to use as tags for this Worker
Tags param.Field[[]string] `json:"tags"`
// List of Workers that will consume logs from the attached Worker.
TailConsumers param.Field[[]workers.ConsumerScriptParam] `json:"tail_consumers"`
// Usage model to apply to invocations.
UsageModel param.Field[DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModel] `json:"usage_model"`
// Key-value pairs to use as tags for this version of this Worker
VersionTags param.Field[interface{}] `json:"version_tags"`
}
func (r DispatchNamespaceScriptUpdateParamsBodyObjectMetadata) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Migrations to apply for Durable Objects associated with this Worker.
type DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrations struct {
// Tag to set as the latest migration tag.
NewTag param.Field[string] `json:"new_tag"`
// Tag used to verify against the latest migration tag for this Worker. If they
// don't match, the upload is rejected.
OldTag param.Field[string] `json:"old_tag"`
DeletedClasses param.Field[interface{}] `json:"deleted_classes,required"`
NewClasses param.Field[interface{}] `json:"new_classes,required"`
RenamedClasses param.Field[interface{}] `json:"renamed_classes,required"`
TransferredClasses param.Field[interface{}] `json:"transferred_classes,required"`
Steps param.Field[interface{}] `json:"steps,required"`
}
func (r DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrations) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
func (r DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrations) implementsWorkersForPlatformsDispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrationsUnion() {
}
// Migrations to apply for Durable Objects associated with this Worker.
//
// Satisfied by [workers.SingleStepMigrationParam],
// [workers.SteppedMigrationParam],
// [DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrations].
type DispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrationsUnion interface {
implementsWorkersForPlatformsDispatchNamespaceScriptUpdateParamsBodyObjectMetadataMigrationsUnion()
}
// Usage model to apply to invocations.
type DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModel string
const (
DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModelBundled DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModel = "bundled"
DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModelUnbound DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModel = "unbound"
)
func (r DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModel) IsKnown() bool {
switch r {
case DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModelBundled, DispatchNamespaceScriptUpdateParamsBodyObjectMetadataUsageModelUnbound:
return true
}
return false
}
type DispatchNamespaceScriptUpdateResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success DispatchNamespaceScriptUpdateResponseEnvelopeSuccess `json:"success,required"`
Result workers.Script `json:"result"`
JSON dispatchNamespaceScriptUpdateResponseEnvelopeJSON `json:"-"`
}
// dispatchNamespaceScriptUpdateResponseEnvelopeJSON contains the JSON metadata for
// the struct [DispatchNamespaceScriptUpdateResponseEnvelope]
type dispatchNamespaceScriptUpdateResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Success apijson.Field
Result apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *DispatchNamespaceScriptUpdateResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r dispatchNamespaceScriptUpdateResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type DispatchNamespaceScriptUpdateResponseEnvelopeSuccess bool
const (
DispatchNamespaceScriptUpdateResponseEnvelopeSuccessTrue DispatchNamespaceScriptUpdateResponseEnvelopeSuccess = true
)
func (r DispatchNamespaceScriptUpdateResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case DispatchNamespaceScriptUpdateResponseEnvelopeSuccessTrue:
return true
}
return false
}
type DispatchNamespaceScriptDeleteParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
Body interface{} `json:"body,required"`
// If set to true, delete will not be stopped by associated service binding,
// durable object, or other binding. Any of these associated bindings/durable
// objects will be deleted along with the script.
Force param.Field[bool] `query:"force"`
}
func (r DispatchNamespaceScriptDeleteParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r.Body)
}
// URLQuery serializes [DispatchNamespaceScriptDeleteParams]'s query parameters as
// `url.Values`.
func (r DispatchNamespaceScriptDeleteParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatRepeat,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type DispatchNamespaceScriptGetParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
}
type DispatchNamespaceScriptGetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success DispatchNamespaceScriptGetResponseEnvelopeSuccess `json:"success,required"`
// Details about a worker uploaded to a Workers for Platforms namespace.
Result Script `json:"result"`
JSON dispatchNamespaceScriptGetResponseEnvelopeJSON `json:"-"`
}
// dispatchNamespaceScriptGetResponseEnvelopeJSON contains the JSON metadata for
// the struct [DispatchNamespaceScriptGetResponseEnvelope]
type dispatchNamespaceScriptGetResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Success apijson.Field
Result apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *DispatchNamespaceScriptGetResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r dispatchNamespaceScriptGetResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type DispatchNamespaceScriptGetResponseEnvelopeSuccess bool
const (
DispatchNamespaceScriptGetResponseEnvelopeSuccessTrue DispatchNamespaceScriptGetResponseEnvelopeSuccess = true
)
func (r DispatchNamespaceScriptGetResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case DispatchNamespaceScriptGetResponseEnvelopeSuccessTrue:
return true
}
return false
}