Skip to content

Commit

Permalink
feat(api): manual updates (#2981)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Aug 30, 2024
1 parent 2d6bb9a commit 48d5b64
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 149 deletions.
4 changes: 2 additions & 2 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,11 @@ Response Types:
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionZone">SubscriptionZone</a>
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionUpdateResponseUnion">SubscriptionUpdateResponseUnion</a>
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionDeleteResponse">SubscriptionDeleteResponse</a>
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionEditResponseUnion">SubscriptionEditResponseUnion</a>

Methods:

- <code title="put /user/subscriptions/{identifier}">client.User.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionService.Update">Update</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>, body <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionUpdateParams">SubscriptionUpdateParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionUpdateResponseUnion">SubscriptionUpdateResponseUnion</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="delete /user/subscriptions/{identifier}">client.User.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionService.Delete">Delete</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionDeleteResponse">SubscriptionDeleteResponse</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="put /zones/{identifier}/subscription">client.User.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionService.Edit">Edit</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>, body <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionEditParams">SubscriptionEditParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionEditResponseUnion">SubscriptionEditResponseUnion</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="get /user/subscriptions">client.User.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#SubscriptionService.Get">Get</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>) ([]<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user">user</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/user#Subscription">Subscription</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>

## Tokens
Expand Down Expand Up @@ -420,11 +418,13 @@ Methods:
Response Types:

- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionNewResponseUnion">SubscriptionNewResponseUnion</a>
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionUpdateResponseUnion">SubscriptionUpdateResponseUnion</a>
- <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionGetResponseUnion">SubscriptionGetResponseUnion</a>

Methods:

- <code title="post /zones/{identifier}/subscription">client.Zones.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionService.New">New</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>, body <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionNewParams">SubscriptionNewParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionNewResponseUnion">SubscriptionNewResponseUnion</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="put /zones/{identifier}/subscription">client.Zones.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionService.Update">Update</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>, body <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionUpdateParams">SubscriptionUpdateParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionUpdateResponseUnion">SubscriptionUpdateResponseUnion</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="get /zones/{identifier}/subscription">client.Zones.Subscriptions.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionService.Get">Get</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, identifier <a href="https://pkg.go.dev/builtin#string">string</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones">zones</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v2/zones#SubscriptionGetResponseUnion">SubscriptionGetResponseUnion</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>

# LoadBalancers
Expand Down
2 changes: 1 addition & 1 deletion shared/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package shared
type UnionString string

func (UnionString) ImplementsUserSubscriptionUpdateResponseUnion() {}
func (UnionString) ImplementsUserSubscriptionEditResponseUnion() {}
func (UnionString) ImplementsZonesSubscriptionNewResponseUnion() {}
func (UnionString) ImplementsZonesSubscriptionUpdateResponseUnion() {}
func (UnionString) ImplementsZonesSubscriptionGetResponseUnion() {}
func (UnionString) ImplementsLoadBalancersRegionListResponseUnion() {}
func (UnionString) ImplementsLoadBalancersRegionGetResponseUnion() {}
Expand Down
86 changes: 0 additions & 86 deletions user/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,6 @@ func (r *SubscriptionService) Delete(ctx context.Context, identifier string, opt
return
}

// Updates zone subscriptions, either plan or add-ons.
func (r *SubscriptionService) Edit(ctx context.Context, identifier string, body SubscriptionEditParams, opts ...option.RequestOption) (res *SubscriptionEditResponseUnion, err error) {
var env SubscriptionEditResponseEnvelope
opts = append(r.Options[:], opts...)
if identifier == "" {
err = errors.New("missing required identifier parameter")
return
}
path := fmt.Sprintf("zones/%s/subscription", identifier)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}

// Lists all of a user's subscriptions.
func (r *SubscriptionService) Get(ctx context.Context, opts ...option.RequestOption) (res *[]Subscription, err error) {
var env SubscriptionGetResponseEnvelope
Expand Down Expand Up @@ -416,24 +399,6 @@ func (r subscriptionDeleteResponseJSON) RawJSON() string {
return r.raw
}

// Union satisfied by
// [user.SubscriptionEditResponseUnnamedSchemaRef9444735ca60712dbcf8afd832eb5716a]
// or [shared.UnionString].
type SubscriptionEditResponseUnion interface {
ImplementsUserSubscriptionEditResponseUnion()
}

func init() {
apijson.RegisterUnion(
reflect.TypeOf((*SubscriptionEditResponseUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
)
}

type SubscriptionUpdateParams struct {
Subscription SubscriptionParam `json:"subscription,required"`
}
Expand Down Expand Up @@ -485,57 +450,6 @@ func (r SubscriptionUpdateResponseEnvelopeSuccess) IsKnown() bool {
return false
}

type SubscriptionEditParams struct {
Subscription SubscriptionParam `json:"subscription,required"`
}

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

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

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

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

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

// Whether the API call was successful
type SubscriptionEditResponseEnvelopeSuccess bool

const (
SubscriptionEditResponseEnvelopeSuccessTrue SubscriptionEditResponseEnvelopeSuccess = true
)

func (r SubscriptionEditResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case SubscriptionEditResponseEnvelopeSuccessTrue:
return true
}
return false
}

type SubscriptionGetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Expand Down
60 changes: 0 additions & 60 deletions user/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,66 +97,6 @@ func TestSubscriptionDelete(t *testing.T) {
}
}

func TestSubscriptionEditWithOptionalParams(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.User.Subscriptions.Edit(
context.TODO(),
"506e3185e9c882d175a2d0cb0093d9f2",
user.SubscriptionEditParams{
Subscription: user.SubscriptionParam{
App: cloudflare.F(user.SubscriptionAppParam{
InstallID: cloudflare.F("install_id"),
}),
ComponentValues: cloudflare.F([]user.SubscriptionComponentParam{{
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}, {
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}, {
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}}),
Frequency: cloudflare.F(user.SubscriptionFrequencyWeekly),
RatePlan: cloudflare.F(user.RatePlanParam{
ID: cloudflare.F("free"),
Currency: cloudflare.F("USD"),
ExternallyManaged: cloudflare.F(false),
IsContract: cloudflare.F(false),
PublicName: cloudflare.F("Business Plan"),
Scope: cloudflare.F("zone"),
Sets: cloudflare.F([]string{"string", "string", "string"}),
}),
Zone: cloudflare.F(user.SubscriptionZoneParam{}),
},
},
)
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())
}
}

func TestSubscriptionGet(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
Expand Down
86 changes: 86 additions & 0 deletions zones/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,23 @@ func (r *SubscriptionService) New(ctx context.Context, identifier string, body S
return
}

// Updates zone subscriptions, either plan or add-ons.
func (r *SubscriptionService) Update(ctx context.Context, identifier string, body SubscriptionUpdateParams, opts ...option.RequestOption) (res *SubscriptionUpdateResponseUnion, err error) {
var env SubscriptionUpdateResponseEnvelope
opts = append(r.Options[:], opts...)
if identifier == "" {
err = errors.New("missing required identifier parameter")
return
}
path := fmt.Sprintf("zones/%s/subscription", identifier)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPut, path, body, &env, opts...)
if err != nil {
return
}
res = &env.Result
return
}

// Lists zone subscription details.
func (r *SubscriptionService) Get(ctx context.Context, identifier string, opts ...option.RequestOption) (res *SubscriptionGetResponseUnion, err error) {
var env SubscriptionGetResponseEnvelope
Expand Down Expand Up @@ -88,6 +105,24 @@ func init() {
)
}

// Union satisfied by
// [zones.SubscriptionUpdateResponseUnnamedSchemaRef9444735ca60712dbcf8afd832eb5716a]
// or [shared.UnionString].
type SubscriptionUpdateResponseUnion interface {
ImplementsZonesSubscriptionUpdateResponseUnion()
}

func init() {
apijson.RegisterUnion(
reflect.TypeOf((*SubscriptionUpdateResponseUnion)(nil)).Elem(),
"",
apijson.UnionVariant{
TypeFilter: gjson.String,
Type: reflect.TypeOf(shared.UnionString("")),
},
)
}

// Union satisfied by
// [zones.SubscriptionGetResponseUnnamedSchemaRef9444735ca60712dbcf8afd832eb5716a]
// or [shared.UnionString].
Expand Down Expand Up @@ -157,6 +192,57 @@ func (r SubscriptionNewResponseEnvelopeSuccess) IsKnown() bool {
return false
}

type SubscriptionUpdateParams struct {
Subscription user.SubscriptionParam `json:"subscription,required"`
}

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

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

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

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

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

// Whether the API call was successful
type SubscriptionUpdateResponseEnvelopeSuccess bool

const (
SubscriptionUpdateResponseEnvelopeSuccessTrue SubscriptionUpdateResponseEnvelopeSuccess = true
)

func (r SubscriptionUpdateResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case SubscriptionUpdateResponseEnvelopeSuccessTrue:
return true
}
return false
}

type SubscriptionGetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Expand Down
60 changes: 60 additions & 0 deletions zones/subscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,66 @@ func TestSubscriptionNewWithOptionalParams(t *testing.T) {
}
}

func TestSubscriptionUpdateWithOptionalParams(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.Zones.Subscriptions.Update(
context.TODO(),
"506e3185e9c882d175a2d0cb0093d9f2",
zones.SubscriptionUpdateParams{
Subscription: user.SubscriptionParam{
App: cloudflare.F(user.SubscriptionAppParam{
InstallID: cloudflare.F("install_id"),
}),
ComponentValues: cloudflare.F([]user.SubscriptionComponentParam{{
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}, {
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}, {
Default: cloudflare.F(5.000000),
Name: cloudflare.F("page_rules"),
Price: cloudflare.F(5.000000),
Value: cloudflare.F(20.000000),
}}),
Frequency: cloudflare.F(user.SubscriptionFrequencyWeekly),
RatePlan: cloudflare.F(user.RatePlanParam{
ID: cloudflare.F("free"),
Currency: cloudflare.F("USD"),
ExternallyManaged: cloudflare.F(false),
IsContract: cloudflare.F(false),
PublicName: cloudflare.F("Business Plan"),
Scope: cloudflare.F("zone"),
Sets: cloudflare.F([]string{"string", "string", "string"}),
}),
Zone: cloudflare.F(user.SubscriptionZoneParam{}),
},
},
)
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())
}
}

func TestSubscriptionGet(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
Expand Down

0 comments on commit 48d5b64

Please sign in to comment.