github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/get_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 "fmt" 20 "net/url" 21 ) 22 23 // GetGroupMemberIDs method 24 func (client *Client) GetGroupMemberIDs(groupID, continuationToken string) *GetGroupMemberIDsCall { 25 return &GetGroupMemberIDsCall{ 26 c: client, 27 groupID: groupID, 28 continuationToken: continuationToken, 29 } 30 } 31 32 // GetGroupMemberIDsCall type 33 type GetGroupMemberIDsCall struct { 34 c *Client 35 ctx context.Context 36 37 groupID string 38 continuationToken string 39 } 40 41 // WithContext method 42 func (call *GetGroupMemberIDsCall) WithContext(ctx context.Context) *GetGroupMemberIDsCall { 43 call.ctx = ctx 44 return call 45 } 46 47 // Do method 48 func (call *GetGroupMemberIDsCall) Do() (*MemberIDsResponse, error) { 49 endpoint := fmt.Sprintf(APIEndpointGetGroupMemberIDs, call.groupID) 50 var q url.Values 51 if call.continuationToken != "" { 52 q = url.Values{"start": []string{call.continuationToken}} 53 } 54 res, err := call.c.get(call.ctx, call.c.endpointBase, endpoint, q) 55 if err != nil { 56 return nil, err 57 } 58 defer closeResponse(res) 59 return decodeToMemberIDsResponse(res) 60 } 61 62 // GetRoomMemberIDs method 63 func (client *Client) GetRoomMemberIDs(roomID, continuationToken string) *GetRoomMemberIDsCall { 64 return &GetRoomMemberIDsCall{ 65 c: client, 66 roomID: roomID, 67 continuationToken: continuationToken, 68 } 69 } 70 71 // GetRoomMemberIDsCall type 72 type GetRoomMemberIDsCall struct { 73 c *Client 74 ctx context.Context 75 76 roomID string 77 continuationToken string 78 } 79 80 // WithContext method 81 func (call *GetRoomMemberIDsCall) WithContext(ctx context.Context) *GetRoomMemberIDsCall { 82 call.ctx = ctx 83 return call 84 } 85 86 // Do method 87 func (call *GetRoomMemberIDsCall) Do() (*MemberIDsResponse, error) { 88 endpoint := fmt.Sprintf(APIEndpointGetRoomMemberIDs, call.roomID) 89 var q url.Values 90 if call.continuationToken != "" { 91 q = url.Values{"start": []string{call.continuationToken}} 92 } 93 res, err := call.c.get(call.ctx, call.c.endpointBase, endpoint, q) 94 if err != nil { 95 return nil, err 96 } 97 defer closeResponse(res) 98 return decodeToMemberIDsResponse(res) 99 } 100 101 // NewScanner returns Group IDs scanner. 102 func (call *GetGroupMemberIDsCall) NewScanner() *IDsScanner { 103 var ctx context.Context 104 if call.ctx != nil { 105 ctx = call.ctx 106 } else { 107 ctx = context.Background() 108 } 109 110 c2 := &GetGroupMemberIDsCall{} 111 *c2 = *call 112 c2.ctx = ctx 113 114 return &IDsScanner{ 115 caller: c2, 116 ctx: ctx, 117 } 118 } 119 120 func (call *GetGroupMemberIDsCall) setContinuationToken(token string) { 121 call.continuationToken = token 122 } 123 124 // NewScanner returns Room IDs scanner. 125 func (call *GetRoomMemberIDsCall) NewScanner() *IDsScanner { 126 var ctx context.Context 127 if call.ctx != nil { 128 ctx = call.ctx 129 } else { 130 ctx = context.Background() 131 } 132 133 c2 := &GetRoomMemberIDsCall{} 134 *c2 = *call 135 c2.ctx = ctx 136 137 return &IDsScanner{ 138 caller: c2, 139 ctx: ctx, 140 } 141 } 142 143 func (call *GetRoomMemberIDsCall) setContinuationToken(token string) { 144 call.continuationToken = token 145 } 146 147 type memberIDsCaller interface { 148 Do() (*MemberIDsResponse, error) 149 setContinuationToken(string) 150 } 151 152 // IDsScanner type 153 type IDsScanner struct { 154 caller memberIDsCaller 155 ctx context.Context 156 start int 157 ids []string 158 next string 159 called bool 160 done bool 161 err error 162 } 163 164 // Scan method 165 func (s *IDsScanner) Scan() bool { 166 if s.done { 167 return false 168 } 169 170 select { 171 case <-s.ctx.Done(): 172 s.err = s.ctx.Err() 173 s.done = true 174 return false 175 default: 176 } 177 178 s.start++ 179 if len(s.ids) > 0 && len(s.ids) > s.start { 180 return true 181 } 182 183 if s.next == "" && s.called { 184 s.done = true 185 return false 186 } 187 188 s.start = 0 189 res, err := s.caller.Do() 190 if err != nil { 191 s.err = err 192 s.done = true 193 return false 194 } 195 196 s.called = true 197 s.ids = res.MemberIDs 198 s.next = res.Next 199 s.caller.setContinuationToken(s.next) 200 201 return true 202 } 203 204 // ID returns member id. 205 func (s *IDsScanner) ID() string { 206 if len(s.ids) == 0 { 207 return "" 208 } 209 return s.ids[s.start : s.start+1][0] 210 } 211 212 // Err returns scan error. 213 func (s *IDsScanner) Err() error { 214 return s.err 215 }