github.com/twilio/twilio-go@v1.20.1/rest/voice/v1/ip_records.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 'CreateIpRecord'
    27  type CreateIpRecordParams struct {
    28  	// An IP address in dotted decimal notation, IPv4 only.
    29  	IpAddress *string `json:"IpAddress,omitempty"`
    30  	// A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long.
    31  	FriendlyName *string `json:"FriendlyName,omitempty"`
    32  	// An integer representing the length of the [CIDR](https://tools.ietf.org/html/rfc4632) prefix to use with this IP address. By default the entire IP address is used, which for IPv4 is value 32.
    33  	CidrPrefixLength *int `json:"CidrPrefixLength,omitempty"`
    34  }
    35  
    36  func (params *CreateIpRecordParams) SetIpAddress(IpAddress string) *CreateIpRecordParams {
    37  	params.IpAddress = &IpAddress
    38  	return params
    39  }
    40  func (params *CreateIpRecordParams) SetFriendlyName(FriendlyName string) *CreateIpRecordParams {
    41  	params.FriendlyName = &FriendlyName
    42  	return params
    43  }
    44  func (params *CreateIpRecordParams) SetCidrPrefixLength(CidrPrefixLength int) *CreateIpRecordParams {
    45  	params.CidrPrefixLength = &CidrPrefixLength
    46  	return params
    47  }
    48  
    49  //
    50  func (c *ApiService) CreateIpRecord(params *CreateIpRecordParams) (*VoiceV1IpRecord, error) {
    51  	path := "/v1/IpRecords"
    52  
    53  	data := url.Values{}
    54  	headers := make(map[string]interface{})
    55  
    56  	if params != nil && params.IpAddress != nil {
    57  		data.Set("IpAddress", *params.IpAddress)
    58  	}
    59  	if params != nil && params.FriendlyName != nil {
    60  		data.Set("FriendlyName", *params.FriendlyName)
    61  	}
    62  	if params != nil && params.CidrPrefixLength != nil {
    63  		data.Set("CidrPrefixLength", fmt.Sprint(*params.CidrPrefixLength))
    64  	}
    65  
    66  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	defer resp.Body.Close()
    72  
    73  	ps := &VoiceV1IpRecord{}
    74  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return ps, err
    79  }
    80  
    81  //
    82  func (c *ApiService) DeleteIpRecord(Sid string) error {
    83  	path := "/v1/IpRecords/{Sid}"
    84  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    85  
    86  	data := url.Values{}
    87  	headers := make(map[string]interface{})
    88  
    89  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    90  	if err != nil {
    91  		return err
    92  	}
    93  
    94  	defer resp.Body.Close()
    95  
    96  	return nil
    97  }
    98  
    99  //
   100  func (c *ApiService) FetchIpRecord(Sid string) (*VoiceV1IpRecord, error) {
   101  	path := "/v1/IpRecords/{Sid}"
   102  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   103  
   104  	data := url.Values{}
   105  	headers := make(map[string]interface{})
   106  
   107  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	defer resp.Body.Close()
   113  
   114  	ps := &VoiceV1IpRecord{}
   115  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	return ps, err
   120  }
   121  
   122  // Optional parameters for the method 'ListIpRecord'
   123  type ListIpRecordParams struct {
   124  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   125  	PageSize *int `json:"PageSize,omitempty"`
   126  	// Max number of records to return.
   127  	Limit *int `json:"limit,omitempty"`
   128  }
   129  
   130  func (params *ListIpRecordParams) SetPageSize(PageSize int) *ListIpRecordParams {
   131  	params.PageSize = &PageSize
   132  	return params
   133  }
   134  func (params *ListIpRecordParams) SetLimit(Limit int) *ListIpRecordParams {
   135  	params.Limit = &Limit
   136  	return params
   137  }
   138  
   139  // Retrieve a single page of IpRecord records from the API. Request is executed immediately.
   140  func (c *ApiService) PageIpRecord(params *ListIpRecordParams, pageToken, pageNumber string) (*ListIpRecordResponse, error) {
   141  	path := "/v1/IpRecords"
   142  
   143  	data := url.Values{}
   144  	headers := make(map[string]interface{})
   145  
   146  	if params != nil && params.PageSize != nil {
   147  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   148  	}
   149  
   150  	if pageToken != "" {
   151  		data.Set("PageToken", pageToken)
   152  	}
   153  	if pageNumber != "" {
   154  		data.Set("Page", pageNumber)
   155  	}
   156  
   157  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	defer resp.Body.Close()
   163  
   164  	ps := &ListIpRecordResponse{}
   165  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	return ps, err
   170  }
   171  
   172  // Lists IpRecord records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   173  func (c *ApiService) ListIpRecord(params *ListIpRecordParams) ([]VoiceV1IpRecord, error) {
   174  	response, errors := c.StreamIpRecord(params)
   175  
   176  	records := make([]VoiceV1IpRecord, 0)
   177  	for record := range response {
   178  		records = append(records, record)
   179  	}
   180  
   181  	if err := <-errors; err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return records, nil
   186  }
   187  
   188  // Streams IpRecord records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   189  func (c *ApiService) StreamIpRecord(params *ListIpRecordParams) (chan VoiceV1IpRecord, chan error) {
   190  	if params == nil {
   191  		params = &ListIpRecordParams{}
   192  	}
   193  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   194  
   195  	recordChannel := make(chan VoiceV1IpRecord, 1)
   196  	errorChannel := make(chan error, 1)
   197  
   198  	response, err := c.PageIpRecord(params, "", "")
   199  	if err != nil {
   200  		errorChannel <- err
   201  		close(recordChannel)
   202  		close(errorChannel)
   203  	} else {
   204  		go c.streamIpRecord(response, params, recordChannel, errorChannel)
   205  	}
   206  
   207  	return recordChannel, errorChannel
   208  }
   209  
   210  func (c *ApiService) streamIpRecord(response *ListIpRecordResponse, params *ListIpRecordParams, recordChannel chan VoiceV1IpRecord, errorChannel chan error) {
   211  	curRecord := 1
   212  
   213  	for response != nil {
   214  		responseRecords := response.IpRecords
   215  		for item := range responseRecords {
   216  			recordChannel <- responseRecords[item]
   217  			curRecord += 1
   218  			if params.Limit != nil && *params.Limit < curRecord {
   219  				close(recordChannel)
   220  				close(errorChannel)
   221  				return
   222  			}
   223  		}
   224  
   225  		record, err := client.GetNext(c.baseURL, response, c.getNextListIpRecordResponse)
   226  		if err != nil {
   227  			errorChannel <- err
   228  			break
   229  		} else if record == nil {
   230  			break
   231  		}
   232  
   233  		response = record.(*ListIpRecordResponse)
   234  	}
   235  
   236  	close(recordChannel)
   237  	close(errorChannel)
   238  }
   239  
   240  func (c *ApiService) getNextListIpRecordResponse(nextPageUrl string) (interface{}, error) {
   241  	if nextPageUrl == "" {
   242  		return nil, nil
   243  	}
   244  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	defer resp.Body.Close()
   250  
   251  	ps := &ListIpRecordResponse{}
   252  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   253  		return nil, err
   254  	}
   255  	return ps, nil
   256  }
   257  
   258  // Optional parameters for the method 'UpdateIpRecord'
   259  type UpdateIpRecordParams struct {
   260  	// A descriptive string that you create to describe the resource. It is not unique and can be up to 255 characters long.
   261  	FriendlyName *string `json:"FriendlyName,omitempty"`
   262  }
   263  
   264  func (params *UpdateIpRecordParams) SetFriendlyName(FriendlyName string) *UpdateIpRecordParams {
   265  	params.FriendlyName = &FriendlyName
   266  	return params
   267  }
   268  
   269  //
   270  func (c *ApiService) UpdateIpRecord(Sid string, params *UpdateIpRecordParams) (*VoiceV1IpRecord, error) {
   271  	path := "/v1/IpRecords/{Sid}"
   272  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   273  
   274  	data := url.Values{}
   275  	headers := make(map[string]interface{})
   276  
   277  	if params != nil && params.FriendlyName != nil {
   278  		data.Set("FriendlyName", *params.FriendlyName)
   279  	}
   280  
   281  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   282  	if err != nil {
   283  		return nil, err
   284  	}
   285  
   286  	defer resp.Body.Close()
   287  
   288  	ps := &VoiceV1IpRecord{}
   289  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   290  		return nil, err
   291  	}
   292  
   293  	return ps, err
   294  }