github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_incoming_phone_numbers_assigned_add_ons.go (about) 1 /* 2 * This code was generated by 3 * ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ 4 * | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ 5 * | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ 6 * 7 * Twilio - Api 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 'CreateIncomingPhoneNumberAssignedAddOn' 27 type CreateIncomingPhoneNumberAssignedAddOnParams struct { 28 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource. 29 PathAccountSid *string `json:"PathAccountSid,omitempty"` 30 // The SID that identifies the Add-on installation. 31 InstalledAddOnSid *string `json:"InstalledAddOnSid,omitempty"` 32 } 33 34 func (params *CreateIncomingPhoneNumberAssignedAddOnParams) SetPathAccountSid(PathAccountSid string) *CreateIncomingPhoneNumberAssignedAddOnParams { 35 params.PathAccountSid = &PathAccountSid 36 return params 37 } 38 func (params *CreateIncomingPhoneNumberAssignedAddOnParams) SetInstalledAddOnSid(InstalledAddOnSid string) *CreateIncomingPhoneNumberAssignedAddOnParams { 39 params.InstalledAddOnSid = &InstalledAddOnSid 40 return params 41 } 42 43 // Assign an Add-on installation to the Number specified. 44 func (c *ApiService) CreateIncomingPhoneNumberAssignedAddOn(ResourceSid string, params *CreateIncomingPhoneNumberAssignedAddOnParams) (*ApiV2010IncomingPhoneNumberAssignedAddOn, error) { 45 path := "/2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers/{ResourceSid}/AssignedAddOns.json" 46 if params != nil && params.PathAccountSid != nil { 47 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 48 } else { 49 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 50 } 51 path = strings.Replace(path, "{"+"ResourceSid"+"}", ResourceSid, -1) 52 53 data := url.Values{} 54 headers := make(map[string]interface{}) 55 56 if params != nil && params.InstalledAddOnSid != nil { 57 data.Set("InstalledAddOnSid", *params.InstalledAddOnSid) 58 } 59 60 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 61 if err != nil { 62 return nil, err 63 } 64 65 defer resp.Body.Close() 66 67 ps := &ApiV2010IncomingPhoneNumberAssignedAddOn{} 68 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 69 return nil, err 70 } 71 72 return ps, err 73 } 74 75 // Optional parameters for the method 'DeleteIncomingPhoneNumberAssignedAddOn' 76 type DeleteIncomingPhoneNumberAssignedAddOnParams struct { 77 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the resources to delete. 78 PathAccountSid *string `json:"PathAccountSid,omitempty"` 79 } 80 81 func (params *DeleteIncomingPhoneNumberAssignedAddOnParams) SetPathAccountSid(PathAccountSid string) *DeleteIncomingPhoneNumberAssignedAddOnParams { 82 params.PathAccountSid = &PathAccountSid 83 return params 84 } 85 86 // Remove the assignment of an Add-on installation from the Number specified. 87 func (c *ApiService) DeleteIncomingPhoneNumberAssignedAddOn(ResourceSid string, Sid string, params *DeleteIncomingPhoneNumberAssignedAddOnParams) error { 88 path := "/2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers/{ResourceSid}/AssignedAddOns/{Sid}.json" 89 if params != nil && params.PathAccountSid != nil { 90 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 91 } else { 92 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 93 } 94 path = strings.Replace(path, "{"+"ResourceSid"+"}", ResourceSid, -1) 95 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 96 97 data := url.Values{} 98 headers := make(map[string]interface{}) 99 100 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 101 if err != nil { 102 return err 103 } 104 105 defer resp.Body.Close() 106 107 return nil 108 } 109 110 // Optional parameters for the method 'FetchIncomingPhoneNumberAssignedAddOn' 111 type FetchIncomingPhoneNumberAssignedAddOnParams struct { 112 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the resource to fetch. 113 PathAccountSid *string `json:"PathAccountSid,omitempty"` 114 } 115 116 func (params *FetchIncomingPhoneNumberAssignedAddOnParams) SetPathAccountSid(PathAccountSid string) *FetchIncomingPhoneNumberAssignedAddOnParams { 117 params.PathAccountSid = &PathAccountSid 118 return params 119 } 120 121 // Fetch an instance of an Add-on installation currently assigned to this Number. 122 func (c *ApiService) FetchIncomingPhoneNumberAssignedAddOn(ResourceSid string, Sid string, params *FetchIncomingPhoneNumberAssignedAddOnParams) (*ApiV2010IncomingPhoneNumberAssignedAddOn, error) { 123 path := "/2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers/{ResourceSid}/AssignedAddOns/{Sid}.json" 124 if params != nil && params.PathAccountSid != nil { 125 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 126 } else { 127 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 128 } 129 path = strings.Replace(path, "{"+"ResourceSid"+"}", ResourceSid, -1) 130 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 131 132 data := url.Values{} 133 headers := make(map[string]interface{}) 134 135 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 136 if err != nil { 137 return nil, err 138 } 139 140 defer resp.Body.Close() 141 142 ps := &ApiV2010IncomingPhoneNumberAssignedAddOn{} 143 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 144 return nil, err 145 } 146 147 return ps, err 148 } 149 150 // Optional parameters for the method 'ListIncomingPhoneNumberAssignedAddOn' 151 type ListIncomingPhoneNumberAssignedAddOnParams struct { 152 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the resources to read. 153 PathAccountSid *string `json:"PathAccountSid,omitempty"` 154 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 155 PageSize *int `json:"PageSize,omitempty"` 156 // Max number of records to return. 157 Limit *int `json:"limit,omitempty"` 158 } 159 160 func (params *ListIncomingPhoneNumberAssignedAddOnParams) SetPathAccountSid(PathAccountSid string) *ListIncomingPhoneNumberAssignedAddOnParams { 161 params.PathAccountSid = &PathAccountSid 162 return params 163 } 164 func (params *ListIncomingPhoneNumberAssignedAddOnParams) SetPageSize(PageSize int) *ListIncomingPhoneNumberAssignedAddOnParams { 165 params.PageSize = &PageSize 166 return params 167 } 168 func (params *ListIncomingPhoneNumberAssignedAddOnParams) SetLimit(Limit int) *ListIncomingPhoneNumberAssignedAddOnParams { 169 params.Limit = &Limit 170 return params 171 } 172 173 // Retrieve a single page of IncomingPhoneNumberAssignedAddOn records from the API. Request is executed immediately. 174 func (c *ApiService) PageIncomingPhoneNumberAssignedAddOn(ResourceSid string, params *ListIncomingPhoneNumberAssignedAddOnParams, pageToken, pageNumber string) (*ListIncomingPhoneNumberAssignedAddOnResponse, error) { 175 path := "/2010-04-01/Accounts/{AccountSid}/IncomingPhoneNumbers/{ResourceSid}/AssignedAddOns.json" 176 177 if params != nil && params.PathAccountSid != nil { 178 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 179 } else { 180 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 181 } 182 path = strings.Replace(path, "{"+"ResourceSid"+"}", ResourceSid, -1) 183 184 data := url.Values{} 185 headers := make(map[string]interface{}) 186 187 if params != nil && params.PageSize != nil { 188 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 189 } 190 191 if pageToken != "" { 192 data.Set("PageToken", pageToken) 193 } 194 if pageNumber != "" { 195 data.Set("Page", pageNumber) 196 } 197 198 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 199 if err != nil { 200 return nil, err 201 } 202 203 defer resp.Body.Close() 204 205 ps := &ListIncomingPhoneNumberAssignedAddOnResponse{} 206 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 207 return nil, err 208 } 209 210 return ps, err 211 } 212 213 // Lists IncomingPhoneNumberAssignedAddOn records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 214 func (c *ApiService) ListIncomingPhoneNumberAssignedAddOn(ResourceSid string, params *ListIncomingPhoneNumberAssignedAddOnParams) ([]ApiV2010IncomingPhoneNumberAssignedAddOn, error) { 215 response, errors := c.StreamIncomingPhoneNumberAssignedAddOn(ResourceSid, params) 216 217 records := make([]ApiV2010IncomingPhoneNumberAssignedAddOn, 0) 218 for record := range response { 219 records = append(records, record) 220 } 221 222 if err := <-errors; err != nil { 223 return nil, err 224 } 225 226 return records, nil 227 } 228 229 // Streams IncomingPhoneNumberAssignedAddOn records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 230 func (c *ApiService) StreamIncomingPhoneNumberAssignedAddOn(ResourceSid string, params *ListIncomingPhoneNumberAssignedAddOnParams) (chan ApiV2010IncomingPhoneNumberAssignedAddOn, chan error) { 231 if params == nil { 232 params = &ListIncomingPhoneNumberAssignedAddOnParams{} 233 } 234 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 235 236 recordChannel := make(chan ApiV2010IncomingPhoneNumberAssignedAddOn, 1) 237 errorChannel := make(chan error, 1) 238 239 response, err := c.PageIncomingPhoneNumberAssignedAddOn(ResourceSid, params, "", "") 240 if err != nil { 241 errorChannel <- err 242 close(recordChannel) 243 close(errorChannel) 244 } else { 245 go c.streamIncomingPhoneNumberAssignedAddOn(response, params, recordChannel, errorChannel) 246 } 247 248 return recordChannel, errorChannel 249 } 250 251 func (c *ApiService) streamIncomingPhoneNumberAssignedAddOn(response *ListIncomingPhoneNumberAssignedAddOnResponse, params *ListIncomingPhoneNumberAssignedAddOnParams, recordChannel chan ApiV2010IncomingPhoneNumberAssignedAddOn, errorChannel chan error) { 252 curRecord := 1 253 254 for response != nil { 255 responseRecords := response.AssignedAddOns 256 for item := range responseRecords { 257 recordChannel <- responseRecords[item] 258 curRecord += 1 259 if params.Limit != nil && *params.Limit < curRecord { 260 close(recordChannel) 261 close(errorChannel) 262 return 263 } 264 } 265 266 record, err := client.GetNext(c.baseURL, response, c.getNextListIncomingPhoneNumberAssignedAddOnResponse) 267 if err != nil { 268 errorChannel <- err 269 break 270 } else if record == nil { 271 break 272 } 273 274 response = record.(*ListIncomingPhoneNumberAssignedAddOnResponse) 275 } 276 277 close(recordChannel) 278 close(errorChannel) 279 } 280 281 func (c *ApiService) getNextListIncomingPhoneNumberAssignedAddOnResponse(nextPageUrl string) (interface{}, error) { 282 if nextPageUrl == "" { 283 return nil, nil 284 } 285 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 286 if err != nil { 287 return nil, err 288 } 289 290 defer resp.Body.Close() 291 292 ps := &ListIncomingPhoneNumberAssignedAddOnResponse{} 293 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 294 return nil, err 295 } 296 return ps, nil 297 }