/
v1.go
510 lines (443 loc) · 15.5 KB
/
v1.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package images
import (
"context"
"fmt"
"net/http"
"net/url"
"reflect"
"time"
"github.com/cloudflare/cloudflare-go/v2/internal/apijson"
"github.com/cloudflare/cloudflare-go/v2/internal/apiquery"
"github.com/cloudflare/cloudflare-go/v2/internal/pagination"
"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/tidwall/gjson"
)
// V1Service 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 [NewV1Service] method instead.
type V1Service struct {
Options []option.RequestOption
Keys *V1KeyService
Stats *V1StatService
Variants *V1VariantService
Blobs *V1BlobService
}
// NewV1Service 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 NewV1Service(opts ...option.RequestOption) (r *V1Service) {
r = &V1Service{}
r.Options = opts
r.Keys = NewV1KeyService(opts...)
r.Stats = NewV1StatService(opts...)
r.Variants = NewV1VariantService(opts...)
r.Blobs = NewV1BlobService(opts...)
return
}
// Upload an image with up to 10 Megabytes using a single HTTP POST
// (multipart/form-data) request. An image can be uploaded by sending an image file
// or passing an accessible to an API url.
func (r *V1Service) New(ctx context.Context, params V1NewParams, opts ...option.RequestOption) (res *Image, err error) {
opts = append(r.Options[:], opts...)
var env V1NewResponseEnvelope
path := fmt.Sprintf("accounts/%s/images/v1", params.AccountID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
// List up to 100 images with one request. Use the optional parameters below to get
// a specific range of images.
func (r *V1Service) List(ctx context.Context, params V1ListParams, opts ...option.RequestOption) (res *pagination.V4PagePagination[V1ListResponse], err error) {
var raw *http.Response
opts = append(r.Options, opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := fmt.Sprintf("accounts/%s/images/v1", params.AccountID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// List up to 100 images with one request. Use the optional parameters below to get
// a specific range of images.
func (r *V1Service) ListAutoPaging(ctx context.Context, params V1ListParams, opts ...option.RequestOption) *pagination.V4PagePaginationAutoPager[V1ListResponse] {
return pagination.NewV4PagePaginationAutoPager(r.List(ctx, params, opts...))
}
// Delete an image on Cloudflare Images. On success, all copies of the image are
// deleted and purged from cache.
func (r *V1Service) Delete(ctx context.Context, imageID string, params V1DeleteParams, opts ...option.RequestOption) (res *V1DeleteResponseUnion, err error) {
opts = append(r.Options[:], opts...)
var env V1DeleteResponseEnvelope
path := fmt.Sprintf("accounts/%s/images/v1/%s", params.AccountID, imageID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
// Update image access control. On access control change, all copies of the image
// are purged from cache.
func (r *V1Service) Edit(ctx context.Context, imageID string, params V1EditParams, opts ...option.RequestOption) (res *Image, err error) {
opts = append(r.Options[:], opts...)
var env V1EditResponseEnvelope
path := fmt.Sprintf("accounts/%s/images/v1/%s", params.AccountID, imageID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, params, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
// Fetch details for a single image.
func (r *V1Service) Get(ctx context.Context, imageID string, query V1GetParams, opts ...option.RequestOption) (res *Image, err error) {
opts = append(r.Options[:], opts...)
var env V1GetResponseEnvelope
path := fmt.Sprintf("accounts/%s/images/v1/%s", query.AccountID, imageID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}
type Image struct {
// Image unique identifier.
ID string `json:"id"`
// Image file name.
Filename string `json:"filename"`
// User modifiable key-value store. Can be used for keeping references to another
// system of record for managing images. Metadata must not exceed 1024 bytes.
Meta interface{} `json:"meta"`
// Indicates whether the image can be a accessed only using it's UID. If set to
// true, a signed token needs to be generated with a signing key to view the image.
RequireSignedURLs bool `json:"requireSignedURLs"`
// When the media item was uploaded.
Uploaded time.Time `json:"uploaded" format:"date-time"`
// Object specifying available variants for an image.
Variants []ImageVariantsUnion `json:"variants" format:"uri"`
JSON imageJSON `json:"-"`
}
// imageJSON contains the JSON metadata for the struct [Image]
type imageJSON struct {
ID apijson.Field
Filename apijson.Field
Meta apijson.Field
RequireSignedURLs apijson.Field
Uploaded apijson.Field
Variants apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Image) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r imageJSON) RawJSON() string {
return r.raw
}
// URI to thumbnail variant for an image.
//
// Union satisfied by [shared.UnionString], [shared.UnionString] or
// [shared.UnionString].
type ImageVariantsUnion interface {
ImplementsImagesImageVariantsUnion()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*ImageVariantsUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
)
}
type V1ListResponse struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result V1ListResponseResult `json:"result,required"`
// Whether the API call was successful
Success V1ListResponseSuccess `json:"success,required"`
JSON v1ListResponseJSON `json:"-"`
}
// v1ListResponseJSON contains the JSON metadata for the struct [V1ListResponse]
type v1ListResponseJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1ListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1ListResponseJSON) RawJSON() string {
return r.raw
}
type V1ListResponseResult struct {
Images []Image `json:"images"`
JSON v1ListResponseResultJSON `json:"-"`
}
// v1ListResponseResultJSON contains the JSON metadata for the struct
// [V1ListResponseResult]
type v1ListResponseResultJSON struct {
Images apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1ListResponseResult) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1ListResponseResultJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type V1ListResponseSuccess bool
const (
V1ListResponseSuccessTrue V1ListResponseSuccess = true
)
func (r V1ListResponseSuccess) IsKnown() bool {
switch r {
case V1ListResponseSuccessTrue:
return true
}
return false
}
// Union satisfied by [images.V1DeleteResponseUnknown] or [shared.UnionString].
type V1DeleteResponseUnion interface {
ImplementsImagesV1DeleteResponseUnion()
}
func init() {
apijson.RegisterUnion(
reflect.TypeOf((*V1DeleteResponseUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
)
}
type V1NewParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
// An image binary data. Only needed when type is uploading a file.
File param.Field[interface{}] `json:"file"`
// User modifiable key-value store. Can use used for keeping references to another
// system of record for managing images.
Metadata param.Field[interface{}] `json:"metadata"`
// Indicates whether the image requires a signature token for the access.
RequireSignedURLs param.Field[bool] `json:"requireSignedURLs"`
// A URL to fetch an image from origin. Only needed when type is uploading from a
// URL.
URL param.Field[string] `json:"url"`
}
func (r V1NewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type V1NewResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result Image `json:"result,required"`
// Whether the API call was successful
Success V1NewResponseEnvelopeSuccess `json:"success,required"`
JSON v1NewResponseEnvelopeJSON `json:"-"`
}
// v1NewResponseEnvelopeJSON contains the JSON metadata for the struct
// [V1NewResponseEnvelope]
type v1NewResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1NewResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1NewResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type V1NewResponseEnvelopeSuccess bool
const (
V1NewResponseEnvelopeSuccessTrue V1NewResponseEnvelopeSuccess = true
)
func (r V1NewResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case V1NewResponseEnvelopeSuccessTrue:
return true
}
return false
}
type V1ListParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
// Page number of paginated results.
Page param.Field[float64] `query:"page"`
// Number of items per page.
PerPage param.Field[float64] `query:"per_page"`
}
// URLQuery serializes [V1ListParams]'s query parameters as `url.Values`.
func (r V1ListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatRepeat,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type V1DeleteParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
Body interface{} `json:"body,required"`
}
func (r V1DeleteParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r.Body)
}
type V1DeleteResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result V1DeleteResponseUnion `json:"result,required"`
// Whether the API call was successful
Success V1DeleteResponseEnvelopeSuccess `json:"success,required"`
JSON v1DeleteResponseEnvelopeJSON `json:"-"`
}
// v1DeleteResponseEnvelopeJSON contains the JSON metadata for the struct
// [V1DeleteResponseEnvelope]
type v1DeleteResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1DeleteResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1DeleteResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type V1DeleteResponseEnvelopeSuccess bool
const (
V1DeleteResponseEnvelopeSuccessTrue V1DeleteResponseEnvelopeSuccess = true
)
func (r V1DeleteResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case V1DeleteResponseEnvelopeSuccessTrue:
return true
}
return false
}
type V1EditParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
// User modifiable key-value store. Can be used for keeping references to another
// system of record for managing images. No change if not specified.
Metadata param.Field[interface{}] `json:"metadata"`
// Indicates whether the image can be accessed using only its UID. If set to
// `true`, a signed token needs to be generated with a signing key to view the
// image. Returns a new UID on a change. No change if not specified.
RequireSignedURLs param.Field[bool] `json:"requireSignedURLs"`
}
func (r V1EditParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type V1EditResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result Image `json:"result,required"`
// Whether the API call was successful
Success V1EditResponseEnvelopeSuccess `json:"success,required"`
JSON v1EditResponseEnvelopeJSON `json:"-"`
}
// v1EditResponseEnvelopeJSON contains the JSON metadata for the struct
// [V1EditResponseEnvelope]
type v1EditResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1EditResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1EditResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type V1EditResponseEnvelopeSuccess bool
const (
V1EditResponseEnvelopeSuccessTrue V1EditResponseEnvelopeSuccess = true
)
func (r V1EditResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case V1EditResponseEnvelopeSuccessTrue:
return true
}
return false
}
type V1GetParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
}
type V1GetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result Image `json:"result,required"`
// Whether the API call was successful
Success V1GetResponseEnvelopeSuccess `json:"success,required"`
JSON v1GetResponseEnvelopeJSON `json:"-"`
}
// v1GetResponseEnvelopeJSON contains the JSON metadata for the struct
// [V1GetResponseEnvelope]
type v1GetResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Result apijson.Field
Success apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *V1GetResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r v1GetResponseEnvelopeJSON) RawJSON() string {
return r.raw
}
// Whether the API call was successful
type V1GetResponseEnvelopeSuccess bool
const (
V1GetResponseEnvelopeSuccessTrue V1GetResponseEnvelopeSuccess = true
)
func (r V1GetResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case V1GetResponseEnvelopeSuccessTrue:
return true
}
return false
}