Skip to content

Commit

Permalink
feat(account_roles): explicitly configure page_pagination for listi…
Browse files Browse the repository at this point in the history
…ng (#3889)
  • Loading branch information
stainless-app[bot] committed Feb 4, 2025
1 parent a4e65a2 commit 7d4a96b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
99 changes: 82 additions & 17 deletions accounts/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/cloudflare/cloudflare-go/v4/internal/param"
"github.com/cloudflare/cloudflare-go/v4/internal/requestconfig"
"github.com/cloudflare/cloudflare-go/v4/option"
"github.com/cloudflare/cloudflare-go/v4/packages/pagination"
"github.com/cloudflare/cloudflare-go/v4/shared"
)

Expand All @@ -36,30 +35,20 @@ func NewRoleService(opts ...option.RequestOption) (r *RoleService) {
}

// Get all available roles for an account.
func (r *RoleService) List(ctx context.Context, query RoleListParams, opts ...option.RequestOption) (res *pagination.SinglePage[shared.Role], err error) {
var raw *http.Response
func (r *RoleService) List(ctx context.Context, query RoleListParams, opts ...option.RequestOption) (res *[]shared.Role, err error) {
var env RoleListResponseEnvelope
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/roles", query.AccountID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, nil, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &env, opts...)
if err != nil {
return nil, err
return
}
res.SetPageConfig(cfg, raw)
return res, nil
}

// Get all available roles for an account.
func (r *RoleService) ListAutoPaging(ctx context.Context, query RoleListParams, opts ...option.RequestOption) *pagination.SinglePageAutoPager[shared.Role] {
return pagination.NewSinglePageAutoPager(r.List(ctx, query, opts...))
res = &env.Result
return
}

// Get information about a specific role for an account.
Expand Down Expand Up @@ -88,6 +77,82 @@ type RoleListParams struct {
AccountID param.Field[string] `path:"account_id,required"`
}

type RoleListResponseEnvelope struct {
Errors []shared.ResponseInfo `json:"errors,required"`
Messages []shared.ResponseInfo `json:"messages,required"`
// Whether the API call was successful
Success RoleListResponseEnvelopeSuccess `json:"success,required"`
Result []shared.Role `json:"result,nullable"`
ResultInfo RoleListResponseEnvelopeResultInfo `json:"result_info"`
JSON roleListResponseEnvelopeJSON `json:"-"`
}

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

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

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

// Whether the API call was successful
type RoleListResponseEnvelopeSuccess bool

const (
RoleListResponseEnvelopeSuccessTrue RoleListResponseEnvelopeSuccess = true
)

func (r RoleListResponseEnvelopeSuccess) IsKnown() bool {
switch r {
case RoleListResponseEnvelopeSuccessTrue:
return true
}
return false
}

type RoleListResponseEnvelopeResultInfo 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 roleListResponseEnvelopeResultInfoJSON `json:"-"`
}

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

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

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

type RoleGetParams struct {
// Account identifier tag.
AccountID param.Field[string] `path:"account_id,required"`
Expand Down
2 changes: 1 addition & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Methods:

Methods:

- <code title="get /accounts/{account_id}/roles">client.Accounts.Roles.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleService.List">List</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, query <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts">accounts</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleListParams">RoleListParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/packages/pagination">pagination</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/packages/pagination#SinglePage">SinglePage</a>[<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared">shared</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared#Role">Role</a>], <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="get /accounts/{account_id}/roles">client.Accounts.Roles.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleService.List">List</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, query <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts">accounts</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleListParams">RoleListParams</a>) ([]<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared">shared</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared#Role">Role</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>
- <code title="get /accounts/{account_id}/roles/{role_id}">client.Accounts.Roles.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleService.Get">Get</a>(ctx <a href="https://pkg.go.dev/context">context</a>.<a href="https://pkg.go.dev/context#Context">Context</a>, roleID <a href="https://pkg.go.dev/builtin#string">string</a>, query <a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts">accounts</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/accounts#RoleGetParams">RoleGetParams</a>) (<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared">shared</a>.<a href="https://pkg.go.dev/github.com/cloudflare/cloudflare-go/v4/shared#Role">Role</a>, <a href="https://pkg.go.dev/builtin#error">error</a>)</code>

## Subscriptions
Expand Down

0 comments on commit 7d4a96b

Please sign in to comment.