|
| 1 | +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +package workers |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/cloudflare/cloudflare-go/v4/internal/apijson" |
| 12 | + "github.com/cloudflare/cloudflare-go/v4/internal/param" |
| 13 | + "github.com/cloudflare/cloudflare-go/v4/internal/requestconfig" |
| 14 | + "github.com/cloudflare/cloudflare-go/v4/option" |
| 15 | + "github.com/cloudflare/cloudflare-go/v4/packages/pagination" |
| 16 | + "github.com/cloudflare/cloudflare-go/v4/shared" |
| 17 | + "github.com/cloudflare/cloudflare-go/v4/workers_for_platforms" |
| 18 | +) |
| 19 | + |
| 20 | +// ScriptSecretService contains methods and other services that help with |
| 21 | +// interacting with the cloudflare API. |
| 22 | +// |
| 23 | +// Note, unlike clients, this service does not read variables from the environment |
| 24 | +// automatically. You should not instantiate this service directly, and instead use |
| 25 | +// the [NewScriptSecretService] method instead. |
| 26 | +type ScriptSecretService struct { |
| 27 | + Options []option.RequestOption |
| 28 | +} |
| 29 | + |
| 30 | +// NewScriptSecretService generates a new service that applies the given options to |
| 31 | +// each request. These options are applied after the parent client's options (if |
| 32 | +// there is one), and before any request-specific options. |
| 33 | +func NewScriptSecretService(opts ...option.RequestOption) (r *ScriptSecretService) { |
| 34 | + r = &ScriptSecretService{} |
| 35 | + r.Options = opts |
| 36 | + return |
| 37 | +} |
| 38 | + |
| 39 | +// Add a secret to a script. |
| 40 | +func (r *ScriptSecretService) Update(ctx context.Context, scriptName string, params ScriptSecretUpdateParams, opts ...option.RequestOption) (res *ScriptSecretUpdateResponse, err error) { |
| 41 | + var env ScriptSecretUpdateResponseEnvelope |
| 42 | + opts = append(r.Options[:], opts...) |
| 43 | + if params.AccountID.Value == "" { |
| 44 | + err = errors.New("missing required account_id parameter") |
| 45 | + return |
| 46 | + } |
| 47 | + if scriptName == "" { |
| 48 | + err = errors.New("missing required script_name parameter") |
| 49 | + return |
| 50 | + } |
| 51 | + path := fmt.Sprintf("accounts/%s/workers/scripts/%s/secrets", params.AccountID, scriptName) |
| 52 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, params, &env, opts...) |
| 53 | + if err != nil { |
| 54 | + return |
| 55 | + } |
| 56 | + res = &env.Result |
| 57 | + return |
| 58 | +} |
| 59 | + |
| 60 | +// List secrets bound to a script. |
| 61 | +func (r *ScriptSecretService) List(ctx context.Context, scriptName string, query ScriptSecretListParams, opts ...option.RequestOption) (res *pagination.SinglePage[ScriptSecretListResponse], err error) { |
| 62 | + var raw *http.Response |
| 63 | + opts = append(r.Options[:], opts...) |
| 64 | + opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...) |
| 65 | + if query.AccountID.Value == "" { |
| 66 | + err = errors.New("missing required account_id parameter") |
| 67 | + return |
| 68 | + } |
| 69 | + if scriptName == "" { |
| 70 | + err = errors.New("missing required script_name parameter") |
| 71 | + return |
| 72 | + } |
| 73 | + path := fmt.Sprintf("accounts/%s/workers/scripts/%s/secrets", query.AccountID, scriptName) |
| 74 | + cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, nil, &res, opts...) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + err = cfg.Execute() |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + res.SetPageConfig(cfg, raw) |
| 83 | + return res, nil |
| 84 | +} |
| 85 | + |
| 86 | +// List secrets bound to a script. |
| 87 | +func (r *ScriptSecretService) ListAutoPaging(ctx context.Context, scriptName string, query ScriptSecretListParams, opts ...option.RequestOption) *pagination.SinglePageAutoPager[ScriptSecretListResponse] { |
| 88 | + return pagination.NewSinglePageAutoPager(r.List(ctx, scriptName, query, opts...)) |
| 89 | +} |
| 90 | + |
| 91 | +// Remove a secret from a script. |
| 92 | +func (r *ScriptSecretService) Delete(ctx context.Context, scriptName string, secretName string, body ScriptSecretDeleteParams, opts ...option.RequestOption) (res *ScriptSecretDeleteResponse, err error) { |
| 93 | + var env ScriptSecretDeleteResponseEnvelope |
| 94 | + opts = append(r.Options[:], opts...) |
| 95 | + if body.AccountID.Value == "" { |
| 96 | + err = errors.New("missing required account_id parameter") |
| 97 | + return |
| 98 | + } |
| 99 | + if scriptName == "" { |
| 100 | + err = errors.New("missing required script_name parameter") |
| 101 | + return |
| 102 | + } |
| 103 | + if secretName == "" { |
| 104 | + err = errors.New("missing required secret_name parameter") |
| 105 | + return |
| 106 | + } |
| 107 | + path := fmt.Sprintf("accounts/%s/workers/scripts/%s/secrets/%s", body.AccountID, scriptName, secretName) |
| 108 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, nil, &env, opts...) |
| 109 | + if err != nil { |
| 110 | + return |
| 111 | + } |
| 112 | + res = &env.Result |
| 113 | + return |
| 114 | +} |
| 115 | + |
| 116 | +// Get a given secret binding (value omitted) on a script. |
| 117 | +func (r *ScriptSecretService) Get(ctx context.Context, scriptName string, secretName string, query ScriptSecretGetParams, opts ...option.RequestOption) (res *ScriptSecretGetResponse, err error) { |
| 118 | + var env ScriptSecretGetResponseEnvelope |
| 119 | + opts = append(r.Options[:], opts...) |
| 120 | + if query.AccountID.Value == "" { |
| 121 | + err = errors.New("missing required account_id parameter") |
| 122 | + return |
| 123 | + } |
| 124 | + if scriptName == "" { |
| 125 | + err = errors.New("missing required script_name parameter") |
| 126 | + return |
| 127 | + } |
| 128 | + if secretName == "" { |
| 129 | + err = errors.New("missing required secret_name parameter") |
| 130 | + return |
| 131 | + } |
| 132 | + path := fmt.Sprintf("accounts/%s/workers/scripts/%s/secrets/%s", query.AccountID, scriptName, secretName) |
| 133 | + err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...) |
| 134 | + if err != nil { |
| 135 | + return |
| 136 | + } |
| 137 | + res = &env.Result |
| 138 | + return |
| 139 | +} |
| 140 | + |
| 141 | +type ScriptSecretUpdateResponse struct { |
| 142 | + // The name of this secret, this is what will be used to access it inside the |
| 143 | + // Worker. |
| 144 | + Name string `json:"name"` |
| 145 | + // The type of secret. |
| 146 | + Type ScriptSecretUpdateResponseType `json:"type"` |
| 147 | + JSON scriptSecretUpdateResponseJSON `json:"-"` |
| 148 | +} |
| 149 | + |
| 150 | +// scriptSecretUpdateResponseJSON contains the JSON metadata for the struct |
| 151 | +// [ScriptSecretUpdateResponse] |
| 152 | +type scriptSecretUpdateResponseJSON struct { |
| 153 | + Name apijson.Field |
| 154 | + Type apijson.Field |
| 155 | + raw string |
| 156 | + ExtraFields map[string]apijson.Field |
| 157 | +} |
| 158 | + |
| 159 | +func (r *ScriptSecretUpdateResponse) UnmarshalJSON(data []byte) (err error) { |
| 160 | + return apijson.UnmarshalRoot(data, r) |
| 161 | +} |
| 162 | + |
| 163 | +func (r scriptSecretUpdateResponseJSON) RawJSON() string { |
| 164 | + return r.raw |
| 165 | +} |
| 166 | + |
| 167 | +// The type of secret. |
| 168 | +type ScriptSecretUpdateResponseType string |
| 169 | + |
| 170 | +const ( |
| 171 | + ScriptSecretUpdateResponseTypeSecretText ScriptSecretUpdateResponseType = "secret_text" |
| 172 | +) |
| 173 | + |
| 174 | +func (r ScriptSecretUpdateResponseType) IsKnown() bool { |
| 175 | + switch r { |
| 176 | + case ScriptSecretUpdateResponseTypeSecretText: |
| 177 | + return true |
| 178 | + } |
| 179 | + return false |
| 180 | +} |
| 181 | + |
| 182 | +type ScriptSecretListResponse struct { |
| 183 | + // The name of this secret, this is what will be used to access it inside the |
| 184 | + // Worker. |
| 185 | + Name string `json:"name"` |
| 186 | + // The type of secret. |
| 187 | + Type ScriptSecretListResponseType `json:"type"` |
| 188 | + JSON scriptSecretListResponseJSON `json:"-"` |
| 189 | +} |
| 190 | + |
| 191 | +// scriptSecretListResponseJSON contains the JSON metadata for the struct |
| 192 | +// [ScriptSecretListResponse] |
| 193 | +type scriptSecretListResponseJSON struct { |
| 194 | + Name apijson.Field |
| 195 | + Type apijson.Field |
| 196 | + raw string |
| 197 | + ExtraFields map[string]apijson.Field |
| 198 | +} |
| 199 | + |
| 200 | +func (r *ScriptSecretListResponse) UnmarshalJSON(data []byte) (err error) { |
| 201 | + return apijson.UnmarshalRoot(data, r) |
| 202 | +} |
| 203 | + |
| 204 | +func (r scriptSecretListResponseJSON) RawJSON() string { |
| 205 | + return r.raw |
| 206 | +} |
| 207 | + |
| 208 | +// The type of secret. |
| 209 | +type ScriptSecretListResponseType string |
| 210 | + |
| 211 | +const ( |
| 212 | + ScriptSecretListResponseTypeSecretText ScriptSecretListResponseType = "secret_text" |
| 213 | +) |
| 214 | + |
| 215 | +func (r ScriptSecretListResponseType) IsKnown() bool { |
| 216 | + switch r { |
| 217 | + case ScriptSecretListResponseTypeSecretText: |
| 218 | + return true |
| 219 | + } |
| 220 | + return false |
| 221 | +} |
| 222 | + |
| 223 | +type ScriptSecretDeleteResponse = interface{} |
| 224 | + |
| 225 | +type ScriptSecretGetResponse struct { |
| 226 | + // The name of this secret, this is what will be used to access it inside the |
| 227 | + // Worker. |
| 228 | + Name string `json:"name"` |
| 229 | + // The type of secret. |
| 230 | + Type ScriptSecretGetResponseType `json:"type"` |
| 231 | + JSON scriptSecretGetResponseJSON `json:"-"` |
| 232 | +} |
| 233 | + |
| 234 | +// scriptSecretGetResponseJSON contains the JSON metadata for the struct |
| 235 | +// [ScriptSecretGetResponse] |
| 236 | +type scriptSecretGetResponseJSON struct { |
| 237 | + Name apijson.Field |
| 238 | + Type apijson.Field |
| 239 | + raw string |
| 240 | + ExtraFields map[string]apijson.Field |
| 241 | +} |
| 242 | + |
| 243 | +func (r *ScriptSecretGetResponse) UnmarshalJSON(data []byte) (err error) { |
| 244 | + return apijson.UnmarshalRoot(data, r) |
| 245 | +} |
| 246 | + |
| 247 | +func (r scriptSecretGetResponseJSON) RawJSON() string { |
| 248 | + return r.raw |
| 249 | +} |
| 250 | + |
| 251 | +// The type of secret. |
| 252 | +type ScriptSecretGetResponseType string |
| 253 | + |
| 254 | +const ( |
| 255 | + ScriptSecretGetResponseTypeSecretText ScriptSecretGetResponseType = "secret_text" |
| 256 | +) |
| 257 | + |
| 258 | +func (r ScriptSecretGetResponseType) IsKnown() bool { |
| 259 | + switch r { |
| 260 | + case ScriptSecretGetResponseTypeSecretText: |
| 261 | + return true |
| 262 | + } |
| 263 | + return false |
| 264 | +} |
| 265 | + |
| 266 | +type ScriptSecretUpdateParams struct { |
| 267 | + // Identifier |
| 268 | + AccountID param.Field[string] `path:"account_id,required"` |
| 269 | + WorkersSecretModel workers_for_platforms.WorkersSecretModelParam `json:"workers_secret_model,required"` |
| 270 | +} |
| 271 | + |
| 272 | +func (r ScriptSecretUpdateParams) MarshalJSON() (data []byte, err error) { |
| 273 | + return apijson.MarshalRoot(r.WorkersSecretModel) |
| 274 | +} |
| 275 | + |
| 276 | +type ScriptSecretUpdateResponseEnvelope struct { |
| 277 | + Errors []shared.ResponseInfo `json:"errors,required"` |
| 278 | + Messages []shared.ResponseInfo `json:"messages,required"` |
| 279 | + // Whether the API call was successful |
| 280 | + Success ScriptSecretUpdateResponseEnvelopeSuccess `json:"success,required"` |
| 281 | + Result ScriptSecretUpdateResponse `json:"result"` |
| 282 | + JSON scriptSecretUpdateResponseEnvelopeJSON `json:"-"` |
| 283 | +} |
| 284 | + |
| 285 | +// scriptSecretUpdateResponseEnvelopeJSON contains the JSON metadata for the struct |
| 286 | +// [ScriptSecretUpdateResponseEnvelope] |
| 287 | +type scriptSecretUpdateResponseEnvelopeJSON struct { |
| 288 | + Errors apijson.Field |
| 289 | + Messages apijson.Field |
| 290 | + Success apijson.Field |
| 291 | + Result apijson.Field |
| 292 | + raw string |
| 293 | + ExtraFields map[string]apijson.Field |
| 294 | +} |
| 295 | + |
| 296 | +func (r *ScriptSecretUpdateResponseEnvelope) UnmarshalJSON(data []byte) (err error) { |
| 297 | + return apijson.UnmarshalRoot(data, r) |
| 298 | +} |
| 299 | + |
| 300 | +func (r scriptSecretUpdateResponseEnvelopeJSON) RawJSON() string { |
| 301 | + return r.raw |
| 302 | +} |
| 303 | + |
| 304 | +// Whether the API call was successful |
| 305 | +type ScriptSecretUpdateResponseEnvelopeSuccess bool |
| 306 | + |
| 307 | +const ( |
| 308 | + ScriptSecretUpdateResponseEnvelopeSuccessTrue ScriptSecretUpdateResponseEnvelopeSuccess = true |
| 309 | +) |
| 310 | + |
| 311 | +func (r ScriptSecretUpdateResponseEnvelopeSuccess) IsKnown() bool { |
| 312 | + switch r { |
| 313 | + case ScriptSecretUpdateResponseEnvelopeSuccessTrue: |
| 314 | + return true |
| 315 | + } |
| 316 | + return false |
| 317 | +} |
| 318 | + |
| 319 | +type ScriptSecretListParams struct { |
| 320 | + // Identifier |
| 321 | + AccountID param.Field[string] `path:"account_id,required"` |
| 322 | +} |
| 323 | + |
| 324 | +type ScriptSecretDeleteParams struct { |
| 325 | + // Identifier |
| 326 | + AccountID param.Field[string] `path:"account_id,required"` |
| 327 | +} |
| 328 | + |
| 329 | +type ScriptSecretDeleteResponseEnvelope struct { |
| 330 | + Errors []shared.ResponseInfo `json:"errors,required"` |
| 331 | + Messages []shared.ResponseInfo `json:"messages,required"` |
| 332 | + // Whether the API call was successful |
| 333 | + Success ScriptSecretDeleteResponseEnvelopeSuccess `json:"success,required"` |
| 334 | + Result ScriptSecretDeleteResponse `json:"result,nullable"` |
| 335 | + JSON scriptSecretDeleteResponseEnvelopeJSON `json:"-"` |
| 336 | +} |
| 337 | + |
| 338 | +// scriptSecretDeleteResponseEnvelopeJSON contains the JSON metadata for the struct |
| 339 | +// [ScriptSecretDeleteResponseEnvelope] |
| 340 | +type scriptSecretDeleteResponseEnvelopeJSON struct { |
| 341 | + Errors apijson.Field |
| 342 | + Messages apijson.Field |
| 343 | + Success apijson.Field |
| 344 | + Result apijson.Field |
| 345 | + raw string |
| 346 | + ExtraFields map[string]apijson.Field |
| 347 | +} |
| 348 | + |
| 349 | +func (r *ScriptSecretDeleteResponseEnvelope) UnmarshalJSON(data []byte) (err error) { |
| 350 | + return apijson.UnmarshalRoot(data, r) |
| 351 | +} |
| 352 | + |
| 353 | +func (r scriptSecretDeleteResponseEnvelopeJSON) RawJSON() string { |
| 354 | + return r.raw |
| 355 | +} |
| 356 | + |
| 357 | +// Whether the API call was successful |
| 358 | +type ScriptSecretDeleteResponseEnvelopeSuccess bool |
| 359 | + |
| 360 | +const ( |
| 361 | + ScriptSecretDeleteResponseEnvelopeSuccessTrue ScriptSecretDeleteResponseEnvelopeSuccess = true |
| 362 | +) |
| 363 | + |
| 364 | +func (r ScriptSecretDeleteResponseEnvelopeSuccess) IsKnown() bool { |
| 365 | + switch r { |
| 366 | + case ScriptSecretDeleteResponseEnvelopeSuccessTrue: |
| 367 | + return true |
| 368 | + } |
| 369 | + return false |
| 370 | +} |
| 371 | + |
| 372 | +type ScriptSecretGetParams struct { |
| 373 | + // Identifier |
| 374 | + AccountID param.Field[string] `path:"account_id,required"` |
| 375 | +} |
| 376 | + |
| 377 | +type ScriptSecretGetResponseEnvelope struct { |
| 378 | + Errors []shared.ResponseInfo `json:"errors,required"` |
| 379 | + Messages []shared.ResponseInfo `json:"messages,required"` |
| 380 | + // Whether the API call was successful |
| 381 | + Success ScriptSecretGetResponseEnvelopeSuccess `json:"success,required"` |
| 382 | + Result ScriptSecretGetResponse `json:"result"` |
| 383 | + JSON scriptSecretGetResponseEnvelopeJSON `json:"-"` |
| 384 | +} |
| 385 | + |
| 386 | +// scriptSecretGetResponseEnvelopeJSON contains the JSON metadata for the struct |
| 387 | +// [ScriptSecretGetResponseEnvelope] |
| 388 | +type scriptSecretGetResponseEnvelopeJSON struct { |
| 389 | + Errors apijson.Field |
| 390 | + Messages apijson.Field |
| 391 | + Success apijson.Field |
| 392 | + Result apijson.Field |
| 393 | + raw string |
| 394 | + ExtraFields map[string]apijson.Field |
| 395 | +} |
| 396 | + |
| 397 | +func (r *ScriptSecretGetResponseEnvelope) UnmarshalJSON(data []byte) (err error) { |
| 398 | + return apijson.UnmarshalRoot(data, r) |
| 399 | +} |
| 400 | + |
| 401 | +func (r scriptSecretGetResponseEnvelopeJSON) RawJSON() string { |
| 402 | + return r.raw |
| 403 | +} |
| 404 | + |
| 405 | +// Whether the API call was successful |
| 406 | +type ScriptSecretGetResponseEnvelopeSuccess bool |
| 407 | + |
| 408 | +const ( |
| 409 | + ScriptSecretGetResponseEnvelopeSuccessTrue ScriptSecretGetResponseEnvelopeSuccess = true |
| 410 | +) |
| 411 | + |
| 412 | +func (r ScriptSecretGetResponseEnvelopeSuccess) IsKnown() bool { |
| 413 | + switch r { |
| 414 | + case ScriptSecretGetResponseEnvelopeSuccessTrue: |
| 415 | + return true |
| 416 | + } |
| 417 | + return false |
| 418 | +} |
0 commit comments