github.com/twilio/twilio-go@v1.20.1/rest/numbers/v2/regulatory_compliance_end_users.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Numbers 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 'CreateEndUser' 27 type CreateEndUserParams struct { 28 // The string that you assigned to describe the resource. 29 FriendlyName *string `json:"FriendlyName,omitempty"` 30 // 31 Type *string `json:"Type,omitempty"` 32 // The set of parameters that are the attributes of the End User resource which are derived End User Types. 33 Attributes *interface{} `json:"Attributes,omitempty"` 34 } 35 36 func (params *CreateEndUserParams) SetFriendlyName(FriendlyName string) *CreateEndUserParams { 37 params.FriendlyName = &FriendlyName 38 return params 39 } 40 func (params *CreateEndUserParams) SetType(Type string) *CreateEndUserParams { 41 params.Type = &Type 42 return params 43 } 44 func (params *CreateEndUserParams) SetAttributes(Attributes interface{}) *CreateEndUserParams { 45 params.Attributes = &Attributes 46 return params 47 } 48 49 // Create a new End User. 50 func (c *ApiService) CreateEndUser(params *CreateEndUserParams) (*NumbersV2EndUser, error) { 51 path := "/v2/RegulatoryCompliance/EndUsers" 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 if params != nil && params.FriendlyName != nil { 57 data.Set("FriendlyName", *params.FriendlyName) 58 } 59 if params != nil && params.Type != nil { 60 data.Set("Type", *params.Type) 61 } 62 if params != nil && params.Attributes != nil { 63 v, err := json.Marshal(params.Attributes) 64 65 if err != nil { 66 return nil, err 67 } 68 69 data.Set("Attributes", string(v)) 70 } 71 72 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 73 if err != nil { 74 return nil, err 75 } 76 77 defer resp.Body.Close() 78 79 ps := &NumbersV2EndUser{} 80 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 81 return nil, err 82 } 83 84 return ps, err 85 } 86 87 // Delete a specific End User. 88 func (c *ApiService) DeleteEndUser(Sid string) error { 89 path := "/v2/RegulatoryCompliance/EndUsers/{Sid}" 90 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 91 92 data := url.Values{} 93 headers := make(map[string]interface{}) 94 95 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 96 if err != nil { 97 return err 98 } 99 100 defer resp.Body.Close() 101 102 return nil 103 } 104 105 // Fetch specific End User Instance. 106 func (c *ApiService) FetchEndUser(Sid string) (*NumbersV2EndUser, error) { 107 path := "/v2/RegulatoryCompliance/EndUsers/{Sid}" 108 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 109 110 data := url.Values{} 111 headers := make(map[string]interface{}) 112 113 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 114 if err != nil { 115 return nil, err 116 } 117 118 defer resp.Body.Close() 119 120 ps := &NumbersV2EndUser{} 121 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 122 return nil, err 123 } 124 125 return ps, err 126 } 127 128 // Optional parameters for the method 'ListEndUser' 129 type ListEndUserParams struct { 130 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 131 PageSize *int `json:"PageSize,omitempty"` 132 // Max number of records to return. 133 Limit *int `json:"limit,omitempty"` 134 } 135 136 func (params *ListEndUserParams) SetPageSize(PageSize int) *ListEndUserParams { 137 params.PageSize = &PageSize 138 return params 139 } 140 func (params *ListEndUserParams) SetLimit(Limit int) *ListEndUserParams { 141 params.Limit = &Limit 142 return params 143 } 144 145 // Retrieve a single page of EndUser records from the API. Request is executed immediately. 146 func (c *ApiService) PageEndUser(params *ListEndUserParams, pageToken, pageNumber string) (*ListEndUserResponse, error) { 147 path := "/v2/RegulatoryCompliance/EndUsers" 148 149 data := url.Values{} 150 headers := make(map[string]interface{}) 151 152 if params != nil && params.PageSize != nil { 153 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 154 } 155 156 if pageToken != "" { 157 data.Set("PageToken", pageToken) 158 } 159 if pageNumber != "" { 160 data.Set("Page", pageNumber) 161 } 162 163 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 164 if err != nil { 165 return nil, err 166 } 167 168 defer resp.Body.Close() 169 170 ps := &ListEndUserResponse{} 171 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 172 return nil, err 173 } 174 175 return ps, err 176 } 177 178 // Lists EndUser records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 179 func (c *ApiService) ListEndUser(params *ListEndUserParams) ([]NumbersV2EndUser, error) { 180 response, errors := c.StreamEndUser(params) 181 182 records := make([]NumbersV2EndUser, 0) 183 for record := range response { 184 records = append(records, record) 185 } 186 187 if err := <-errors; err != nil { 188 return nil, err 189 } 190 191 return records, nil 192 } 193 194 // Streams EndUser records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 195 func (c *ApiService) StreamEndUser(params *ListEndUserParams) (chan NumbersV2EndUser, chan error) { 196 if params == nil { 197 params = &ListEndUserParams{} 198 } 199 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 200 201 recordChannel := make(chan NumbersV2EndUser, 1) 202 errorChannel := make(chan error, 1) 203 204 response, err := c.PageEndUser(params, "", "") 205 if err != nil { 206 errorChannel <- err 207 close(recordChannel) 208 close(errorChannel) 209 } else { 210 go c.streamEndUser(response, params, recordChannel, errorChannel) 211 } 212 213 return recordChannel, errorChannel 214 } 215 216 func (c *ApiService) streamEndUser(response *ListEndUserResponse, params *ListEndUserParams, recordChannel chan NumbersV2EndUser, errorChannel chan error) { 217 curRecord := 1 218 219 for response != nil { 220 responseRecords := response.Results 221 for item := range responseRecords { 222 recordChannel <- responseRecords[item] 223 curRecord += 1 224 if params.Limit != nil && *params.Limit < curRecord { 225 close(recordChannel) 226 close(errorChannel) 227 return 228 } 229 } 230 231 record, err := client.GetNext(c.baseURL, response, c.getNextListEndUserResponse) 232 if err != nil { 233 errorChannel <- err 234 break 235 } else if record == nil { 236 break 237 } 238 239 response = record.(*ListEndUserResponse) 240 } 241 242 close(recordChannel) 243 close(errorChannel) 244 } 245 246 func (c *ApiService) getNextListEndUserResponse(nextPageUrl string) (interface{}, error) { 247 if nextPageUrl == "" { 248 return nil, nil 249 } 250 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 251 if err != nil { 252 return nil, err 253 } 254 255 defer resp.Body.Close() 256 257 ps := &ListEndUserResponse{} 258 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 259 return nil, err 260 } 261 return ps, nil 262 } 263 264 // Optional parameters for the method 'UpdateEndUser' 265 type UpdateEndUserParams struct { 266 // The string that you assigned to describe the resource. 267 FriendlyName *string `json:"FriendlyName,omitempty"` 268 // The set of parameters that are the attributes of the End User resource which are derived End User Types. 269 Attributes *interface{} `json:"Attributes,omitempty"` 270 } 271 272 func (params *UpdateEndUserParams) SetFriendlyName(FriendlyName string) *UpdateEndUserParams { 273 params.FriendlyName = &FriendlyName 274 return params 275 } 276 func (params *UpdateEndUserParams) SetAttributes(Attributes interface{}) *UpdateEndUserParams { 277 params.Attributes = &Attributes 278 return params 279 } 280 281 // Update an existing End User. 282 func (c *ApiService) UpdateEndUser(Sid string, params *UpdateEndUserParams) (*NumbersV2EndUser, error) { 283 path := "/v2/RegulatoryCompliance/EndUsers/{Sid}" 284 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 285 286 data := url.Values{} 287 headers := make(map[string]interface{}) 288 289 if params != nil && params.FriendlyName != nil { 290 data.Set("FriendlyName", *params.FriendlyName) 291 } 292 if params != nil && params.Attributes != nil { 293 v, err := json.Marshal(params.Attributes) 294 295 if err != nil { 296 return nil, err 297 } 298 299 data.Set("Attributes", string(v)) 300 } 301 302 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 303 if err != nil { 304 return nil, err 305 } 306 307 defer resp.Body.Close() 308 309 ps := &NumbersV2EndUser{} 310 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 311 return nil, err 312 } 313 314 return ps, err 315 }