github.com/twilio/twilio-go@v1.20.1/rest/trusthub/v1/customer_profiles_channel_endpoint_assignments.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Trusthub 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 'CreateCustomerProfileChannelEndpointAssignment' 27 type CreateCustomerProfileChannelEndpointAssignmentParams struct { 28 // The type of channel endpoint. eg: phone-number 29 ChannelEndpointType *string `json:"ChannelEndpointType,omitempty"` 30 // The SID of an channel endpoint 31 ChannelEndpointSid *string `json:"ChannelEndpointSid,omitempty"` 32 } 33 34 func (params *CreateCustomerProfileChannelEndpointAssignmentParams) SetChannelEndpointType(ChannelEndpointType string) *CreateCustomerProfileChannelEndpointAssignmentParams { 35 params.ChannelEndpointType = &ChannelEndpointType 36 return params 37 } 38 func (params *CreateCustomerProfileChannelEndpointAssignmentParams) SetChannelEndpointSid(ChannelEndpointSid string) *CreateCustomerProfileChannelEndpointAssignmentParams { 39 params.ChannelEndpointSid = &ChannelEndpointSid 40 return params 41 } 42 43 // Create a new Assigned Item. 44 func (c *ApiService) CreateCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, params *CreateCustomerProfileChannelEndpointAssignmentParams) (*TrusthubV1CustomerProfileChannelEndpointAssignment, error) { 45 path := "/v1/CustomerProfiles/{CustomerProfileSid}/ChannelEndpointAssignments" 46 path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1) 47 48 data := url.Values{} 49 headers := make(map[string]interface{}) 50 51 if params != nil && params.ChannelEndpointType != nil { 52 data.Set("ChannelEndpointType", *params.ChannelEndpointType) 53 } 54 if params != nil && params.ChannelEndpointSid != nil { 55 data.Set("ChannelEndpointSid", *params.ChannelEndpointSid) 56 } 57 58 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 59 if err != nil { 60 return nil, err 61 } 62 63 defer resp.Body.Close() 64 65 ps := &TrusthubV1CustomerProfileChannelEndpointAssignment{} 66 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 67 return nil, err 68 } 69 70 return ps, err 71 } 72 73 // Remove an Assignment Item Instance. 74 func (c *ApiService) DeleteCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, Sid string) error { 75 path := "/v1/CustomerProfiles/{CustomerProfileSid}/ChannelEndpointAssignments/{Sid}" 76 path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1) 77 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 78 79 data := url.Values{} 80 headers := make(map[string]interface{}) 81 82 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 83 if err != nil { 84 return err 85 } 86 87 defer resp.Body.Close() 88 89 return nil 90 } 91 92 // Fetch specific Assigned Item Instance. 93 func (c *ApiService) FetchCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, Sid string) (*TrusthubV1CustomerProfileChannelEndpointAssignment, error) { 94 path := "/v1/CustomerProfiles/{CustomerProfileSid}/ChannelEndpointAssignments/{Sid}" 95 path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1) 96 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 97 98 data := url.Values{} 99 headers := make(map[string]interface{}) 100 101 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 102 if err != nil { 103 return nil, err 104 } 105 106 defer resp.Body.Close() 107 108 ps := &TrusthubV1CustomerProfileChannelEndpointAssignment{} 109 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 110 return nil, err 111 } 112 113 return ps, err 114 } 115 116 // Optional parameters for the method 'ListCustomerProfileChannelEndpointAssignment' 117 type ListCustomerProfileChannelEndpointAssignmentParams struct { 118 // The SID of an channel endpoint 119 ChannelEndpointSid *string `json:"ChannelEndpointSid,omitempty"` 120 // comma separated list of channel endpoint sids 121 ChannelEndpointSids *string `json:"ChannelEndpointSids,omitempty"` 122 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 123 PageSize *int `json:"PageSize,omitempty"` 124 // Max number of records to return. 125 Limit *int `json:"limit,omitempty"` 126 } 127 128 func (params *ListCustomerProfileChannelEndpointAssignmentParams) SetChannelEndpointSid(ChannelEndpointSid string) *ListCustomerProfileChannelEndpointAssignmentParams { 129 params.ChannelEndpointSid = &ChannelEndpointSid 130 return params 131 } 132 func (params *ListCustomerProfileChannelEndpointAssignmentParams) SetChannelEndpointSids(ChannelEndpointSids string) *ListCustomerProfileChannelEndpointAssignmentParams { 133 params.ChannelEndpointSids = &ChannelEndpointSids 134 return params 135 } 136 func (params *ListCustomerProfileChannelEndpointAssignmentParams) SetPageSize(PageSize int) *ListCustomerProfileChannelEndpointAssignmentParams { 137 params.PageSize = &PageSize 138 return params 139 } 140 func (params *ListCustomerProfileChannelEndpointAssignmentParams) SetLimit(Limit int) *ListCustomerProfileChannelEndpointAssignmentParams { 141 params.Limit = &Limit 142 return params 143 } 144 145 // Retrieve a single page of CustomerProfileChannelEndpointAssignment records from the API. Request is executed immediately. 146 func (c *ApiService) PageCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, params *ListCustomerProfileChannelEndpointAssignmentParams, pageToken, pageNumber string) (*ListCustomerProfileChannelEndpointAssignmentResponse, error) { 147 path := "/v1/CustomerProfiles/{CustomerProfileSid}/ChannelEndpointAssignments" 148 149 path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1) 150 151 data := url.Values{} 152 headers := make(map[string]interface{}) 153 154 if params != nil && params.ChannelEndpointSid != nil { 155 data.Set("ChannelEndpointSid", *params.ChannelEndpointSid) 156 } 157 if params != nil && params.ChannelEndpointSids != nil { 158 data.Set("ChannelEndpointSids", *params.ChannelEndpointSids) 159 } 160 if params != nil && params.PageSize != nil { 161 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 162 } 163 164 if pageToken != "" { 165 data.Set("PageToken", pageToken) 166 } 167 if pageNumber != "" { 168 data.Set("Page", pageNumber) 169 } 170 171 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 172 if err != nil { 173 return nil, err 174 } 175 176 defer resp.Body.Close() 177 178 ps := &ListCustomerProfileChannelEndpointAssignmentResponse{} 179 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 180 return nil, err 181 } 182 183 return ps, err 184 } 185 186 // Lists CustomerProfileChannelEndpointAssignment records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 187 func (c *ApiService) ListCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, params *ListCustomerProfileChannelEndpointAssignmentParams) ([]TrusthubV1CustomerProfileChannelEndpointAssignment, error) { 188 response, errors := c.StreamCustomerProfileChannelEndpointAssignment(CustomerProfileSid, params) 189 190 records := make([]TrusthubV1CustomerProfileChannelEndpointAssignment, 0) 191 for record := range response { 192 records = append(records, record) 193 } 194 195 if err := <-errors; err != nil { 196 return nil, err 197 } 198 199 return records, nil 200 } 201 202 // Streams CustomerProfileChannelEndpointAssignment records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 203 func (c *ApiService) StreamCustomerProfileChannelEndpointAssignment(CustomerProfileSid string, params *ListCustomerProfileChannelEndpointAssignmentParams) (chan TrusthubV1CustomerProfileChannelEndpointAssignment, chan error) { 204 if params == nil { 205 params = &ListCustomerProfileChannelEndpointAssignmentParams{} 206 } 207 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 208 209 recordChannel := make(chan TrusthubV1CustomerProfileChannelEndpointAssignment, 1) 210 errorChannel := make(chan error, 1) 211 212 response, err := c.PageCustomerProfileChannelEndpointAssignment(CustomerProfileSid, params, "", "") 213 if err != nil { 214 errorChannel <- err 215 close(recordChannel) 216 close(errorChannel) 217 } else { 218 go c.streamCustomerProfileChannelEndpointAssignment(response, params, recordChannel, errorChannel) 219 } 220 221 return recordChannel, errorChannel 222 } 223 224 func (c *ApiService) streamCustomerProfileChannelEndpointAssignment(response *ListCustomerProfileChannelEndpointAssignmentResponse, params *ListCustomerProfileChannelEndpointAssignmentParams, recordChannel chan TrusthubV1CustomerProfileChannelEndpointAssignment, errorChannel chan error) { 225 curRecord := 1 226 227 for response != nil { 228 responseRecords := response.Results 229 for item := range responseRecords { 230 recordChannel <- responseRecords[item] 231 curRecord += 1 232 if params.Limit != nil && *params.Limit < curRecord { 233 close(recordChannel) 234 close(errorChannel) 235 return 236 } 237 } 238 239 record, err := client.GetNext(c.baseURL, response, c.getNextListCustomerProfileChannelEndpointAssignmentResponse) 240 if err != nil { 241 errorChannel <- err 242 break 243 } else if record == nil { 244 break 245 } 246 247 response = record.(*ListCustomerProfileChannelEndpointAssignmentResponse) 248 } 249 250 close(recordChannel) 251 close(errorChannel) 252 } 253 254 func (c *ApiService) getNextListCustomerProfileChannelEndpointAssignmentResponse(nextPageUrl string) (interface{}, error) { 255 if nextPageUrl == "" { 256 return nil, nil 257 } 258 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 259 if err != nil { 260 return nil, err 261 } 262 263 defer resp.Body.Close() 264 265 ps := &ListCustomerProfileChannelEndpointAssignmentResponse{} 266 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 267 return nil, err 268 } 269 return ps, nil 270 }