MCPcopy
hub / github.com/larksuite/cli / apiPaginate

Function apiPaginate

cmd/api/api.go:304–376  ·  view source on GitHub ↗
(ctx context.Context, ac *client.APIClient, request client.RawApiRequest, format output.Format, jqExpr string, out, errOut io.Writer, commandPath string, pagOpts client.PaginationOptions)

Source from the content-addressed store, hash-verified

302}
303
304func apiPaginate(ctx context.Context, ac *client.APIClient, request client.RawApiRequest, format output.Format, jqExpr string, out, errOut io.Writer, commandPath string, pagOpts client.PaginationOptions) error {
305 if pagOpts.Identity == "" {
306 pagOpts.Identity = request.As
307 }
308 // When jq is set, always aggregate all pages then filter.
309 if jqExpr != "" {
310 result, err := ac.PaginateAll(ctx, request, pagOpts)
311 if err != nil {
312 return errs.MarkRaw(err)
313 }
314 if apiErr := ac.CheckResponse(result, pagOpts.Identity); apiErr != nil {
315 output.FormatValue(out, result, output.FormatJSON)
316 return errs.MarkRaw(apiErr)
317 }
318 return output.WriteSuccessEnvelope(output.SuccessEnvelopeData(result), output.SuccessEnvelopeOptions{
319 CommandPath: commandPath,
320 Identity: string(pagOpts.Identity),
321 JqExpr: jqExpr,
322 Out: out,
323 ErrOut: errOut,
324 })
325 }
326
327 switch format {
328 case output.FormatNDJSON, output.FormatTable, output.FormatCSV:
329 pf := output.NewPaginatedFormatter(out, format)
330 result, hasItems, err := ac.StreamPages(ctx, request, func(items []interface{}) error {
331 // Streaming formats intentionally emit each page after that page has
332 // passed safety scanning. A later page may still fail, so callers
333 // must use the exit code to distinguish complete vs partial output.
334 scanResult := output.ScanForSafety(commandPath, items, errOut)
335 if scanResult.Blocked {
336 return scanResult.BlockErr
337 }
338 if scanResult.Alert != nil {
339 output.WriteAlertWarning(errOut, scanResult.Alert)
340 }
341 pf.FormatPage(items)
342 return nil
343 }, pagOpts)
344 if err != nil {
345 return errs.MarkRaw(err)
346 }
347 if apiErr := ac.CheckResponse(result, pagOpts.Identity); apiErr != nil {
348 return errs.MarkRaw(apiErr)
349 }
350 if !hasItems {
351 fmt.Fprintf(errOut, "warning: this API does not return a list, format %q is not supported, falling back to json\n", format)
352 return output.WriteSuccessEnvelope(output.SuccessEnvelopeData(result), output.SuccessEnvelopeOptions{
353 CommandPath: commandPath,
354 Identity: string(pagOpts.Identity),
355 Out: out,
356 ErrOut: errOut,
357 })
358 }
359 return nil
360 default:
361 result, err := ac.PaginateAll(ctx, request, pagOpts)

Callers 1

apiRunFunction · 0.85

Calls 11

FormatPageMethod · 0.95
MarkRawFunction · 0.92
FormatValueFunction · 0.92
WriteSuccessEnvelopeFunction · 0.92
SuccessEnvelopeDataFunction · 0.92
NewPaginatedFormatterFunction · 0.92
ScanForSafetyFunction · 0.92
WriteAlertWarningFunction · 0.92
CheckResponseMethod · 0.80
PaginateAllMethod · 0.45
StreamPagesMethod · 0.45

Tested by

no test coverage detected