github.com/twilio/twilio-go@v1.20.1/rest/ip_messaging/v2/services_users_bindings.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Ip_messaging 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 // 27 func (c *ApiService) DeleteUserBinding(ServiceSid string, UserSid string, Sid string) error { 28 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Bindings/{Sid}" 29 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 30 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 31 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 32 33 data := url.Values{} 34 headers := make(map[string]interface{}) 35 36 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 37 if err != nil { 38 return err 39 } 40 41 defer resp.Body.Close() 42 43 return nil 44 } 45 46 // 47 func (c *ApiService) FetchUserBinding(ServiceSid string, UserSid string, Sid string) (*IpMessagingV2UserBinding, error) { 48 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Bindings/{Sid}" 49 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 50 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 51 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 57 if err != nil { 58 return nil, err 59 } 60 61 defer resp.Body.Close() 62 63 ps := &IpMessagingV2UserBinding{} 64 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 65 return nil, err 66 } 67 68 return ps, err 69 } 70 71 // Optional parameters for the method 'ListUserBinding' 72 type ListUserBindingParams struct { 73 // 74 BindingType *[]string `json:"BindingType,omitempty"` 75 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 76 PageSize *int `json:"PageSize,omitempty"` 77 // Max number of records to return. 78 Limit *int `json:"limit,omitempty"` 79 } 80 81 func (params *ListUserBindingParams) SetBindingType(BindingType []string) *ListUserBindingParams { 82 params.BindingType = &BindingType 83 return params 84 } 85 func (params *ListUserBindingParams) SetPageSize(PageSize int) *ListUserBindingParams { 86 params.PageSize = &PageSize 87 return params 88 } 89 func (params *ListUserBindingParams) SetLimit(Limit int) *ListUserBindingParams { 90 params.Limit = &Limit 91 return params 92 } 93 94 // Retrieve a single page of UserBinding records from the API. Request is executed immediately. 95 func (c *ApiService) PageUserBinding(ServiceSid string, UserSid string, params *ListUserBindingParams, pageToken, pageNumber string) (*ListUserBindingResponse, error) { 96 path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Bindings" 97 98 path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1) 99 path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1) 100 101 data := url.Values{} 102 headers := make(map[string]interface{}) 103 104 if params != nil && params.BindingType != nil { 105 for _, item := range *params.BindingType { 106 data.Add("BindingType", item) 107 } 108 } 109 if params != nil && params.PageSize != nil { 110 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 111 } 112 113 if pageToken != "" { 114 data.Set("PageToken", pageToken) 115 } 116 if pageNumber != "" { 117 data.Set("Page", pageNumber) 118 } 119 120 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 121 if err != nil { 122 return nil, err 123 } 124 125 defer resp.Body.Close() 126 127 ps := &ListUserBindingResponse{} 128 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 129 return nil, err 130 } 131 132 return ps, err 133 } 134 135 // Lists UserBinding records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 136 func (c *ApiService) ListUserBinding(ServiceSid string, UserSid string, params *ListUserBindingParams) ([]IpMessagingV2UserBinding, error) { 137 response, errors := c.StreamUserBinding(ServiceSid, UserSid, params) 138 139 records := make([]IpMessagingV2UserBinding, 0) 140 for record := range response { 141 records = append(records, record) 142 } 143 144 if err := <-errors; err != nil { 145 return nil, err 146 } 147 148 return records, nil 149 } 150 151 // Streams UserBinding records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 152 func (c *ApiService) StreamUserBinding(ServiceSid string, UserSid string, params *ListUserBindingParams) (chan IpMessagingV2UserBinding, chan error) { 153 if params == nil { 154 params = &ListUserBindingParams{} 155 } 156 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 157 158 recordChannel := make(chan IpMessagingV2UserBinding, 1) 159 errorChannel := make(chan error, 1) 160 161 response, err := c.PageUserBinding(ServiceSid, UserSid, params, "", "") 162 if err != nil { 163 errorChannel <- err 164 close(recordChannel) 165 close(errorChannel) 166 } else { 167 go c.streamUserBinding(response, params, recordChannel, errorChannel) 168 } 169 170 return recordChannel, errorChannel 171 } 172 173 func (c *ApiService) streamUserBinding(response *ListUserBindingResponse, params *ListUserBindingParams, recordChannel chan IpMessagingV2UserBinding, errorChannel chan error) { 174 curRecord := 1 175 176 for response != nil { 177 responseRecords := response.Bindings 178 for item := range responseRecords { 179 recordChannel <- responseRecords[item] 180 curRecord += 1 181 if params.Limit != nil && *params.Limit < curRecord { 182 close(recordChannel) 183 close(errorChannel) 184 return 185 } 186 } 187 188 record, err := client.GetNext(c.baseURL, response, c.getNextListUserBindingResponse) 189 if err != nil { 190 errorChannel <- err 191 break 192 } else if record == nil { 193 break 194 } 195 196 response = record.(*ListUserBindingResponse) 197 } 198 199 close(recordChannel) 200 close(errorChannel) 201 } 202 203 func (c *ApiService) getNextListUserBindingResponse(nextPageUrl string) (interface{}, error) { 204 if nextPageUrl == "" { 205 return nil, nil 206 } 207 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 208 if err != nil { 209 return nil, err 210 } 211 212 defer resp.Body.Close() 213 214 ps := &ListUserBindingResponse{} 215 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 216 return nil, err 217 } 218 return ps, nil 219 }