github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_addresses.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 'CreateAddress' 27 type CreateAddressParams struct { 28 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will be responsible for the new Address resource. 29 PathAccountSid *string `json:"PathAccountSid,omitempty"` 30 // The name to associate with the new address. 31 CustomerName *string `json:"CustomerName,omitempty"` 32 // The number and street address of the new address. 33 Street *string `json:"Street,omitempty"` 34 // The city of the new address. 35 City *string `json:"City,omitempty"` 36 // The state or region of the new address. 37 Region *string `json:"Region,omitempty"` 38 // The postal code of the new address. 39 PostalCode *string `json:"PostalCode,omitempty"` 40 // The ISO country code of the new address. 41 IsoCountry *string `json:"IsoCountry,omitempty"` 42 // A descriptive string that you create to describe the new address. It can be up to 64 characters long. 43 FriendlyName *string `json:"FriendlyName,omitempty"` 44 // Whether to enable emergency calling on the new address. Can be: `true` or `false`. 45 EmergencyEnabled *bool `json:"EmergencyEnabled,omitempty"` 46 // Whether we should automatically correct the address. Can be: `true` or `false` and the default is `true`. If empty or `true`, we will correct the address you provide if necessary. If `false`, we won't alter the address you provide. 47 AutoCorrectAddress *bool `json:"AutoCorrectAddress,omitempty"` 48 // The additional number and street address of the address. 49 StreetSecondary *string `json:"StreetSecondary,omitempty"` 50 } 51 52 func (params *CreateAddressParams) SetPathAccountSid(PathAccountSid string) *CreateAddressParams { 53 params.PathAccountSid = &PathAccountSid 54 return params 55 } 56 func (params *CreateAddressParams) SetCustomerName(CustomerName string) *CreateAddressParams { 57 params.CustomerName = &CustomerName 58 return params 59 } 60 func (params *CreateAddressParams) SetStreet(Street string) *CreateAddressParams { 61 params.Street = &Street 62 return params 63 } 64 func (params *CreateAddressParams) SetCity(City string) *CreateAddressParams { 65 params.City = &City 66 return params 67 } 68 func (params *CreateAddressParams) SetRegion(Region string) *CreateAddressParams { 69 params.Region = &Region 70 return params 71 } 72 func (params *CreateAddressParams) SetPostalCode(PostalCode string) *CreateAddressParams { 73 params.PostalCode = &PostalCode 74 return params 75 } 76 func (params *CreateAddressParams) SetIsoCountry(IsoCountry string) *CreateAddressParams { 77 params.IsoCountry = &IsoCountry 78 return params 79 } 80 func (params *CreateAddressParams) SetFriendlyName(FriendlyName string) *CreateAddressParams { 81 params.FriendlyName = &FriendlyName 82 return params 83 } 84 func (params *CreateAddressParams) SetEmergencyEnabled(EmergencyEnabled bool) *CreateAddressParams { 85 params.EmergencyEnabled = &EmergencyEnabled 86 return params 87 } 88 func (params *CreateAddressParams) SetAutoCorrectAddress(AutoCorrectAddress bool) *CreateAddressParams { 89 params.AutoCorrectAddress = &AutoCorrectAddress 90 return params 91 } 92 func (params *CreateAddressParams) SetStreetSecondary(StreetSecondary string) *CreateAddressParams { 93 params.StreetSecondary = &StreetSecondary 94 return params 95 } 96 97 // 98 func (c *ApiService) CreateAddress(params *CreateAddressParams) (*ApiV2010Address, error) { 99 path := "/2010-04-01/Accounts/{AccountSid}/Addresses.json" 100 if params != nil && params.PathAccountSid != nil { 101 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 102 } else { 103 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 104 } 105 106 data := url.Values{} 107 headers := make(map[string]interface{}) 108 109 if params != nil && params.CustomerName != nil { 110 data.Set("CustomerName", *params.CustomerName) 111 } 112 if params != nil && params.Street != nil { 113 data.Set("Street", *params.Street) 114 } 115 if params != nil && params.City != nil { 116 data.Set("City", *params.City) 117 } 118 if params != nil && params.Region != nil { 119 data.Set("Region", *params.Region) 120 } 121 if params != nil && params.PostalCode != nil { 122 data.Set("PostalCode", *params.PostalCode) 123 } 124 if params != nil && params.IsoCountry != nil { 125 data.Set("IsoCountry", *params.IsoCountry) 126 } 127 if params != nil && params.FriendlyName != nil { 128 data.Set("FriendlyName", *params.FriendlyName) 129 } 130 if params != nil && params.EmergencyEnabled != nil { 131 data.Set("EmergencyEnabled", fmt.Sprint(*params.EmergencyEnabled)) 132 } 133 if params != nil && params.AutoCorrectAddress != nil { 134 data.Set("AutoCorrectAddress", fmt.Sprint(*params.AutoCorrectAddress)) 135 } 136 if params != nil && params.StreetSecondary != nil { 137 data.Set("StreetSecondary", *params.StreetSecondary) 138 } 139 140 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 141 if err != nil { 142 return nil, err 143 } 144 145 defer resp.Body.Close() 146 147 ps := &ApiV2010Address{} 148 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 149 return nil, err 150 } 151 152 return ps, err 153 } 154 155 // Optional parameters for the method 'DeleteAddress' 156 type DeleteAddressParams struct { 157 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is responsible for the Address resource to delete. 158 PathAccountSid *string `json:"PathAccountSid,omitempty"` 159 } 160 161 func (params *DeleteAddressParams) SetPathAccountSid(PathAccountSid string) *DeleteAddressParams { 162 params.PathAccountSid = &PathAccountSid 163 return params 164 } 165 166 // 167 func (c *ApiService) DeleteAddress(Sid string, params *DeleteAddressParams) error { 168 path := "/2010-04-01/Accounts/{AccountSid}/Addresses/{Sid}.json" 169 if params != nil && params.PathAccountSid != nil { 170 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 171 } else { 172 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 173 } 174 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 175 176 data := url.Values{} 177 headers := make(map[string]interface{}) 178 179 resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers) 180 if err != nil { 181 return err 182 } 183 184 defer resp.Body.Close() 185 186 return nil 187 } 188 189 // Optional parameters for the method 'FetchAddress' 190 type FetchAddressParams struct { 191 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is responsible for the Address resource to fetch. 192 PathAccountSid *string `json:"PathAccountSid,omitempty"` 193 } 194 195 func (params *FetchAddressParams) SetPathAccountSid(PathAccountSid string) *FetchAddressParams { 196 params.PathAccountSid = &PathAccountSid 197 return params 198 } 199 200 // 201 func (c *ApiService) FetchAddress(Sid string, params *FetchAddressParams) (*ApiV2010Address, error) { 202 path := "/2010-04-01/Accounts/{AccountSid}/Addresses/{Sid}.json" 203 if params != nil && params.PathAccountSid != nil { 204 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 205 } else { 206 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 207 } 208 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 209 210 data := url.Values{} 211 headers := make(map[string]interface{}) 212 213 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 214 if err != nil { 215 return nil, err 216 } 217 218 defer resp.Body.Close() 219 220 ps := &ApiV2010Address{} 221 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 222 return nil, err 223 } 224 225 return ps, err 226 } 227 228 // Optional parameters for the method 'ListAddress' 229 type ListAddressParams struct { 230 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is responsible for the Address resource to read. 231 PathAccountSid *string `json:"PathAccountSid,omitempty"` 232 // The `customer_name` of the Address resources to read. 233 CustomerName *string `json:"CustomerName,omitempty"` 234 // The string that identifies the Address resources to read. 235 FriendlyName *string `json:"FriendlyName,omitempty"` 236 // The ISO country code of the Address resources to read. 237 IsoCountry *string `json:"IsoCountry,omitempty"` 238 // How many resources to return in each list page. The default is 50, and the maximum is 1000. 239 PageSize *int `json:"PageSize,omitempty"` 240 // Max number of records to return. 241 Limit *int `json:"limit,omitempty"` 242 } 243 244 func (params *ListAddressParams) SetPathAccountSid(PathAccountSid string) *ListAddressParams { 245 params.PathAccountSid = &PathAccountSid 246 return params 247 } 248 func (params *ListAddressParams) SetCustomerName(CustomerName string) *ListAddressParams { 249 params.CustomerName = &CustomerName 250 return params 251 } 252 func (params *ListAddressParams) SetFriendlyName(FriendlyName string) *ListAddressParams { 253 params.FriendlyName = &FriendlyName 254 return params 255 } 256 func (params *ListAddressParams) SetIsoCountry(IsoCountry string) *ListAddressParams { 257 params.IsoCountry = &IsoCountry 258 return params 259 } 260 func (params *ListAddressParams) SetPageSize(PageSize int) *ListAddressParams { 261 params.PageSize = &PageSize 262 return params 263 } 264 func (params *ListAddressParams) SetLimit(Limit int) *ListAddressParams { 265 params.Limit = &Limit 266 return params 267 } 268 269 // Retrieve a single page of Address records from the API. Request is executed immediately. 270 func (c *ApiService) PageAddress(params *ListAddressParams, pageToken, pageNumber string) (*ListAddressResponse, error) { 271 path := "/2010-04-01/Accounts/{AccountSid}/Addresses.json" 272 273 if params != nil && params.PathAccountSid != nil { 274 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 275 } else { 276 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 277 } 278 279 data := url.Values{} 280 headers := make(map[string]interface{}) 281 282 if params != nil && params.CustomerName != nil { 283 data.Set("CustomerName", *params.CustomerName) 284 } 285 if params != nil && params.FriendlyName != nil { 286 data.Set("FriendlyName", *params.FriendlyName) 287 } 288 if params != nil && params.IsoCountry != nil { 289 data.Set("IsoCountry", *params.IsoCountry) 290 } 291 if params != nil && params.PageSize != nil { 292 data.Set("PageSize", fmt.Sprint(*params.PageSize)) 293 } 294 295 if pageToken != "" { 296 data.Set("PageToken", pageToken) 297 } 298 if pageNumber != "" { 299 data.Set("Page", pageNumber) 300 } 301 302 resp, err := c.requestHandler.Get(c.baseURL+path, data, headers) 303 if err != nil { 304 return nil, err 305 } 306 307 defer resp.Body.Close() 308 309 ps := &ListAddressResponse{} 310 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 311 return nil, err 312 } 313 314 return ps, err 315 } 316 317 // Lists Address records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning. 318 func (c *ApiService) ListAddress(params *ListAddressParams) ([]ApiV2010Address, error) { 319 response, errors := c.StreamAddress(params) 320 321 records := make([]ApiV2010Address, 0) 322 for record := range response { 323 records = append(records, record) 324 } 325 326 if err := <-errors; err != nil { 327 return nil, err 328 } 329 330 return records, nil 331 } 332 333 // Streams Address records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached. 334 func (c *ApiService) StreamAddress(params *ListAddressParams) (chan ApiV2010Address, chan error) { 335 if params == nil { 336 params = &ListAddressParams{} 337 } 338 params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit)) 339 340 recordChannel := make(chan ApiV2010Address, 1) 341 errorChannel := make(chan error, 1) 342 343 response, err := c.PageAddress(params, "", "") 344 if err != nil { 345 errorChannel <- err 346 close(recordChannel) 347 close(errorChannel) 348 } else { 349 go c.streamAddress(response, params, recordChannel, errorChannel) 350 } 351 352 return recordChannel, errorChannel 353 } 354 355 func (c *ApiService) streamAddress(response *ListAddressResponse, params *ListAddressParams, recordChannel chan ApiV2010Address, errorChannel chan error) { 356 curRecord := 1 357 358 for response != nil { 359 responseRecords := response.Addresses 360 for item := range responseRecords { 361 recordChannel <- responseRecords[item] 362 curRecord += 1 363 if params.Limit != nil && *params.Limit < curRecord { 364 close(recordChannel) 365 close(errorChannel) 366 return 367 } 368 } 369 370 record, err := client.GetNext(c.baseURL, response, c.getNextListAddressResponse) 371 if err != nil { 372 errorChannel <- err 373 break 374 } else if record == nil { 375 break 376 } 377 378 response = record.(*ListAddressResponse) 379 } 380 381 close(recordChannel) 382 close(errorChannel) 383 } 384 385 func (c *ApiService) getNextListAddressResponse(nextPageUrl string) (interface{}, error) { 386 if nextPageUrl == "" { 387 return nil, nil 388 } 389 resp, err := c.requestHandler.Get(nextPageUrl, nil, nil) 390 if err != nil { 391 return nil, err 392 } 393 394 defer resp.Body.Close() 395 396 ps := &ListAddressResponse{} 397 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 398 return nil, err 399 } 400 return ps, nil 401 } 402 403 // Optional parameters for the method 'UpdateAddress' 404 type UpdateAddressParams struct { 405 // The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that is responsible for the Address resource to update. 406 PathAccountSid *string `json:"PathAccountSid,omitempty"` 407 // A descriptive string that you create to describe the address. It can be up to 64 characters long. 408 FriendlyName *string `json:"FriendlyName,omitempty"` 409 // The name to associate with the address. 410 CustomerName *string `json:"CustomerName,omitempty"` 411 // The number and street address of the address. 412 Street *string `json:"Street,omitempty"` 413 // The city of the address. 414 City *string `json:"City,omitempty"` 415 // The state or region of the address. 416 Region *string `json:"Region,omitempty"` 417 // The postal code of the address. 418 PostalCode *string `json:"PostalCode,omitempty"` 419 // Whether to enable emergency calling on the address. Can be: `true` or `false`. 420 EmergencyEnabled *bool `json:"EmergencyEnabled,omitempty"` 421 // Whether we should automatically correct the address. Can be: `true` or `false` and the default is `true`. If empty or `true`, we will correct the address you provide if necessary. If `false`, we won't alter the address you provide. 422 AutoCorrectAddress *bool `json:"AutoCorrectAddress,omitempty"` 423 // The additional number and street address of the address. 424 StreetSecondary *string `json:"StreetSecondary,omitempty"` 425 } 426 427 func (params *UpdateAddressParams) SetPathAccountSid(PathAccountSid string) *UpdateAddressParams { 428 params.PathAccountSid = &PathAccountSid 429 return params 430 } 431 func (params *UpdateAddressParams) SetFriendlyName(FriendlyName string) *UpdateAddressParams { 432 params.FriendlyName = &FriendlyName 433 return params 434 } 435 func (params *UpdateAddressParams) SetCustomerName(CustomerName string) *UpdateAddressParams { 436 params.CustomerName = &CustomerName 437 return params 438 } 439 func (params *UpdateAddressParams) SetStreet(Street string) *UpdateAddressParams { 440 params.Street = &Street 441 return params 442 } 443 func (params *UpdateAddressParams) SetCity(City string) *UpdateAddressParams { 444 params.City = &City 445 return params 446 } 447 func (params *UpdateAddressParams) SetRegion(Region string) *UpdateAddressParams { 448 params.Region = &Region 449 return params 450 } 451 func (params *UpdateAddressParams) SetPostalCode(PostalCode string) *UpdateAddressParams { 452 params.PostalCode = &PostalCode 453 return params 454 } 455 func (params *UpdateAddressParams) SetEmergencyEnabled(EmergencyEnabled bool) *UpdateAddressParams { 456 params.EmergencyEnabled = &EmergencyEnabled 457 return params 458 } 459 func (params *UpdateAddressParams) SetAutoCorrectAddress(AutoCorrectAddress bool) *UpdateAddressParams { 460 params.AutoCorrectAddress = &AutoCorrectAddress 461 return params 462 } 463 func (params *UpdateAddressParams) SetStreetSecondary(StreetSecondary string) *UpdateAddressParams { 464 params.StreetSecondary = &StreetSecondary 465 return params 466 } 467 468 // 469 func (c *ApiService) UpdateAddress(Sid string, params *UpdateAddressParams) (*ApiV2010Address, error) { 470 path := "/2010-04-01/Accounts/{AccountSid}/Addresses/{Sid}.json" 471 if params != nil && params.PathAccountSid != nil { 472 path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1) 473 } else { 474 path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1) 475 } 476 path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1) 477 478 data := url.Values{} 479 headers := make(map[string]interface{}) 480 481 if params != nil && params.FriendlyName != nil { 482 data.Set("FriendlyName", *params.FriendlyName) 483 } 484 if params != nil && params.CustomerName != nil { 485 data.Set("CustomerName", *params.CustomerName) 486 } 487 if params != nil && params.Street != nil { 488 data.Set("Street", *params.Street) 489 } 490 if params != nil && params.City != nil { 491 data.Set("City", *params.City) 492 } 493 if params != nil && params.Region != nil { 494 data.Set("Region", *params.Region) 495 } 496 if params != nil && params.PostalCode != nil { 497 data.Set("PostalCode", *params.PostalCode) 498 } 499 if params != nil && params.EmergencyEnabled != nil { 500 data.Set("EmergencyEnabled", fmt.Sprint(*params.EmergencyEnabled)) 501 } 502 if params != nil && params.AutoCorrectAddress != nil { 503 data.Set("AutoCorrectAddress", fmt.Sprint(*params.AutoCorrectAddress)) 504 } 505 if params != nil && params.StreetSecondary != nil { 506 data.Set("StreetSecondary", *params.StreetSecondary) 507 } 508 509 resp, err := c.requestHandler.Post(c.baseURL+path, data, headers) 510 if err != nil { 511 return nil, err 512 } 513 514 defer resp.Body.Close() 515 516 ps := &ApiV2010Address{} 517 if err := json.NewDecoder(resp.Body).Decode(ps); err != nil { 518 return nil, err 519 } 520 521 return ps, err 522 }