Skip to content

Commit

Permalink
feat(api): move account subscription into the hierarchy (#3041)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored and stainless-bot committed Sep 10, 2024
1 parent 7453e9a commit 92e558b
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 300 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 1330
configured_endpoints: 1331
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/cloudflare%2Fcloudflare-625db41a04731c747d5718b947997356c1cee171ce155038378db3e82c739591.yml
8 changes: 5 additions & 3 deletions accounts/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
// automatically. You should not instantiate this service directly, and instead use
// the [NewAccountService] method instead.
type AccountService struct {
Options []option.RequestOption
Members *MemberService
Roles *RoleService
Options []option.RequestOption
Members *MemberService
Roles *RoleService
Subscriptions *SubscriptionService
}

// NewAccountService generates a new service that applies the given options to each
Expand All @@ -39,6 +40,7 @@ func NewAccountService(opts ...option.RequestOption) (r *AccountService) {
r.Options = opts
r.Members = NewMemberService(opts...)
r.Roles = NewRoleService(opts...)
r.Subscriptions = NewSubscriptionService(opts...)
return
}

Expand Down
103 changes: 55 additions & 48 deletions subscriptions/subscription.go → accounts/subscription.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package subscriptions
package accounts

import (
"context"
Expand All @@ -9,7 +9,6 @@ import (
"net/http"

"github.com/cloudflare/cloudflare-go/v2/internal/apijson"
"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/option"
Expand All @@ -36,16 +35,16 @@ func NewSubscriptionService(opts ...option.RequestOption) (r *SubscriptionServic
return
}

// Create a zone subscription, either plan or add-ons.
func (r *SubscriptionService) New(ctx context.Context, identifier string, body SubscriptionNewParams, opts ...option.RequestOption) (res *interface{}, err error) {
// Creates an account subscription.
func (r *SubscriptionService) New(ctx context.Context, params SubscriptionNewParams, opts ...option.RequestOption) (res *interface{}, err error) {
var env SubscriptionNewResponseEnvelope
opts = append(r.Options[:], opts...)
if identifier == "" {
err = errors.New("missing required identifier parameter")
if params.AccountID.Value == "" {
err = errors.New("missing required account_id parameter")
return
}
path := fmt.Sprintf("zones/%s/subscription", identifier)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &env, opts...)
path := fmt.Sprintf("accounts/%s/subscriptions", params.AccountID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &env, opts...)
if err != nil {
return
}
Expand Down Expand Up @@ -74,33 +73,6 @@ func (r *SubscriptionService) Update(ctx context.Context, subscriptionIdentifier
return
}

// Lists all of an account's subscriptions.
func (r *SubscriptionService) List(ctx context.Context, query SubscriptionListParams, opts ...option.RequestOption) (res *pagination.SinglePage[user.Subscription], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if query.AccountID.Value == "" {
err = errors.New("missing required account_id parameter")
return
}
path := fmt.Sprintf("accounts/%s/subscriptions", query.AccountID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, nil, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}

// Lists all of an account's subscriptions.
func (r *SubscriptionService) ListAutoPaging(ctx context.Context, query SubscriptionListParams, opts ...option.RequestOption) *pagination.SinglePageAutoPager[user.Subscription] {
return pagination.NewSinglePageAutoPager(r.List(ctx, query, opts...))
}

// Deletes an account's subscription.
func (r *SubscriptionService) Delete(ctx context.Context, subscriptionIdentifier string, body SubscriptionDeleteParams, opts ...option.RequestOption) (res *SubscriptionDeleteResponse, err error) {
var env SubscriptionDeleteResponseEnvelope
Expand All @@ -122,15 +94,15 @@ func (r *SubscriptionService) Delete(ctx context.Context, subscriptionIdentifier
return
}

// Lists zone subscription details.
func (r *SubscriptionService) Get(ctx context.Context, identifier string, opts ...option.RequestOption) (res *interface{}, err error) {
// Lists all of an account's subscriptions.
func (r *SubscriptionService) Get(ctx context.Context, query SubscriptionGetParams, opts ...option.RequestOption) (res *[]user.Subscription, err error) {
var env SubscriptionGetResponseEnvelope
opts = append(r.Options[:], opts...)
if identifier == "" {
err = errors.New("missing required identifier parameter")
if query.AccountID.Value == "" {
err = errors.New("missing required account_id parameter")
return
}
path := fmt.Sprintf("zones/%s/subscription", identifier)
path := fmt.Sprintf("accounts/%s/subscriptions", query.AccountID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return
Expand Down Expand Up @@ -162,6 +134,8 @@ func (r subscriptionDeleteResponseJSON) RawJSON() string {
}

type SubscriptionNewParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
Subscription user.SubscriptionParam `json:"subscription,required"`
}

Expand Down Expand Up @@ -265,11 +239,6 @@ func (r SubscriptionUpdateResponseEnvelopeSuccess) IsKnown() bool {
return false
}

type SubscriptionListParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
}

type SubscriptionDeleteParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
Expand Down Expand Up @@ -318,13 +287,19 @@ func (r SubscriptionDeleteResponseEnvelopeSuccess) IsKnown() bool {
return false
}

type SubscriptionGetParams struct {
// Identifier
AccountID param.Field[string] `path:"account_id,required"`
}

type SubscriptionGetResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
Result interface{} `json:"result,required"`
Result []user.Subscription `json:"result,required,nullable"`
// Whether the API call was successful
Success SubscriptionGetResponseEnvelopeSuccess `json:"success,required"`
JSON subscriptionGetResponseEnvelopeJSON `json:"-"`
Success SubscriptionGetResponseEnvelopeSuccess `json:"success,required"`
ResultInfo SubscriptionGetResponseEnvelopeResultInfo `json:"result_info"`
JSON subscriptionGetResponseEnvelopeJSON `json:"-"`
}

// subscriptionGetResponseEnvelopeJSON contains the JSON metadata for the struct
Expand All @@ -334,6 +309,7 @@ type subscriptionGetResponseEnvelopeJSON struct {
Messages apijson.Field
Result apijson.Field
Success apijson.Field
ResultInfo apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
Expand All @@ -360,3 +336,34 @@ func (r SubscriptionGetResponseEnvelopeSuccess) IsKnown() bool {
}
return false
}

type SubscriptionGetResponseEnvelopeResultInfo struct {
// Total number of results for the requested service
Count float64 `json:"count"`
// Current page within paginated list of results
Page float64 `json:"page"`
// Number of results per page of results
PerPage float64 `json:"per_page"`
// Total results available without any search parameters
TotalCount float64 `json:"total_count"`
JSON subscriptionGetResponseEnvelopeResultInfoJSON `json:"-"`
}

// subscriptionGetResponseEnvelopeResultInfoJSON contains the JSON metadata for the
// struct [SubscriptionGetResponseEnvelopeResultInfo]
type subscriptionGetResponseEnvelopeResultInfoJSON struct {
Count apijson.Field
Page apijson.Field
PerPage apijson.Field
TotalCount apijson.Field
raw string
ExtraFields map[string]apijson.Field
}

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

func (r subscriptionGetResponseEnvelopeResultInfoJSON) RawJSON() string {
return r.raw
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

package subscriptions_test
package accounts_test

import (
"context"
Expand All @@ -9,9 +9,9 @@ import (
"testing"

"github.com/cloudflare/cloudflare-go/v2"
"github.com/cloudflare/cloudflare-go/v2/accounts"
"github.com/cloudflare/cloudflare-go/v2/internal/testutil"
"github.com/cloudflare/cloudflare-go/v2/option"
"github.com/cloudflare/cloudflare-go/v2/subscriptions"
"github.com/cloudflare/cloudflare-go/v2/user"
)

Expand All @@ -28,24 +28,21 @@ func TestSubscriptionNewWithOptionalParams(t *testing.T) {
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("user@example.com"),
)
_, err := client.Subscriptions.New(
context.TODO(),
"506e3185e9c882d175a2d0cb0093d9f2",
subscriptions.SubscriptionNewParams{
Subscription: user.SubscriptionParam{
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"}),
}),
},
_, err := client.Accounts.Subscriptions.New(context.TODO(), accounts.SubscriptionNewParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
Subscription: user.SubscriptionParam{
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"}),
}),
},
)
})
if err != nil {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
Expand All @@ -68,10 +65,10 @@ func TestSubscriptionUpdateWithOptionalParams(t *testing.T) {
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("user@example.com"),
)
_, err := client.Subscriptions.Update(
_, err := client.Accounts.Subscriptions.Update(
context.TODO(),
"506e3185e9c882d175a2d0cb0093d9f2",
subscriptions.SubscriptionUpdateParams{
accounts.SubscriptionUpdateParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
Subscription: user.SubscriptionParam{
Frequency: cloudflare.F(user.SubscriptionFrequencyWeekly),
Expand All @@ -96,31 +93,6 @@ func TestSubscriptionUpdateWithOptionalParams(t *testing.T) {
}
}

func TestSubscriptionList(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("user@example.com"),
)
_, err := client.Subscriptions.List(context.TODO(), subscriptions.SubscriptionListParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
})
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 TestSubscriptionDelete(t *testing.T) {
baseURL := "http://localhost:4010"
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
Expand All @@ -134,10 +106,10 @@ func TestSubscriptionDelete(t *testing.T) {
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("user@example.com"),
)
_, err := client.Subscriptions.Delete(
_, err := client.Accounts.Subscriptions.Delete(
context.TODO(),
"506e3185e9c882d175a2d0cb0093d9f2",
subscriptions.SubscriptionDeleteParams{
accounts.SubscriptionDeleteParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
},
)
Expand All @@ -163,7 +135,9 @@ func TestSubscriptionGet(t *testing.T) {
option.WithAPIKey("144c9defac04969c7bfad8efaa8ea194"),
option.WithAPIEmail("user@example.com"),
)
_, err := client.Subscriptions.Get(context.TODO(), "506e3185e9c882d175a2d0cb0093d9f2")
_, err := client.Accounts.Subscriptions.Get(context.TODO(), accounts.SubscriptionGetParams{
AccountID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
})
if err != nil {
var apierr *cloudflare.Error
if errors.As(err, &apierr) {
Expand Down
Loading

0 comments on commit 92e558b

Please sign in to comment.