Skip to content

Commit

Permalink
Do not terminate listing for token-based pagination resources on empt…
Browse files Browse the repository at this point in the history
…y response (#530)

## Changes
It is allowed for list APIs to return less than the number of resources
in the page_size limit in a response. In case no items are returned but
there are still items to list, we should continue to list resources
until no next_page_token is included in the response. This matches the
logic from the Go SDK:
https://github.com/databricks/databricks-sdk-go/blob/main/.codegen/api.go.tmpl#L275-L294

## Tests
- [x] Nightly tests pass.
  • Loading branch information
mgyucht authored Feb 8, 2024
1 parent a57b7e0 commit c20bbff
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 186 deletions.
31 changes: 17 additions & 14 deletions .codegen/service.py.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,32 @@ class {{.Name}}API:{{if .Description}}
{{- end}}
while True:
json = {{template "method-do" .}}
if '{{.Pagination.Results.Name}}' not in json or not json['{{.Pagination.Results.Name}}']:
if '{{.Pagination.Results.Name}}' in json:
for v in json['{{.Pagination.Results.Name}}']:
{{if .NeedsOffsetDedupe -}}
i = v['{{.IdentifierField.Name}}']
if i in seen:
continue
seen.add(i)
{{end -}}
yield {{.Pagination.Entity.PascalName}}.from_dict(v)
{{ if .Pagination.Token -}}
if '{{.Pagination.Token.Bind.Name}}' not in json or not json['{{.Pagination.Token.Bind.Name}}']:
return
for v in json['{{.Pagination.Results.Name}}']:
{{if .NeedsOffsetDedupe -}}
i = v['{{.IdentifierField.Name}}']
if i in seen:
continue
seen.add(i)
{{end -}}
yield {{.Pagination.Entity.PascalName}}.from_dict(v)
{{if eq .Path "/api/2.0/clusters/events" -}}
{{if eq "GET" .Verb}}query{{else}}body{{end}}['{{.Pagination.Token.PollField.Name}}'] = json['{{.Pagination.Token.Bind.Name}}']
{{- else if eq .Path "/api/2.0/clusters/events" -}}
if 'next_page' not in json or not json['next_page']:
return
body = json['next_page']
{{- else if .Pagination.Token -}}
if '{{.Pagination.Token.Bind.Name}}' not in json or not json['{{.Pagination.Token.Bind.Name}}']:
{{- else -}}
if '{{.Pagination.Results.Name}}' not in json or not json['{{.Pagination.Results.Name}}']:
return
{{if eq "GET" .Verb}}query{{else}}body{{end}}['{{.Pagination.Token.PollField.Name}}'] = json['{{.Pagination.Token.Bind.Name}}']
{{- else if eq .Pagination.Increment 1 -}}
{{ if eq .Pagination.Increment 1 -}}
query['{{.Pagination.Offset.Name}}'] += 1
{{- else -}}
query['{{.Pagination.Offset.Name}}'] += len(json['{{.Pagination.Results.Name}}'])
{{- end}}
{{- end}}
{{else -}}
json = {{template "method-do" .}}
parsed = {{.Response.PascalName}}.from_dict(json).{{template "safe-snake-name" .Pagination.Results}}
Expand Down
56 changes: 24 additions & 32 deletions databricks/sdk/service/catalog.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions databricks/sdk/service/compute.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 42 additions & 36 deletions databricks/sdk/service/iam.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 6 additions & 8 deletions databricks/sdk/service/jobs.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c20bbff

Please sign in to comment.