Skip to content

Commit

Permalink
Add pagination extension to user list (#2166)
Browse files Browse the repository at this point in the history
Co-authored-by: ci.datadog-api-spec <packages@datadoghq.com>
  • Loading branch information
api-clients-generation-pipeline[bot] and ci.datadog-api-spec authored Sep 6, 2023
1 parent 6f0d94c commit 24ceca4
Show file tree
Hide file tree
Showing 7 changed files with 819 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.6.5",
"regenerated": "2023-09-06 12:26:32.656691",
"spec_repo_commit": "07ee6775"
"regenerated": "2023-09-06 13:01:09.536900",
"spec_repo_commit": "c0d26405"
},
"v2": {
"apigentools_version": "1.6.5",
"regenerated": "2023-09-06 12:26:32.675428",
"spec_repo_commit": "07ee6775"
"regenerated": "2023-09-06 13:01:09.556478",
"spec_repo_commit": "c0d26405"
}
}
}
4 changes: 4 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27459,6 +27459,10 @@ paths:
tags:
- Users
x-codegen-request-body-name: body
x-pagination:
limitParam: page[size]
pageParam: page[number]
resultsPath: data
post:
description: Create a user for your organization.
operationId: CreateUser
Expand Down
48 changes: 48 additions & 0 deletions api/datadogV2/api_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,54 @@ func (a *UsersApi) ListUsers(ctx _context.Context, o ...ListUsersOptionalParamet
return localVarReturnValue, localVarHTTPResponse, nil
}

// ListUsersWithPagination provides a paginated version of ListUsers returning a channel with all items.
func (a *UsersApi) ListUsersWithPagination(ctx _context.Context, o ...ListUsersOptionalParameters) (<-chan datadog.PaginationResult[User], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int64(10)
if len(o) == 0 {
o = append(o, ListUsersOptionalParameters{})
}
if o[0].PageSize != nil {
pageSize_ = *o[0].PageSize
}
o[0].PageSize = &pageSize_
page_ := int64(0)
o[0].PageNumber = &page_

items := make(chan datadog.PaginationResult[User], pageSize_)
go func() {
for {
resp, _, err := a.ListUsers(ctx, o...)
if err != nil {
var returnItem User
items <- datadog.PaginationResult[User]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
if !ok {
break
}
results := *respData

for _, item := range results {
select {
case items <- datadog.PaginationResult[User]{item, nil}:
case <-ctx.Done():
close(items)
return
}
}
if len(results) < int(pageSize_) {
break
}
pageOffset_ := *o[0].PageNumber + 1
o[0].PageNumber = &pageOffset_
}
close(items)
}()
return items, cancel
}

// SendInvitations Send invitation emails.
// Sends emails to one or more users inviting them to join the organization.
func (a *UsersApi) SendInvitations(ctx _context.Context, body UserInvitationsRequest) (UserInvitationsResponse, *_nethttp.Response, error) {
Expand Down
29 changes: 29 additions & 0 deletions examples/v2/users/ListUsers_4075885358.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// List all users returns "OK" response with pagination

package main

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)

func main() {
ctx := datadog.NewDefaultContext(context.Background())
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
api := datadogV2.NewUsersApi(apiClient)
resp, _ := api.ListUsersWithPagination(ctx, *datadogV2.NewListUsersOptionalParameters().WithPageSize(2))

for paginationResult := range resp {
if paginationResult.Error != nil {
fmt.Fprintf(os.Stderr, "Error when calling `UsersApi.ListUsers`: %v\n", paginationResult.Error)
}
responseContent, _ := json.MarshalIndent(paginationResult.Item, "", " ")
fmt.Fprintf(os.Stdout, "%s\n", responseContent)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2023-09-05T13:14:24.601Z
Loading

0 comments on commit 24ceca4

Please sign in to comment.