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

Method StreamPages

internal/client/client.go:448–486  ·  view source on GitHub ↗

StreamPages fetches all pages and streams each page's list items via onItems. Returns the last page result (for error checking), whether any list items were found, and any network error. Use this for streaming formats (ndjson, table, csv).

(ctx context.Context, request RawApiRequest, onItems func([]interface{}) error, opts PaginationOptions)

Source from the content-addressed store, hash-verified

446// Returns the last page result (for error checking), whether any list items were found,
447// and any network error. Use this for streaming formats (ndjson, table, csv).
448func (c *APIClient) StreamPages(ctx context.Context, request RawApiRequest, onItems func([]interface{}) error, opts PaginationOptions) (result interface{}, hasItems bool, err error) {
449 totalItems := 0
450 results, loopErr := c.paginateLoop(ctx, request, opts, func(r interface{}) error {
451 resultMap, ok := r.(map[string]interface{})
452 if !ok {
453 return nil
454 }
455 data, ok := resultMap["data"].(map[string]interface{})
456 if !ok {
457 return nil
458 }
459 arrayField := output.FindArrayField(data)
460 if arrayField == "" {
461 return nil
462 }
463 items, ok := data[arrayField].([]interface{})
464 if !ok {
465 return nil
466 }
467 totalItems += len(items)
468 if err := onItems(items); err != nil {
469 return err
470 }
471 hasItems = true
472 return nil
473 })
474 if loopErr != nil {
475 return nil, false, loopErr
476 }
477
478 if hasItems {
479 fmt.Fprintf(c.ErrOut, "[pagination] streamed %d pages, %d total items\n", len(results), totalItems)
480 }
481
482 if len(results) > 0 {
483 return results[len(results)-1], hasItems, nil
484 }
485 return map[string]interface{}{"code": 0, "msg": "success", "data": map[string]interface{}{}}, false, nil
486}
487
488// CheckResponse inspects a Lark API response for business-level errors (non-zero code)
489// and routes the result through errclass.BuildAPIError so the wire envelope carries

Calls 2

paginateLoopMethod · 0.95
FindArrayFieldFunction · 0.92