Skip to content

Commit

Permalink
feat(api): api update (#3600)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Nov 14, 2024
1 parent 4aaf8f2 commit 3082cf5
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 1416
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-3f87896b62cce36e33c03f416ad98add2717ccd78f1743925c160a6c6a67ff15.yml
configured_endpoints: 1417
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-4396f2b615f2349cc28c23e9df6cf66c1c0fd8257d18df0ce54d7e74c839bf9f.yml
8 changes: 8 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2173,6 +2173,14 @@ Methods:

#### Upload

Response Types:

- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers">workers</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers#ScriptAssetUploadNewResponse">ScriptAssetUploadNewResponse</a>

Methods:

- <code title="post /accounts/{account_id}/workers/scripts/{script_name}/assets-upload-session">client.Workers.Scripts.Assets.Upload.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers#ScriptAssetUploadService.New">New</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, scriptName <a href="https://pkg.go.dev/builtin#string">string</a>, params <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers">workers</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers#ScriptAssetUploadNewParams">ScriptAssetUploadNewParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers">workers</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v3/workers#ScriptAssetUploadNewResponse">ScriptAssetUploadNewResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>

### Subdomain

Response Types:
Expand Down
121 changes: 121 additions & 0 deletions workers/scriptassetupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
package workers

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/cloudflare/cloudflare-go/v3/internal/apijson"
"github.com/cloudflare/cloudflare-go/v3/internal/param"
"github.com/cloudflare/cloudflare-go/v3/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v3/option"
"github.com/cloudflare/cloudflare-go/v3/shared"
)

// ScriptAssetUploadService contains methods and other services that help with
Expand All @@ -24,3 +33,115 @@ func NewScriptAssetUploadService(opts ...option.RequestOption) (r *ScriptAssetUp
r.Options = opts
return
}

// Start uploading a collection of assets for use in a Worker version.
func (r *ScriptAssetUploadService) New(ctx context.Context, scriptName string, params ScriptAssetUploadNewParams, opts ...option.RequestOption) (res *ScriptAssetUploadNewResponse, err error) {
var env ScriptAssetUploadNewResponseEnvelope
opts = append(r.Options[:], opts...)
if params.AccountID.Value == "" {
err = errors.New("missing required account_id parameter")
return
}
if scriptName == "" {
err = errors.New("missing required script_name parameter")
return
}
path := fmt.Sprintf("accounts/%s/workers/scripts/%s/assets-upload-session", params.AccountID, scriptName)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}

type ScriptAssetUploadNewResponse struct {
// The requests to make to upload assets.
Buckets [][]string `json:"buckets"`
// A JWT to use as authentication for uploading assets.
JWT string `json:"jwt"`
JSON scriptAssetUploadNewResponseJSON `json:"-"`
}

// scriptAssetUploadNewResponseJSON contains the JSON metadata for the struct
// [ScriptAssetUploadNewResponse]
type scriptAssetUploadNewResponseJSON struct {
Buckets apijson.Field
JWT apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *ScriptAssetUploadNewResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r scriptAssetUploadNewResponseJSON) RawJSON() string {
return r.raw
}

type ScriptAssetUploadNewParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
// A manifest ([path]: {hash, size}) map of files to upload. As an example,
// `/blog/hello-world.html` would be a valid path key.
Manifest param.Field[map[string]ScriptAssetUploadNewParamsManifest] `json:"manifest"`
}

func (r ScriptAssetUploadNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}

type ScriptAssetUploadNewParamsManifest struct {
// The hash of the file.
Hash param.Field[string] `json:"hash"`
// The size of the file in bytes.
Size param.Field[int64] `json:"size"`
}

func (r ScriptAssetUploadNewParamsManifest) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}

type ScriptAssetUploadNewResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success ScriptAssetUploadNewResponseEnvelopeSuccess `json:"success,required"`
Result ScriptAssetUploadNewResponse `json:"result"`
JSON scriptAssetUploadNewResponseEnvelopeJSON `json:"-"`
}

// scriptAssetUploadNewResponseEnvelopeJSON contains the JSON metadata for the
// struct [ScriptAssetUploadNewResponseEnvelope]
type scriptAssetUploadNewResponseEnvelopeJSON struct {
Errors apijson.Field
Messages apijson.Field
Success apijson.Field
Result apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

func (r *ScriptAssetUploadNewResponseEnvelope) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}

func (r scriptAssetUploadNewResponseEnvelopeJSON) RawJSON() string {
return r.raw
}

// Whether the API call was successful
type ScriptAssetUploadNewResponseEnvelopeSuccess bool

const (
ScriptAssetUploadNewResponseEnvelopeSuccessTrue ScriptAssetUploadNewResponseEnvelopeSuccess = true
)

func (r ScriptAssetUploadNewResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case ScriptAssetUploadNewResponseEnvelopeSuccessTrue:
return true
}
return false
}
50 changes: 50 additions & 0 deletions workers/scriptassetupload_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package workers_test

import (
"context"
"errors"
"os"
"testing"

"github.com/cloudflare/cloudflare-go/v3"
"github.com/cloudflare/cloudflare-go/v3/internal/testutil"
"github.com/cloudflare/cloudflare-go/v3/option"
"github.com/cloudflare/cloudflare-go/v3/workers"
)

func TestScriptAssetUploadNewWithOptionalParams(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
baseURL = envURL
}
if !testutil.CheckTestServer(t, baseURL) {
return
}
client := cloudflare.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("[email protected]"),
)
_, err := client.Workers.Scripts.Assets.Upload.New(
context.TODO(),
"this-is_my_script-01",
workers.ScriptAssetUploadNewParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
Manifest: cloudflare.F(map[string]workers.ScriptAssetUploadNewParamsManifest{
"foo": {
Hash: cloudflare.F("hash"),
Size: cloudflare.F(int64(0)),
},
}),
},
)
if err != nil {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
t.Log(string(apierr.DumpRequest(true)))
}
t.Fatalf("err should be nil: %s", err.Error())
}
}

0 comments on commit 3082cf5

Please sign in to comment.