Skip to content

Commit

Permalink
Add pagination extension to team listing (#2169)
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 24ceca4 commit 9e718ee
Show file tree
Hide file tree
Showing 7 changed files with 133 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 13:01:09.536900",
"spec_repo_commit": "c0d26405"
"regenerated": "2023-09-06 14:56:08.114611",
"spec_repo_commit": "5896c0ef"
},
"v2": {
"apigentools_version": "1.6.5",
"regenerated": "2023-09-06 13:01:09.556478",
"spec_repo_commit": "c0d26405"
"regenerated": "2023-09-06 14:56:08.127460",
"spec_repo_commit": "5896c0ef"
}
}
}
4 changes: 4 additions & 0 deletions .generator/schemas/v2/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25989,6 +25989,10 @@ paths:
summary: Get all teams
tags:
- Teams
x-pagination:
limitParam: page[size]
pageParam: page[number]
resultsPath: data
post:
description: 'Create a new team.

Expand Down
48 changes: 48 additions & 0 deletions api/datadogV2/api_teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,54 @@ func (a *TeamsApi) ListTeams(ctx _context.Context, o ...ListTeamsOptionalParamet
return localVarReturnValue, localVarHTTPResponse, nil
}

// ListTeamsWithPagination provides a paginated version of ListTeams returning a channel with all items.
func (a *TeamsApi) ListTeamsWithPagination(ctx _context.Context, o ...ListTeamsOptionalParameters) (<-chan datadog.PaginationResult[Team], func()) {
ctx, cancel := _context.WithCancel(ctx)
pageSize_ := int64(10)
if len(o) == 0 {
o = append(o, ListTeamsOptionalParameters{})
}
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[Team], pageSize_)
go func() {
for {
resp, _, err := a.ListTeams(ctx, o...)
if err != nil {
var returnItem Team
items <- datadog.PaginationResult[Team]{returnItem, err}
break
}
respData, ok := resp.GetDataOk()
if !ok {
break
}
results := *respData

for _, item := range results {
select {
case items <- datadog.PaginationResult[Team]{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
}

// UpdateTeam Update a team.
// Update a team using the team's `id`.
// If the `team_links` relationship is present, the associated links are updated to be in the order they appear in the array, and any existing team links not present are removed.
Expand Down
29 changes: 29 additions & 0 deletions examples/v2/teams/ListTeams_3592098458.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Get all teams 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.NewTeamsApi(apiClient)
resp, _ := api.ListTeamsWithPagination(ctx, *datadogV2.NewListTeamsOptionalParameters().WithPageSize(2))

for paginationResult := range resp {
if paginationResult.Error != nil {
fmt.Fprintf(os.Stderr, "Error when calling `TeamsApi.ListTeams`: %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-05T14:20:30.333Z
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
interactions:
- request:
body: ''
form: {}
headers:
Accept:
- application/json
method: GET
url: https://api.datadoghq.com/api/v2/team?page%5Bsize%5D=2&page%5Bnumber%5D=0
response:
body: '{"data":[{"type":"team","attributes":{"created_at":"2023-03-22T17:12:07.018313+00:00","user_count":0,"description":null,"link_count":0,"modified_at":"2023-03-22T17:12:07.018317+00:00","name":"Example
Team","summary":null,"handle":"api-spec-test-team"},"relationships":{"team_links":{"links":{"related":"/api/v2/team/ac73040e-c8d4-11ed-b2ea-da7ad0900002/links"}},"user_team_permissions":{"links":{"related":"/api/v2/team/ac73040e-c8d4-11ed-b2ea-da7ad0900002/permission-settings"}}},"id":"ac73040e-c8d4-11ed-b2ea-da7ad0900002"},{"type":"team","attributes":{"created_at":"2023-08-21T19:49:10.718738+00:00","user_count":1,"description":"","link_count":0,"modified_at":"2023-08-21T19:49:10.718744+00:00","name":"test-name-12938712938","summary":null,"handle":"test-handle-12938712938"},"relationships":{"team_links":{"links":{"related":"/api/v2/team/cc338b02-405b-11ee-835e-da7ad0900002/links"}},"user_team_permissions":{"links":{"related":"/api/v2/team/cc338b02-405b-11ee-835e-da7ad0900002/permission-settings"}}},"id":"cc338b02-405b-11ee-835e-da7ad0900002"}],"meta":{"pagination":{"number":0,"first_number":0,"prev_number":0,"next_number":1,"last_number":1,"size":2,"type":"number_size","total":3}},"links":{"self":"https://api.datadoghq.com/api/v2/team?page%5Bsize%5D=2&page%5Bnumber%5D=0","last":"https://api.datadoghq.com/api/v2/team?page[number]=1&page[size]=2","next":"https://api.datadoghq.com/api/v2/team?page[number]=1&page[size]=2","prev":null,"first":"https://api.datadoghq.com/api/v2/team?page[number]=0&page[size]=2"}}
'
code: 200
duration: ''
headers:
Content-Type:
- application/json
status: 200 OK
- request:
body: ''
form: {}
headers:
Accept:
- application/json
method: GET
url: https://api.datadoghq.com/api/v2/team?page%5Bsize%5D=2&page%5Bnumber%5D=1
response:
body: '{"data":[{"type":"team","relationships":{"user_team_permissions":{"links":{"related":"/api/v2/team/ecc8affc-27d6-11ee-b33c-da7ad0900002/permission-settings"}},"team_links":{"links":{"related":"/api/v2/team/ecc8affc-27d6-11ee-b33c-da7ad0900002/links"}}},"attributes":{"link_count":0,"handle":"tf-testaccteamlinkbasic-local-1689951453","modified_at":"2023-07-21T14:57:34.412713+00:00","created_at":"2023-07-21T14:57:34.412707+00:00","summary":"123","name":"tf-testaccteamlinkbasic-local-1689951453","user_count":0,"description":"123"},"id":"ecc8affc-27d6-11ee-b33c-da7ad0900002"}],"meta":{"pagination":{"number":1,"first_number":0,"prev_number":0,"next_number":2,"last_number":1,"size":2,"type":"number_size","total":3}},"links":{"self":"https://api.datadoghq.com/api/v2/team?page%5Bsize%5D=2&page%5Bnumber%5D=1","last":"https://api.datadoghq.com/api/v2/team?page[number]=1&page[size]=2","next":"https://api.datadoghq.com/api/v2/team?page[number]=2&page[size]=2","prev":"https://api.datadoghq.com/api/v2/team?page[number]=0&page[size]=2","first":"https://api.datadoghq.com/api/v2/team?page[number]=0&page[size]=2"}}

'
code: 200
duration: ''
headers:
Content-Type:
- application/json
status: 200 OK
version: 1
8 changes: 8 additions & 0 deletions tests/scenarios/features/v2/teams.feature
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ Feature: Teams
Then the response status is 200 OK
And the response "data" has item with field "id" with value "{{ dd_team.data.id }}"

@replay-only @skip-validation @team:DataDog/core-app @with-pagination
Scenario: Get all teams returns "OK" response with pagination
Given new "ListTeams" request
And request contains "page[size]" parameter with value 2
When the request with pagination is sent
Then the response status is 200 OK
And the response has 3 items

@team:DataDog/core-app
Scenario: Get links for a team returns "API error response." response
Given new "GetTeamLinks" request
Expand Down

0 comments on commit 9e718ee

Please sign in to comment.