gitee.com/larksuite/oapi-sdk-go/v3@v3.0.3/service/drive/v1/api_ext.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2022 Lark Technologies Pte. Ltd.
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
     7   *
     8   * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
     9   *
    10   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    11   */
    12  
    13  package larkdrive
    14  
    15  import (
    16  	"context"
    17  	"errors"
    18  	"fmt"
    19  	"math"
    20  
    21  	"gitee.com/larksuite/oapi-sdk-go/v3/core"
    22  )
    23  
    24  func (f *file) ListByIterator(ctx context.Context, req *ListFileReq, options ...larkcore.RequestOptionFunc) (*ListFileIterator, error) {
    25  	return &ListFileIterator{
    26  		ctx:      ctx,
    27  		req:      req,
    28  		listFunc: f.List,
    29  		options:  options,
    30  		limit:    math.MaxInt64}, nil
    31  }
    32  
    33  type ListFileIterator struct {
    34  	nextPageToken *string
    35  	items         []*File
    36  	index         int
    37  	limit         int
    38  	ctx           context.Context
    39  	req           *ListFileReq
    40  	listFunc      func(ctx context.Context, req *ListFileReq, options ...larkcore.RequestOptionFunc) (*ListFileResp, error)
    41  	options       []larkcore.RequestOptionFunc
    42  	curlNum       int
    43  }
    44  
    45  func (iterator *ListFileIterator) Next() (bool, *File, error) {
    46  	// 达到最大量,则返回
    47  	if iterator.limit > 0 && iterator.curlNum >= iterator.limit {
    48  		return false, nil, nil
    49  	}
    50  
    51  	// 为0则拉取数据
    52  	if iterator.index == 0 || iterator.index >= len(iterator.items) {
    53  		if iterator.index != 0 && iterator.nextPageToken == nil {
    54  			return false, nil, nil
    55  		}
    56  		if iterator.nextPageToken != nil {
    57  			iterator.req.apiReq.QueryParams.Set("page_token", *iterator.nextPageToken)
    58  		}
    59  		resp, err := iterator.listFunc(iterator.ctx, iterator.req, iterator.options...)
    60  		if err != nil {
    61  			return false, nil, err
    62  		}
    63  
    64  		if resp.Code != 0 {
    65  			return false, nil, errors.New(fmt.Sprintf("Code:%d,Msg:%s", resp.Code, resp.Msg))
    66  		}
    67  
    68  		if len(resp.Data.Files) == 0 {
    69  			return false, nil, nil
    70  		}
    71  
    72  		iterator.nextPageToken = resp.Data.NextPageToken
    73  		iterator.items = resp.Data.Files
    74  		iterator.index = 0
    75  	}
    76  
    77  	block := iterator.items[iterator.index]
    78  	iterator.index++
    79  	iterator.curlNum++
    80  	return true, block, nil
    81  }
    82  
    83  func (iterator *ListFileIterator) NextPageToken() *string {
    84  	return iterator.nextPageToken
    85  }