github.com/twilio/twilio-go@v1.20.1/rest/voice/v1/byoc_trunks.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Voice
     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 'CreateByocTrunk'
    27  type CreateByocTrunkParams struct {
    28  	// A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// The URL we should call when the BYOC Trunk receives a call.
    31  	VoiceUrl *string `json:"VoiceUrl,omitempty"`
    32  	// The HTTP method we should use to call `voice_url`. Can be: `GET` or `POST`.
    33  	VoiceMethod *string `json:"VoiceMethod,omitempty"`
    34  	// The URL that we should call when an error occurs while retrieving or executing the TwiML from `voice_url`.
    35  	VoiceFallbackUrl *string `json:"VoiceFallbackUrl,omitempty"`
    36  	// The HTTP method we should use to call `voice_fallback_url`. Can be: `GET` or `POST`.
    37  	VoiceFallbackMethod *string `json:"VoiceFallbackMethod,omitempty"`
    38  	// The URL that we should call to pass status parameters (such as call ended) to your application.
    39  	StatusCallbackUrl *string `json:"StatusCallbackUrl,omitempty"`
    40  	// The HTTP method we should use to call `status_callback_url`. Can be: `GET` or `POST`.
    41  	StatusCallbackMethod *string `json:"StatusCallbackMethod,omitempty"`
    42  	// Whether Caller ID Name (CNAM) lookup is enabled for the trunk. If enabled, all inbound calls to the BYOC Trunk from the United States and Canada automatically perform a CNAM Lookup and display Caller ID data on your phone. See [CNAM Lookups](https://www.twilio.com/docs/sip-trunking#CNAM) for more information.
    43  	CnamLookupEnabled *bool `json:"CnamLookupEnabled,omitempty"`
    44  	// The SID of the Connection Policy that Twilio will use when routing traffic to your communications infrastructure.
    45  	ConnectionPolicySid *string `json:"ConnectionPolicySid,omitempty"`
    46  	// The SID of the SIP Domain that should be used in the `From` header of originating calls sent to your SIP infrastructure. If your SIP infrastructure allows users to \\\"call back\\\" an incoming call, configure this with a [SIP Domain](https://www.twilio.com/docs/voice/api/sending-sip) to ensure proper routing. If not configured, the from domain will default to \\\"sip.twilio.com\\\".
    47  	FromDomainSid *string `json:"FromDomainSid,omitempty"`
    48  }
    49  
    50  func (params *CreateByocTrunkParams) SetFriendlyName(FriendlyName string) *CreateByocTrunkParams {
    51  	params.FriendlyName = &FriendlyName
    52  	return params
    53  }
    54  func (params *CreateByocTrunkParams) SetVoiceUrl(VoiceUrl string) *CreateByocTrunkParams {
    55  	params.VoiceUrl = &VoiceUrl
    56  	return params
    57  }
    58  func (params *CreateByocTrunkParams) SetVoiceMethod(VoiceMethod string) *CreateByocTrunkParams {
    59  	params.VoiceMethod = &VoiceMethod
    60  	return params
    61  }
    62  func (params *CreateByocTrunkParams) SetVoiceFallbackUrl(VoiceFallbackUrl string) *CreateByocTrunkParams {
    63  	params.VoiceFallbackUrl = &VoiceFallbackUrl
    64  	return params
    65  }
    66  func (params *CreateByocTrunkParams) SetVoiceFallbackMethod(VoiceFallbackMethod string) *CreateByocTrunkParams {
    67  	params.VoiceFallbackMethod = &VoiceFallbackMethod
    68  	return params
    69  }
    70  func (params *CreateByocTrunkParams) SetStatusCallbackUrl(StatusCallbackUrl string) *CreateByocTrunkParams {
    71  	params.StatusCallbackUrl = &StatusCallbackUrl
    72  	return params
    73  }
    74  func (params *CreateByocTrunkParams) SetStatusCallbackMethod(StatusCallbackMethod string) *CreateByocTrunkParams {
    75  	params.StatusCallbackMethod = &StatusCallbackMethod
    76  	return params
    77  }
    78  func (params *CreateByocTrunkParams) SetCnamLookupEnabled(CnamLookupEnabled bool) *CreateByocTrunkParams {
    79  	params.CnamLookupEnabled = &CnamLookupEnabled
    80  	return params
    81  }
    82  func (params *CreateByocTrunkParams) SetConnectionPolicySid(ConnectionPolicySid string) *CreateByocTrunkParams {
    83  	params.ConnectionPolicySid = &ConnectionPolicySid
    84  	return params
    85  }
    86  func (params *CreateByocTrunkParams) SetFromDomainSid(FromDomainSid string) *CreateByocTrunkParams {
    87  	params.FromDomainSid = &FromDomainSid
    88  	return params
    89  }
    90  
    91  //
    92  func (c *ApiService) CreateByocTrunk(params *CreateByocTrunkParams) (*VoiceV1ByocTrunk, error) {
    93  	path := "/v1/ByocTrunks"
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	if params != nil && params.FriendlyName != nil {
    99  		data.Set("FriendlyName", *params.FriendlyName)
   100  	}
   101  	if params != nil && params.VoiceUrl != nil {
   102  		data.Set("VoiceUrl", *params.VoiceUrl)
   103  	}
   104  	if params != nil && params.VoiceMethod != nil {
   105  		data.Set("VoiceMethod", *params.VoiceMethod)
   106  	}
   107  	if params != nil && params.VoiceFallbackUrl != nil {
   108  		data.Set("VoiceFallbackUrl", *params.VoiceFallbackUrl)
   109  	}
   110  	if params != nil && params.VoiceFallbackMethod != nil {
   111  		data.Set("VoiceFallbackMethod", *params.VoiceFallbackMethod)
   112  	}
   113  	if params != nil && params.StatusCallbackUrl != nil {
   114  		data.Set("StatusCallbackUrl", *params.StatusCallbackUrl)
   115  	}
   116  	if params != nil && params.StatusCallbackMethod != nil {
   117  		data.Set("StatusCallbackMethod", *params.StatusCallbackMethod)
   118  	}
   119  	if params != nil && params.CnamLookupEnabled != nil {
   120  		data.Set("CnamLookupEnabled", fmt.Sprint(*params.CnamLookupEnabled))
   121  	}
   122  	if params != nil && params.ConnectionPolicySid != nil {
   123  		data.Set("ConnectionPolicySid", *params.ConnectionPolicySid)
   124  	}
   125  	if params != nil && params.FromDomainSid != nil {
   126  		data.Set("FromDomainSid", *params.FromDomainSid)
   127  	}
   128  
   129  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   130  	if err != nil {
   131  		return nil, err
   132  	}
   133  
   134  	defer resp.Body.Close()
   135  
   136  	ps := &VoiceV1ByocTrunk{}
   137  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return ps, err
   142  }
   143  
   144  //
   145  func (c *ApiService) DeleteByocTrunk(Sid string) error {
   146  	path := "/v1/ByocTrunks/{Sid}"
   147  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   148  
   149  	data := url.Values{}
   150  	headers := make(map[string]interface{})
   151  
   152  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   153  	if err != nil {
   154  		return err
   155  	}
   156  
   157  	defer resp.Body.Close()
   158  
   159  	return nil
   160  }
   161  
   162  //
   163  func (c *ApiService) FetchByocTrunk(Sid string) (*VoiceV1ByocTrunk, error) {
   164  	path := "/v1/ByocTrunks/{Sid}"
   165  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   166  
   167  	data := url.Values{}
   168  	headers := make(map[string]interface{})
   169  
   170  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   171  	if err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	defer resp.Body.Close()
   176  
   177  	ps := &VoiceV1ByocTrunk{}
   178  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   179  		return nil, err
   180  	}
   181  
   182  	return ps, err
   183  }
   184  
   185  // Optional parameters for the method 'ListByocTrunk'
   186  type ListByocTrunkParams struct {
   187  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   188  	PageSize *int `json:"PageSize,omitempty"`
   189  	// Max number of records to return.
   190  	Limit *int `json:"limit,omitempty"`
   191  }
   192  
   193  func (params *ListByocTrunkParams) SetPageSize(PageSize int) *ListByocTrunkParams {
   194  	params.PageSize = &PageSize
   195  	return params
   196  }
   197  func (params *ListByocTrunkParams) SetLimit(Limit int) *ListByocTrunkParams {
   198  	params.Limit = &Limit
   199  	return params
   200  }
   201  
   202  // Retrieve a single page of ByocTrunk records from the API. Request is executed immediately.
   203  func (c *ApiService) PageByocTrunk(params *ListByocTrunkParams, pageToken, pageNumber string) (*ListByocTrunkResponse, error) {
   204  	path := "/v1/ByocTrunks"
   205  
   206  	data := url.Values{}
   207  	headers := make(map[string]interface{})
   208  
   209  	if params != nil && params.PageSize != nil {
   210  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   211  	}
   212  
   213  	if pageToken != "" {
   214  		data.Set("PageToken", pageToken)
   215  	}
   216  	if pageNumber != "" {
   217  		data.Set("Page", pageNumber)
   218  	}
   219  
   220  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	defer resp.Body.Close()
   226  
   227  	ps := &ListByocTrunkResponse{}
   228  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	return ps, err
   233  }
   234  
   235  // Lists ByocTrunk records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   236  func (c *ApiService) ListByocTrunk(params *ListByocTrunkParams) ([]VoiceV1ByocTrunk, error) {
   237  	response, errors := c.StreamByocTrunk(params)
   238  
   239  	records := make([]VoiceV1ByocTrunk, 0)
   240  	for record := range response {
   241  		records = append(records, record)
   242  	}
   243  
   244  	if err := <-errors; err != nil {
   245  		return nil, err
   246  	}
   247  
   248  	return records, nil
   249  }
   250  
   251  // Streams ByocTrunk records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   252  func (c *ApiService) StreamByocTrunk(params *ListByocTrunkParams) (chan VoiceV1ByocTrunk, chan error) {
   253  	if params == nil {
   254  		params = &ListByocTrunkParams{}
   255  	}
   256  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   257  
   258  	recordChannel := make(chan VoiceV1ByocTrunk, 1)
   259  	errorChannel := make(chan error, 1)
   260  
   261  	response, err := c.PageByocTrunk(params, "", "")
   262  	if err != nil {
   263  		errorChannel <- err
   264  		close(recordChannel)
   265  		close(errorChannel)
   266  	} else {
   267  		go c.streamByocTrunk(response, params, recordChannel, errorChannel)
   268  	}
   269  
   270  	return recordChannel, errorChannel
   271  }
   272  
   273  func (c *ApiService) streamByocTrunk(response *ListByocTrunkResponse, params *ListByocTrunkParams, recordChannel chan VoiceV1ByocTrunk, errorChannel chan error) {
   274  	curRecord := 1
   275  
   276  	for response != nil {
   277  		responseRecords := response.ByocTrunks
   278  		for item := range responseRecords {
   279  			recordChannel <- responseRecords[item]
   280  			curRecord += 1
   281  			if params.Limit != nil && *params.Limit < curRecord {
   282  				close(recordChannel)
   283  				close(errorChannel)
   284  				return
   285  			}
   286  		}
   287  
   288  		record, err := client.GetNext(c.baseURL, response, c.getNextListByocTrunkResponse)
   289  		if err != nil {
   290  			errorChannel <- err
   291  			break
   292  		} else if record == nil {
   293  			break
   294  		}
   295  
   296  		response = record.(*ListByocTrunkResponse)
   297  	}
   298  
   299  	close(recordChannel)
   300  	close(errorChannel)
   301  }
   302  
   303  func (c *ApiService) getNextListByocTrunkResponse(nextPageUrl string) (interface{}, error) {
   304  	if nextPageUrl == "" {
   305  		return nil, nil
   306  	}
   307  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  
   312  	defer resp.Body.Close()
   313  
   314  	ps := &ListByocTrunkResponse{}
   315  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   316  		return nil, err
   317  	}
   318  	return ps, nil
   319  }
   320  
   321  // Optional parameters for the method 'UpdateByocTrunk'
   322  type UpdateByocTrunkParams struct {
   323  	// A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long.
   324  	FriendlyName *string `json:"FriendlyName,omitempty"`
   325  	// The URL we should call when the BYOC Trunk receives a call.
   326  	VoiceUrl *string `json:"VoiceUrl,omitempty"`
   327  	// The HTTP method we should use to call `voice_url`
   328  	VoiceMethod *string `json:"VoiceMethod,omitempty"`
   329  	// The URL that we should call when an error occurs while retrieving or executing the TwiML requested by `voice_url`.
   330  	VoiceFallbackUrl *string `json:"VoiceFallbackUrl,omitempty"`
   331  	// The HTTP method we should use to call `voice_fallback_url`. Can be: `GET` or `POST`.
   332  	VoiceFallbackMethod *string `json:"VoiceFallbackMethod,omitempty"`
   333  	// The URL that we should call to pass status parameters (such as call ended) to your application.
   334  	StatusCallbackUrl *string `json:"StatusCallbackUrl,omitempty"`
   335  	// The HTTP method we should use to call `status_callback_url`. Can be: `GET` or `POST`.
   336  	StatusCallbackMethod *string `json:"StatusCallbackMethod,omitempty"`
   337  	// Whether Caller ID Name (CNAM) lookup is enabled for the trunk. If enabled, all inbound calls to the BYOC Trunk from the United States and Canada automatically perform a CNAM Lookup and display Caller ID data on your phone. See [CNAM Lookups](https://www.twilio.com/docs/sip-trunking#CNAM) for more information.
   338  	CnamLookupEnabled *bool `json:"CnamLookupEnabled,omitempty"`
   339  	// The SID of the Connection Policy that Twilio will use when routing traffic to your communications infrastructure.
   340  	ConnectionPolicySid *string `json:"ConnectionPolicySid,omitempty"`
   341  	// The SID of the SIP Domain that should be used in the `From` header of originating calls sent to your SIP infrastructure. If your SIP infrastructure allows users to \\\"call back\\\" an incoming call, configure this with a [SIP Domain](https://www.twilio.com/docs/voice/api/sending-sip) to ensure proper routing. If not configured, the from domain will default to \\\"sip.twilio.com\\\".
   342  	FromDomainSid *string `json:"FromDomainSid,omitempty"`
   343  }
   344  
   345  func (params *UpdateByocTrunkParams) SetFriendlyName(FriendlyName string) *UpdateByocTrunkParams {
   346  	params.FriendlyName = &FriendlyName
   347  	return params
   348  }
   349  func (params *UpdateByocTrunkParams) SetVoiceUrl(VoiceUrl string) *UpdateByocTrunkParams {
   350  	params.VoiceUrl = &VoiceUrl
   351  	return params
   352  }
   353  func (params *UpdateByocTrunkParams) SetVoiceMethod(VoiceMethod string) *UpdateByocTrunkParams {
   354  	params.VoiceMethod = &VoiceMethod
   355  	return params
   356  }
   357  func (params *UpdateByocTrunkParams) SetVoiceFallbackUrl(VoiceFallbackUrl string) *UpdateByocTrunkParams {
   358  	params.VoiceFallbackUrl = &VoiceFallbackUrl
   359  	return params
   360  }
   361  func (params *UpdateByocTrunkParams) SetVoiceFallbackMethod(VoiceFallbackMethod string) *UpdateByocTrunkParams {
   362  	params.VoiceFallbackMethod = &VoiceFallbackMethod
   363  	return params
   364  }
   365  func (params *UpdateByocTrunkParams) SetStatusCallbackUrl(StatusCallbackUrl string) *UpdateByocTrunkParams {
   366  	params.StatusCallbackUrl = &StatusCallbackUrl
   367  	return params
   368  }
   369  func (params *UpdateByocTrunkParams) SetStatusCallbackMethod(StatusCallbackMethod string) *UpdateByocTrunkParams {
   370  	params.StatusCallbackMethod = &StatusCallbackMethod
   371  	return params
   372  }
   373  func (params *UpdateByocTrunkParams) SetCnamLookupEnabled(CnamLookupEnabled bool) *UpdateByocTrunkParams {
   374  	params.CnamLookupEnabled = &CnamLookupEnabled
   375  	return params
   376  }
   377  func (params *UpdateByocTrunkParams) SetConnectionPolicySid(ConnectionPolicySid string) *UpdateByocTrunkParams {
   378  	params.ConnectionPolicySid = &ConnectionPolicySid
   379  	return params
   380  }
   381  func (params *UpdateByocTrunkParams) SetFromDomainSid(FromDomainSid string) *UpdateByocTrunkParams {
   382  	params.FromDomainSid = &FromDomainSid
   383  	return params
   384  }
   385  
   386  //
   387  func (c *ApiService) UpdateByocTrunk(Sid string, params *UpdateByocTrunkParams) (*VoiceV1ByocTrunk, error) {
   388  	path := "/v1/ByocTrunks/{Sid}"
   389  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   390  
   391  	data := url.Values{}
   392  	headers := make(map[string]interface{})
   393  
   394  	if params != nil && params.FriendlyName != nil {
   395  		data.Set("FriendlyName", *params.FriendlyName)
   396  	}
   397  	if params != nil && params.VoiceUrl != nil {
   398  		data.Set("VoiceUrl", *params.VoiceUrl)
   399  	}
   400  	if params != nil && params.VoiceMethod != nil {
   401  		data.Set("VoiceMethod", *params.VoiceMethod)
   402  	}
   403  	if params != nil && params.VoiceFallbackUrl != nil {
   404  		data.Set("VoiceFallbackUrl", *params.VoiceFallbackUrl)
   405  	}
   406  	if params != nil && params.VoiceFallbackMethod != nil {
   407  		data.Set("VoiceFallbackMethod", *params.VoiceFallbackMethod)
   408  	}
   409  	if params != nil && params.StatusCallbackUrl != nil {
   410  		data.Set("StatusCallbackUrl", *params.StatusCallbackUrl)
   411  	}
   412  	if params != nil && params.StatusCallbackMethod != nil {
   413  		data.Set("StatusCallbackMethod", *params.StatusCallbackMethod)
   414  	}
   415  	if params != nil && params.CnamLookupEnabled != nil {
   416  		data.Set("CnamLookupEnabled", fmt.Sprint(*params.CnamLookupEnabled))
   417  	}
   418  	if params != nil && params.ConnectionPolicySid != nil {
   419  		data.Set("ConnectionPolicySid", *params.ConnectionPolicySid)
   420  	}
   421  	if params != nil && params.FromDomainSid != nil {
   422  		data.Set("FromDomainSid", *params.FromDomainSid)
   423  	}
   424  
   425  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   426  	if err != nil {
   427  		return nil, err
   428  	}
   429  
   430  	defer resp.Body.Close()
   431  
   432  	ps := &VoiceV1ByocTrunk{}
   433  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   434  		return nil, err
   435  	}
   436  
   437  	return ps, err
   438  }