github.com/twilio/twilio-go@v1.20.1/rest/trunking/v1/trunks.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Trunking
     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 'CreateTrunk'
    27  type CreateTrunkParams struct {
    28  	// A descriptive string that you create to describe the resource. It can be up to 64 characters long.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// The unique address you reserve on Twilio to which you route your SIP traffic. Domain names can contain letters, digits, and `-` and must end with `pstn.twilio.com`. See [Termination Settings](https://www.twilio.com/docs/sip-trunking#termination) for more information.
    31  	DomainName *string `json:"DomainName,omitempty"`
    32  	// The URL we should call using the `disaster_recovery_method` if an error occurs while sending SIP traffic towards the configured Origination URL. We retrieve TwiML from the URL and execute the instructions like any other normal TwiML call. See [Disaster Recovery](https://www.twilio.com/docs/sip-trunking#disaster-recovery) for more information.
    33  	DisasterRecoveryUrl *string `json:"DisasterRecoveryUrl,omitempty"`
    34  	// The HTTP method we should use to call the `disaster_recovery_url`. Can be: `GET` or `POST`.
    35  	DisasterRecoveryMethod *string `json:"DisasterRecoveryMethod,omitempty"`
    36  	//
    37  	TransferMode *string `json:"TransferMode,omitempty"`
    38  	// Whether Secure Trunking is enabled for the trunk. If enabled, all calls going through the trunk will be secure using SRTP for media and TLS for signaling. If disabled, then RTP will be used for media. See [Secure Trunking](https://www.twilio.com/docs/sip-trunking#securetrunking) for more information.
    39  	Secure *bool `json:"Secure,omitempty"`
    40  	// Whether Caller ID Name (CNAM) lookup should be enabled for the trunk. If enabled, all inbound calls to the SIP 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.
    41  	CnamLookupEnabled *bool `json:"CnamLookupEnabled,omitempty"`
    42  	//
    43  	TransferCallerId *string `json:"TransferCallerId,omitempty"`
    44  }
    45  
    46  func (params *CreateTrunkParams) SetFriendlyName(FriendlyName string) *CreateTrunkParams {
    47  	params.FriendlyName = &FriendlyName
    48  	return params
    49  }
    50  func (params *CreateTrunkParams) SetDomainName(DomainName string) *CreateTrunkParams {
    51  	params.DomainName = &DomainName
    52  	return params
    53  }
    54  func (params *CreateTrunkParams) SetDisasterRecoveryUrl(DisasterRecoveryUrl string) *CreateTrunkParams {
    55  	params.DisasterRecoveryUrl = &DisasterRecoveryUrl
    56  	return params
    57  }
    58  func (params *CreateTrunkParams) SetDisasterRecoveryMethod(DisasterRecoveryMethod string) *CreateTrunkParams {
    59  	params.DisasterRecoveryMethod = &DisasterRecoveryMethod
    60  	return params
    61  }
    62  func (params *CreateTrunkParams) SetTransferMode(TransferMode string) *CreateTrunkParams {
    63  	params.TransferMode = &TransferMode
    64  	return params
    65  }
    66  func (params *CreateTrunkParams) SetSecure(Secure bool) *CreateTrunkParams {
    67  	params.Secure = &Secure
    68  	return params
    69  }
    70  func (params *CreateTrunkParams) SetCnamLookupEnabled(CnamLookupEnabled bool) *CreateTrunkParams {
    71  	params.CnamLookupEnabled = &CnamLookupEnabled
    72  	return params
    73  }
    74  func (params *CreateTrunkParams) SetTransferCallerId(TransferCallerId string) *CreateTrunkParams {
    75  	params.TransferCallerId = &TransferCallerId
    76  	return params
    77  }
    78  
    79  //
    80  func (c *ApiService) CreateTrunk(params *CreateTrunkParams) (*TrunkingV1Trunk, error) {
    81  	path := "/v1/Trunks"
    82  
    83  	data := url.Values{}
    84  	headers := make(map[string]interface{})
    85  
    86  	if params != nil && params.FriendlyName != nil {
    87  		data.Set("FriendlyName", *params.FriendlyName)
    88  	}
    89  	if params != nil && params.DomainName != nil {
    90  		data.Set("DomainName", *params.DomainName)
    91  	}
    92  	if params != nil && params.DisasterRecoveryUrl != nil {
    93  		data.Set("DisasterRecoveryUrl", *params.DisasterRecoveryUrl)
    94  	}
    95  	if params != nil && params.DisasterRecoveryMethod != nil {
    96  		data.Set("DisasterRecoveryMethod", *params.DisasterRecoveryMethod)
    97  	}
    98  	if params != nil && params.TransferMode != nil {
    99  		data.Set("TransferMode", *params.TransferMode)
   100  	}
   101  	if params != nil && params.Secure != nil {
   102  		data.Set("Secure", fmt.Sprint(*params.Secure))
   103  	}
   104  	if params != nil && params.CnamLookupEnabled != nil {
   105  		data.Set("CnamLookupEnabled", fmt.Sprint(*params.CnamLookupEnabled))
   106  	}
   107  	if params != nil && params.TransferCallerId != nil {
   108  		data.Set("TransferCallerId", *params.TransferCallerId)
   109  	}
   110  
   111  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	defer resp.Body.Close()
   117  
   118  	ps := &TrunkingV1Trunk{}
   119  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return ps, err
   124  }
   125  
   126  //
   127  func (c *ApiService) DeleteTrunk(Sid string) error {
   128  	path := "/v1/Trunks/{Sid}"
   129  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   130  
   131  	data := url.Values{}
   132  	headers := make(map[string]interface{})
   133  
   134  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	defer resp.Body.Close()
   140  
   141  	return nil
   142  }
   143  
   144  //
   145  func (c *ApiService) FetchTrunk(Sid string) (*TrunkingV1Trunk, error) {
   146  	path := "/v1/Trunks/{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.Get(c.baseURL+path, data, headers)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	defer resp.Body.Close()
   158  
   159  	ps := &TrunkingV1Trunk{}
   160  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	return ps, err
   165  }
   166  
   167  // Optional parameters for the method 'ListTrunk'
   168  type ListTrunkParams struct {
   169  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   170  	PageSize *int `json:"PageSize,omitempty"`
   171  	// Max number of records to return.
   172  	Limit *int `json:"limit,omitempty"`
   173  }
   174  
   175  func (params *ListTrunkParams) SetPageSize(PageSize int) *ListTrunkParams {
   176  	params.PageSize = &PageSize
   177  	return params
   178  }
   179  func (params *ListTrunkParams) SetLimit(Limit int) *ListTrunkParams {
   180  	params.Limit = &Limit
   181  	return params
   182  }
   183  
   184  // Retrieve a single page of Trunk records from the API. Request is executed immediately.
   185  func (c *ApiService) PageTrunk(params *ListTrunkParams, pageToken, pageNumber string) (*ListTrunkResponse, error) {
   186  	path := "/v1/Trunks"
   187  
   188  	data := url.Values{}
   189  	headers := make(map[string]interface{})
   190  
   191  	if params != nil && params.PageSize != nil {
   192  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   193  	}
   194  
   195  	if pageToken != "" {
   196  		data.Set("PageToken", pageToken)
   197  	}
   198  	if pageNumber != "" {
   199  		data.Set("Page", pageNumber)
   200  	}
   201  
   202  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  
   207  	defer resp.Body.Close()
   208  
   209  	ps := &ListTrunkResponse{}
   210  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   211  		return nil, err
   212  	}
   213  
   214  	return ps, err
   215  }
   216  
   217  // Lists Trunk records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   218  func (c *ApiService) ListTrunk(params *ListTrunkParams) ([]TrunkingV1Trunk, error) {
   219  	response, errors := c.StreamTrunk(params)
   220  
   221  	records := make([]TrunkingV1Trunk, 0)
   222  	for record := range response {
   223  		records = append(records, record)
   224  	}
   225  
   226  	if err := <-errors; err != nil {
   227  		return nil, err
   228  	}
   229  
   230  	return records, nil
   231  }
   232  
   233  // Streams Trunk records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   234  func (c *ApiService) StreamTrunk(params *ListTrunkParams) (chan TrunkingV1Trunk, chan error) {
   235  	if params == nil {
   236  		params = &ListTrunkParams{}
   237  	}
   238  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   239  
   240  	recordChannel := make(chan TrunkingV1Trunk, 1)
   241  	errorChannel := make(chan error, 1)
   242  
   243  	response, err := c.PageTrunk(params, "", "")
   244  	if err != nil {
   245  		errorChannel <- err
   246  		close(recordChannel)
   247  		close(errorChannel)
   248  	} else {
   249  		go c.streamTrunk(response, params, recordChannel, errorChannel)
   250  	}
   251  
   252  	return recordChannel, errorChannel
   253  }
   254  
   255  func (c *ApiService) streamTrunk(response *ListTrunkResponse, params *ListTrunkParams, recordChannel chan TrunkingV1Trunk, errorChannel chan error) {
   256  	curRecord := 1
   257  
   258  	for response != nil {
   259  		responseRecords := response.Trunks
   260  		for item := range responseRecords {
   261  			recordChannel <- responseRecords[item]
   262  			curRecord += 1
   263  			if params.Limit != nil && *params.Limit < curRecord {
   264  				close(recordChannel)
   265  				close(errorChannel)
   266  				return
   267  			}
   268  		}
   269  
   270  		record, err := client.GetNext(c.baseURL, response, c.getNextListTrunkResponse)
   271  		if err != nil {
   272  			errorChannel <- err
   273  			break
   274  		} else if record == nil {
   275  			break
   276  		}
   277  
   278  		response = record.(*ListTrunkResponse)
   279  	}
   280  
   281  	close(recordChannel)
   282  	close(errorChannel)
   283  }
   284  
   285  func (c *ApiService) getNextListTrunkResponse(nextPageUrl string) (interface{}, error) {
   286  	if nextPageUrl == "" {
   287  		return nil, nil
   288  	}
   289  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   290  	if err != nil {
   291  		return nil, err
   292  	}
   293  
   294  	defer resp.Body.Close()
   295  
   296  	ps := &ListTrunkResponse{}
   297  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   298  		return nil, err
   299  	}
   300  	return ps, nil
   301  }
   302  
   303  // Optional parameters for the method 'UpdateTrunk'
   304  type UpdateTrunkParams struct {
   305  	// A descriptive string that you create to describe the resource. It can be up to 64 characters long.
   306  	FriendlyName *string `json:"FriendlyName,omitempty"`
   307  	// The unique address you reserve on Twilio to which you route your SIP traffic. Domain names can contain letters, digits, and `-` and must end with `pstn.twilio.com`. See [Termination Settings](https://www.twilio.com/docs/sip-trunking#termination) for more information.
   308  	DomainName *string `json:"DomainName,omitempty"`
   309  	// The URL we should call using the `disaster_recovery_method` if an error occurs while sending SIP traffic towards the configured Origination URL. We retrieve TwiML from the URL and execute the instructions like any other normal TwiML call. See [Disaster Recovery](https://www.twilio.com/docs/sip-trunking#disaster-recovery) for more information.
   310  	DisasterRecoveryUrl *string `json:"DisasterRecoveryUrl,omitempty"`
   311  	// The HTTP method we should use to call the `disaster_recovery_url`. Can be: `GET` or `POST`.
   312  	DisasterRecoveryMethod *string `json:"DisasterRecoveryMethod,omitempty"`
   313  	//
   314  	TransferMode *string `json:"TransferMode,omitempty"`
   315  	// Whether Secure Trunking is enabled for the trunk. If enabled, all calls going through the trunk will be secure using SRTP for media and TLS for signaling. If disabled, then RTP will be used for media. See [Secure Trunking](https://www.twilio.com/docs/sip-trunking#securetrunking) for more information.
   316  	Secure *bool `json:"Secure,omitempty"`
   317  	// Whether Caller ID Name (CNAM) lookup should be enabled for the trunk. If enabled, all inbound calls to the SIP 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.
   318  	CnamLookupEnabled *bool `json:"CnamLookupEnabled,omitempty"`
   319  	//
   320  	TransferCallerId *string `json:"TransferCallerId,omitempty"`
   321  }
   322  
   323  func (params *UpdateTrunkParams) SetFriendlyName(FriendlyName string) *UpdateTrunkParams {
   324  	params.FriendlyName = &FriendlyName
   325  	return params
   326  }
   327  func (params *UpdateTrunkParams) SetDomainName(DomainName string) *UpdateTrunkParams {
   328  	params.DomainName = &DomainName
   329  	return params
   330  }
   331  func (params *UpdateTrunkParams) SetDisasterRecoveryUrl(DisasterRecoveryUrl string) *UpdateTrunkParams {
   332  	params.DisasterRecoveryUrl = &DisasterRecoveryUrl
   333  	return params
   334  }
   335  func (params *UpdateTrunkParams) SetDisasterRecoveryMethod(DisasterRecoveryMethod string) *UpdateTrunkParams {
   336  	params.DisasterRecoveryMethod = &DisasterRecoveryMethod
   337  	return params
   338  }
   339  func (params *UpdateTrunkParams) SetTransferMode(TransferMode string) *UpdateTrunkParams {
   340  	params.TransferMode = &TransferMode
   341  	return params
   342  }
   343  func (params *UpdateTrunkParams) SetSecure(Secure bool) *UpdateTrunkParams {
   344  	params.Secure = &Secure
   345  	return params
   346  }
   347  func (params *UpdateTrunkParams) SetCnamLookupEnabled(CnamLookupEnabled bool) *UpdateTrunkParams {
   348  	params.CnamLookupEnabled = &CnamLookupEnabled
   349  	return params
   350  }
   351  func (params *UpdateTrunkParams) SetTransferCallerId(TransferCallerId string) *UpdateTrunkParams {
   352  	params.TransferCallerId = &TransferCallerId
   353  	return params
   354  }
   355  
   356  //
   357  func (c *ApiService) UpdateTrunk(Sid string, params *UpdateTrunkParams) (*TrunkingV1Trunk, error) {
   358  	path := "/v1/Trunks/{Sid}"
   359  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   360  
   361  	data := url.Values{}
   362  	headers := make(map[string]interface{})
   363  
   364  	if params != nil && params.FriendlyName != nil {
   365  		data.Set("FriendlyName", *params.FriendlyName)
   366  	}
   367  	if params != nil && params.DomainName != nil {
   368  		data.Set("DomainName", *params.DomainName)
   369  	}
   370  	if params != nil && params.DisasterRecoveryUrl != nil {
   371  		data.Set("DisasterRecoveryUrl", *params.DisasterRecoveryUrl)
   372  	}
   373  	if params != nil && params.DisasterRecoveryMethod != nil {
   374  		data.Set("DisasterRecoveryMethod", *params.DisasterRecoveryMethod)
   375  	}
   376  	if params != nil && params.TransferMode != nil {
   377  		data.Set("TransferMode", *params.TransferMode)
   378  	}
   379  	if params != nil && params.Secure != nil {
   380  		data.Set("Secure", fmt.Sprint(*params.Secure))
   381  	}
   382  	if params != nil && params.CnamLookupEnabled != nil {
   383  		data.Set("CnamLookupEnabled", fmt.Sprint(*params.CnamLookupEnabled))
   384  	}
   385  	if params != nil && params.TransferCallerId != nil {
   386  		data.Set("TransferCallerId", *params.TransferCallerId)
   387  	}
   388  
   389  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   390  	if err != nil {
   391  		return nil, err
   392  	}
   393  
   394  	defer resp.Body.Close()
   395  
   396  	ps := &TrunkingV1Trunk{}
   397  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   398  		return nil, err
   399  	}
   400  
   401  	return ps, err
   402  }