github.com/twilio/twilio-go@v1.20.1/rest/chat/v1/services_users_channels.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Chat 8 * This is the public Twilio REST API. 9 * 10 * NOTE: This class is auto generated by OpenAPI Generator. 11 * https://openapi-generator.tech 12 * Do not edit the class manually. 13 */ 14 15 package openapi 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "net/url" 21 "strings" 22 23 "github.com/twilio/twilio-go/client" 24 ) 25 26 // Optional parameters for the method 'ListUserChannel' 27 type ListUserChannelParams struct { 28 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 29 PageSize *int `json:"PageSize,omitempty"` 30 // Max number of records to return. 31 Limit *int `json:"limit,omitempty"` 32 } 33 34 func (params *ListUserChannelParams) SetPageSize(PageSize int) *ListUserChannelParams { 35 params.PageSize = &PageSize 36 return params 37 } 38 func (params *ListUserChannelParams) SetLimit(Limit int) *ListUserChannelParams { 39 params.Limit = &Limit 40 return params 41 } 42 43 // Retrieve a single page of UserChannel records from the API. Request is executed immediately. 44 func (c *ApiService) PageUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams, pageToken, pageNumber string) (*ListUserChannelResponse, error) { 45 path := "/v1/Services/{ServiceSid}/Users/{UserSid}/Channels" 46 47 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 48 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 49 50 data := url.Values{} 51 headers := make(map[string]interface{}) 52 53 if params != nil && params.PageSize != nil { 54 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 55 } 56 57 if pageToken != "" { 58 data.Set("PageToken", pageToken) 59 } 60 if pageNumber != "" { 61 data.Set("Page", pageNumber) 62 } 63 64 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 65 if err != nil { 66 return nil, err 67 } 68 69 defer resp.Body.Close() 70 71 ps := &ListUserChannelResponse{} 72 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 73 return nil, err 74 } 75 76 return ps, err 77 } 78 79 // Lists UserChannel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 80 func (c *ApiService) ListUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) ([]ChatV1UserChannel, error) { 81 response, errors := c.StreamUserChannel(ServiceSid, UserSid, params) 82 83 records := make([]ChatV1UserChannel, 0) 84 for record := range response { 85 records = append(records, record) 86 } 87 88 if err := <-errors; err != nil { 89 return nil, err 90 } 91 92 return records, nil 93 } 94 95 // Streams UserChannel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 96 func (c *ApiService) StreamUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) (chan ChatV1UserChannel, chan error) { 97 if params == nil { 98 params = &ListUserChannelParams{} 99 } 100 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 101 102 recordChannel := make(chan ChatV1UserChannel, 1) 103 errorChannel := make(chan error, 1) 104 105 response, err := c.PageUserChannel(ServiceSid, UserSid, params, "", "") 106 if err != nil { 107 errorChannel <- err 108 close(recordChannel) 109 close(errorChannel) 110 } else { 111 go c.streamUserChannel(response, params, recordChannel, errorChannel) 112 } 113 114 return recordChannel, errorChannel 115 } 116 117 func (c *ApiService) streamUserChannel(response *ListUserChannelResponse, params *ListUserChannelParams, recordChannel chan ChatV1UserChannel, errorChannel chan error) { 118 curRecord := 1 119 120 for response != nil { 121 responseRecords := response.Channels 122 for item := range responseRecords { 123 recordChannel <- responseRecords[item] 124 curRecord += 1 125 if params.Limit != nil && *params.Limit < curRecord { 126 close(recordChannel) 127 close(errorChannel) 128 return 129 } 130 } 131 132 record, err := client.GetNext(c.baseURL, response, c.getNextListUserChannelResponse) 133 if err != nil { 134 errorChannel <- err 135 break 136 } else if record == nil { 137 break 138 } 139 140 response = record.(*ListUserChannelResponse) 141 } 142 143 close(recordChannel) 144 close(errorChannel) 145 } 146 147 func (c *ApiService) getNextListUserChannelResponse(nextPageUrl string) (interface{}, error) { 148 if nextPageUrl == "" { 149 return nil, nil 150 } 151 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 152 if err != nil { 153 return nil, err 154 } 155 156 defer resp.Body.Close() 157 158 ps := &ListUserChannelResponse{} 159 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 160 return nil, err 161 } 162 return ps, nil 163 }