github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_outgoing_caller_ids.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 'CreateValidationRequest'
    27  type CreateValidationRequestParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) responsible for the new caller ID resource.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  	// The phone number to verify in [E.164](https://www.twilio.com/docs/glossary/what-e164) format, which consists of a + followed by the country code and subscriber number.
    31  	PhoneNumber *string `json:"PhoneNumber,omitempty"`
    32  	// A descriptive string that you create to describe the new caller ID resource. It can be up to 64 characters long. The default value is a formatted version of the phone number.
    33  	FriendlyName *string `json:"FriendlyName,omitempty"`
    34  	// The number of seconds to delay before initiating the verification call. Can be an integer between `0` and `60`, inclusive. The default is `0`.
    35  	CallDelay *int `json:"CallDelay,omitempty"`
    36  	// The digits to dial after connecting the verification call.
    37  	Extension *string `json:"Extension,omitempty"`
    38  	// The URL we should call using the `status_callback_method` to send status information about the verification process to your application.
    39  	StatusCallback *string `json:"StatusCallback,omitempty"`
    40  	// The HTTP method we should use to call `status_callback`. Can be: `GET` or `POST`, and the default is `POST`.
    41  	StatusCallbackMethod *string `json:"StatusCallbackMethod,omitempty"`
    42  }
    43  
    44  func (params *CreateValidationRequestParams) SetPathAccountSid(PathAccountSid string) *CreateValidationRequestParams {
    45  	params.PathAccountSid = &PathAccountSid
    46  	return params
    47  }
    48  func (params *CreateValidationRequestParams) SetPhoneNumber(PhoneNumber string) *CreateValidationRequestParams {
    49  	params.PhoneNumber = &PhoneNumber
    50  	return params
    51  }
    52  func (params *CreateValidationRequestParams) SetFriendlyName(FriendlyName string) *CreateValidationRequestParams {
    53  	params.FriendlyName = &FriendlyName
    54  	return params
    55  }
    56  func (params *CreateValidationRequestParams) SetCallDelay(CallDelay int) *CreateValidationRequestParams {
    57  	params.CallDelay = &CallDelay
    58  	return params
    59  }
    60  func (params *CreateValidationRequestParams) SetExtension(Extension string) *CreateValidationRequestParams {
    61  	params.Extension = &Extension
    62  	return params
    63  }
    64  func (params *CreateValidationRequestParams) SetStatusCallback(StatusCallback string) *CreateValidationRequestParams {
    65  	params.StatusCallback = &StatusCallback
    66  	return params
    67  }
    68  func (params *CreateValidationRequestParams) SetStatusCallbackMethod(StatusCallbackMethod string) *CreateValidationRequestParams {
    69  	params.StatusCallbackMethod = &StatusCallbackMethod
    70  	return params
    71  }
    72  
    73  //
    74  func (c *ApiService) CreateValidationRequest(params *CreateValidationRequestParams) (*ApiV2010ValidationRequest, error) {
    75  	path := "/2010-04-01/Accounts/{AccountSid}/OutgoingCallerIds.json"
    76  	if params != nil && params.PathAccountSid != nil {
    77  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    78  	} else {
    79  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    80  	}
    81  
    82  	data := url.Values{}
    83  	headers := make(map[string]interface{})
    84  
    85  	if params != nil && params.PhoneNumber != nil {
    86  		data.Set("PhoneNumber", *params.PhoneNumber)
    87  	}
    88  	if params != nil && params.FriendlyName != nil {
    89  		data.Set("FriendlyName", *params.FriendlyName)
    90  	}
    91  	if params != nil && params.CallDelay != nil {
    92  		data.Set("CallDelay", fmt.Sprint(*params.CallDelay))
    93  	}
    94  	if params != nil && params.Extension != nil {
    95  		data.Set("Extension", *params.Extension)
    96  	}
    97  	if params != nil && params.StatusCallback != nil {
    98  		data.Set("StatusCallback", *params.StatusCallback)
    99  	}
   100  	if params != nil && params.StatusCallbackMethod != nil {
   101  		data.Set("StatusCallbackMethod", *params.StatusCallbackMethod)
   102  	}
   103  
   104  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	defer resp.Body.Close()
   110  
   111  	ps := &ApiV2010ValidationRequest{}
   112  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return ps, err
   117  }
   118  
   119  // Optional parameters for the method 'DeleteOutgoingCallerId'
   120  type DeleteOutgoingCallerIdParams struct {
   121  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the OutgoingCallerId resources to delete.
   122  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   123  }
   124  
   125  func (params *DeleteOutgoingCallerIdParams) SetPathAccountSid(PathAccountSid string) *DeleteOutgoingCallerIdParams {
   126  	params.PathAccountSid = &PathAccountSid
   127  	return params
   128  }
   129  
   130  // Delete the caller-id specified from the account
   131  func (c *ApiService) DeleteOutgoingCallerId(Sid string, params *DeleteOutgoingCallerIdParams) error {
   132  	path := "/2010-04-01/Accounts/{AccountSid}/OutgoingCallerIds/{Sid}.json"
   133  	if params != nil && params.PathAccountSid != nil {
   134  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   135  	} else {
   136  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   137  	}
   138  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   139  
   140  	data := url.Values{}
   141  	headers := make(map[string]interface{})
   142  
   143  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	defer resp.Body.Close()
   149  
   150  	return nil
   151  }
   152  
   153  // Optional parameters for the method 'FetchOutgoingCallerId'
   154  type FetchOutgoingCallerIdParams struct {
   155  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the OutgoingCallerId resource to fetch.
   156  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   157  }
   158  
   159  func (params *FetchOutgoingCallerIdParams) SetPathAccountSid(PathAccountSid string) *FetchOutgoingCallerIdParams {
   160  	params.PathAccountSid = &PathAccountSid
   161  	return params
   162  }
   163  
   164  // Fetch an outgoing-caller-id belonging to the account used to make the request
   165  func (c *ApiService) FetchOutgoingCallerId(Sid string, params *FetchOutgoingCallerIdParams) (*ApiV2010OutgoingCallerId, error) {
   166  	path := "/2010-04-01/Accounts/{AccountSid}/OutgoingCallerIds/{Sid}.json"
   167  	if params != nil && params.PathAccountSid != nil {
   168  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   169  	} else {
   170  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   171  	}
   172  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   173  
   174  	data := url.Values{}
   175  	headers := make(map[string]interface{})
   176  
   177  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  
   182  	defer resp.Body.Close()
   183  
   184  	ps := &ApiV2010OutgoingCallerId{}
   185  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   186  		return nil, err
   187  	}
   188  
   189  	return ps, err
   190  }
   191  
   192  // Optional parameters for the method 'ListOutgoingCallerId'
   193  type ListOutgoingCallerIdParams struct {
   194  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the OutgoingCallerId resources to read.
   195  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   196  	// The phone number of the OutgoingCallerId resources to read.
   197  	PhoneNumber *string `json:"PhoneNumber,omitempty"`
   198  	// The string that identifies the OutgoingCallerId resources to read.
   199  	FriendlyName *string `json:"FriendlyName,omitempty"`
   200  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   201  	PageSize *int `json:"PageSize,omitempty"`
   202  	// Max number of records to return.
   203  	Limit *int `json:"limit,omitempty"`
   204  }
   205  
   206  func (params *ListOutgoingCallerIdParams) SetPathAccountSid(PathAccountSid string) *ListOutgoingCallerIdParams {
   207  	params.PathAccountSid = &PathAccountSid
   208  	return params
   209  }
   210  func (params *ListOutgoingCallerIdParams) SetPhoneNumber(PhoneNumber string) *ListOutgoingCallerIdParams {
   211  	params.PhoneNumber = &PhoneNumber
   212  	return params
   213  }
   214  func (params *ListOutgoingCallerIdParams) SetFriendlyName(FriendlyName string) *ListOutgoingCallerIdParams {
   215  	params.FriendlyName = &FriendlyName
   216  	return params
   217  }
   218  func (params *ListOutgoingCallerIdParams) SetPageSize(PageSize int) *ListOutgoingCallerIdParams {
   219  	params.PageSize = &PageSize
   220  	return params
   221  }
   222  func (params *ListOutgoingCallerIdParams) SetLimit(Limit int) *ListOutgoingCallerIdParams {
   223  	params.Limit = &Limit
   224  	return params
   225  }
   226  
   227  // Retrieve a single page of OutgoingCallerId records from the API. Request is executed immediately.
   228  func (c *ApiService) PageOutgoingCallerId(params *ListOutgoingCallerIdParams, pageToken, pageNumber string) (*ListOutgoingCallerIdResponse, error) {
   229  	path := "/2010-04-01/Accounts/{AccountSid}/OutgoingCallerIds.json"
   230  
   231  	if params != nil && params.PathAccountSid != nil {
   232  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   233  	} else {
   234  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   235  	}
   236  
   237  	data := url.Values{}
   238  	headers := make(map[string]interface{})
   239  
   240  	if params != nil && params.PhoneNumber != nil {
   241  		data.Set("PhoneNumber", *params.PhoneNumber)
   242  	}
   243  	if params != nil && params.FriendlyName != nil {
   244  		data.Set("FriendlyName", *params.FriendlyName)
   245  	}
   246  	if params != nil && params.PageSize != nil {
   247  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   248  	}
   249  
   250  	if pageToken != "" {
   251  		data.Set("PageToken", pageToken)
   252  	}
   253  	if pageNumber != "" {
   254  		data.Set("Page", pageNumber)
   255  	}
   256  
   257  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   258  	if err != nil {
   259  		return nil, err
   260  	}
   261  
   262  	defer resp.Body.Close()
   263  
   264  	ps := &ListOutgoingCallerIdResponse{}
   265  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   266  		return nil, err
   267  	}
   268  
   269  	return ps, err
   270  }
   271  
   272  // Lists OutgoingCallerId records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   273  func (c *ApiService) ListOutgoingCallerId(params *ListOutgoingCallerIdParams) ([]ApiV2010OutgoingCallerId, error) {
   274  	response, errors := c.StreamOutgoingCallerId(params)
   275  
   276  	records := make([]ApiV2010OutgoingCallerId, 0)
   277  	for record := range response {
   278  		records = append(records, record)
   279  	}
   280  
   281  	if err := <-errors; err != nil {
   282  		return nil, err
   283  	}
   284  
   285  	return records, nil
   286  }
   287  
   288  // Streams OutgoingCallerId records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   289  func (c *ApiService) StreamOutgoingCallerId(params *ListOutgoingCallerIdParams) (chan ApiV2010OutgoingCallerId, chan error) {
   290  	if params == nil {
   291  		params = &ListOutgoingCallerIdParams{}
   292  	}
   293  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   294  
   295  	recordChannel := make(chan ApiV2010OutgoingCallerId, 1)
   296  	errorChannel := make(chan error, 1)
   297  
   298  	response, err := c.PageOutgoingCallerId(params, "", "")
   299  	if err != nil {
   300  		errorChannel <- err
   301  		close(recordChannel)
   302  		close(errorChannel)
   303  	} else {
   304  		go c.streamOutgoingCallerId(response, params, recordChannel, errorChannel)
   305  	}
   306  
   307  	return recordChannel, errorChannel
   308  }
   309  
   310  func (c *ApiService) streamOutgoingCallerId(response *ListOutgoingCallerIdResponse, params *ListOutgoingCallerIdParams, recordChannel chan ApiV2010OutgoingCallerId, errorChannel chan error) {
   311  	curRecord := 1
   312  
   313  	for response != nil {
   314  		responseRecords := response.OutgoingCallerIds
   315  		for item := range responseRecords {
   316  			recordChannel <- responseRecords[item]
   317  			curRecord += 1
   318  			if params.Limit != nil && *params.Limit < curRecord {
   319  				close(recordChannel)
   320  				close(errorChannel)
   321  				return
   322  			}
   323  		}
   324  
   325  		record, err := client.GetNext(c.baseURL, response, c.getNextListOutgoingCallerIdResponse)
   326  		if err != nil {
   327  			errorChannel <- err
   328  			break
   329  		} else if record == nil {
   330  			break
   331  		}
   332  
   333  		response = record.(*ListOutgoingCallerIdResponse)
   334  	}
   335  
   336  	close(recordChannel)
   337  	close(errorChannel)
   338  }
   339  
   340  func (c *ApiService) getNextListOutgoingCallerIdResponse(nextPageUrl string) (interface{}, error) {
   341  	if nextPageUrl == "" {
   342  		return nil, nil
   343  	}
   344  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   345  	if err != nil {
   346  		return nil, err
   347  	}
   348  
   349  	defer resp.Body.Close()
   350  
   351  	ps := &ListOutgoingCallerIdResponse{}
   352  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   353  		return nil, err
   354  	}
   355  	return ps, nil
   356  }
   357  
   358  // Optional parameters for the method 'UpdateOutgoingCallerId'
   359  type UpdateOutgoingCallerIdParams struct {
   360  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the OutgoingCallerId resources to update.
   361  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   362  	// A descriptive string that you create to describe the resource. It can be up to 64 characters long.
   363  	FriendlyName *string `json:"FriendlyName,omitempty"`
   364  }
   365  
   366  func (params *UpdateOutgoingCallerIdParams) SetPathAccountSid(PathAccountSid string) *UpdateOutgoingCallerIdParams {
   367  	params.PathAccountSid = &PathAccountSid
   368  	return params
   369  }
   370  func (params *UpdateOutgoingCallerIdParams) SetFriendlyName(FriendlyName string) *UpdateOutgoingCallerIdParams {
   371  	params.FriendlyName = &FriendlyName
   372  	return params
   373  }
   374  
   375  // Updates the caller-id
   376  func (c *ApiService) UpdateOutgoingCallerId(Sid string, params *UpdateOutgoingCallerIdParams) (*ApiV2010OutgoingCallerId, error) {
   377  	path := "/2010-04-01/Accounts/{AccountSid}/OutgoingCallerIds/{Sid}.json"
   378  	if params != nil && params.PathAccountSid != nil {
   379  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   380  	} else {
   381  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   382  	}
   383  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   384  
   385  	data := url.Values{}
   386  	headers := make(map[string]interface{})
   387  
   388  	if params != nil && params.FriendlyName != nil {
   389  		data.Set("FriendlyName", *params.FriendlyName)
   390  	}
   391  
   392  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   393  	if err != nil {
   394  		return nil, err
   395  	}
   396  
   397  	defer resp.Body.Close()
   398  
   399  	ps := &ApiV2010OutgoingCallerId{}
   400  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   401  		return nil, err
   402  	}
   403  
   404  	return ps, err
   405  }