github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/get_follower_ids.go (about)

     1  // Copyright 2016 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package linebot
    16  
    17  import (
    18  	"context"
    19  	"net/url"
    20  )
    21  
    22  // GetFollowerIDs method
    23  func (client *Client) GetFollowerIDs(continuationToken string) *GetFollowerIDsCall {
    24  	return &GetFollowerIDsCall{
    25  		c:                 client,
    26  		continuationToken: continuationToken,
    27  	}
    28  }
    29  
    30  // GetFollowerIDsCall type
    31  type GetFollowerIDsCall struct {
    32  	c   *Client
    33  	ctx context.Context
    34  
    35  	continuationToken string
    36  }
    37  
    38  // WithContext method
    39  func (call *GetFollowerIDsCall) WithContext(ctx context.Context) *GetFollowerIDsCall {
    40  	call.ctx = ctx
    41  	return call
    42  }
    43  
    44  // Do method
    45  func (call *GetFollowerIDsCall) Do() (*UserIDsResponse, error) {
    46  	var q url.Values
    47  	if call.continuationToken != "" {
    48  		q = url.Values{"start": []string{call.continuationToken}}
    49  	}
    50  	res, err := call.c.get(call.ctx, call.c.endpointBase, APIEndpointGetFollowerIDs, q)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	defer closeResponse(res)
    55  	return decodeToUserIDsResponse(res)
    56  }
    57  
    58  // NewScanner returns Group IDs scanner.
    59  func (call *GetFollowerIDsCall) NewScanner() *UserIDsScanner {
    60  	var ctx context.Context
    61  	if call.ctx != nil {
    62  		ctx = call.ctx
    63  	} else {
    64  		ctx = context.Background()
    65  	}
    66  
    67  	c2 := &GetFollowerIDsCall{}
    68  	*c2 = *call
    69  	c2.ctx = ctx
    70  
    71  	return &UserIDsScanner{
    72  		caller: c2,
    73  		ctx:    ctx,
    74  	}
    75  }
    76  
    77  func (call *GetFollowerIDsCall) setContinuationToken(token string) {
    78  	call.continuationToken = token
    79  }
    80  
    81  type userIDsCaller interface {
    82  	Do() (*UserIDsResponse, error)
    83  	setContinuationToken(string)
    84  }
    85  
    86  // UserIDsScanner type
    87  type UserIDsScanner struct {
    88  	caller userIDsCaller
    89  	ctx    context.Context
    90  	start  int
    91  	ids    []string
    92  	next   string
    93  	called bool
    94  	done   bool
    95  	err    error
    96  }
    97  
    98  // Scan method
    99  func (s *UserIDsScanner) Scan() bool {
   100  	if s.done {
   101  		return false
   102  	}
   103  
   104  	select {
   105  	case <-s.ctx.Done():
   106  		s.err = s.ctx.Err()
   107  		s.done = true
   108  		return false
   109  	default:
   110  	}
   111  
   112  	s.start++
   113  	if len(s.ids) > 0 && len(s.ids) > s.start {
   114  		return true
   115  	}
   116  
   117  	if s.next == "" && s.called {
   118  		s.done = true
   119  		return false
   120  	}
   121  
   122  	s.start = 0
   123  	res, err := s.caller.Do()
   124  	if err != nil {
   125  		s.err = err
   126  		s.done = true
   127  		return false
   128  	}
   129  
   130  	s.called = true
   131  	s.ids = res.UserIDs
   132  	s.next = res.Next
   133  	s.caller.setContinuationToken(s.next)
   134  
   135  	return true
   136  }
   137  
   138  // ID returns member id.
   139  func (s *UserIDsScanner) ID() string {
   140  	if len(s.ids) == 0 {
   141  		return ""
   142  	}
   143  	return s.ids[s.start : s.start+1][0]
   144  }
   145  
   146  // Err returns scan error.
   147  func (s *UserIDsScanner) Err() error {
   148  	return s.err
   149  }