github.com/twilio/twilio-go@v1.20.1/rest/proxy/v1/services_phone_numbers.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Proxy
     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 'CreatePhoneNumber'
    27  type CreatePhoneNumberParams struct {
    28  	// The SID of a Twilio [IncomingPhoneNumber](https://www.twilio.com/docs/phone-numbers/api/incomingphonenumber-resource) resource that represents the Twilio Number you would like to assign to your Proxy Service.
    29  	Sid *string `json:"Sid,omitempty"`
    30  	// The phone number in [E.164](https://www.twilio.com/docs/glossary/what-e164) format.  E.164 phone numbers consist of a + followed by the country code and subscriber number without punctuation characters. For example, +14155551234.
    31  	PhoneNumber *string `json:"PhoneNumber,omitempty"`
    32  	// Whether the new phone number should be reserved and not be assigned to a participant using proxy pool logic. See [Reserved Phone Numbers](https://www.twilio.com/docs/proxy/reserved-phone-numbers) for more information.
    33  	IsReserved *bool `json:"IsReserved,omitempty"`
    34  }
    35  
    36  func (params *CreatePhoneNumberParams) SetSid(Sid string) *CreatePhoneNumberParams {
    37  	params.Sid = &Sid
    38  	return params
    39  }
    40  func (params *CreatePhoneNumberParams) SetPhoneNumber(PhoneNumber string) *CreatePhoneNumberParams {
    41  	params.PhoneNumber = &PhoneNumber
    42  	return params
    43  }
    44  func (params *CreatePhoneNumberParams) SetIsReserved(IsReserved bool) *CreatePhoneNumberParams {
    45  	params.IsReserved = &IsReserved
    46  	return params
    47  }
    48  
    49  // Add a Phone Number to a Service's Proxy Number Pool.
    50  func (c *ApiService) CreatePhoneNumber(ServiceSid string, params *CreatePhoneNumberParams) (*ProxyV1PhoneNumber, error) {
    51  	path := "/v1/Services/{ServiceSid}/PhoneNumbers"
    52  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	if params != nil && params.Sid != nil {
    58  		data.Set("Sid", *params.Sid)
    59  	}
    60  	if params != nil && params.PhoneNumber != nil {
    61  		data.Set("PhoneNumber", *params.PhoneNumber)
    62  	}
    63  	if params != nil && params.IsReserved != nil {
    64  		data.Set("IsReserved", fmt.Sprint(*params.IsReserved))
    65  	}
    66  
    67  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	defer resp.Body.Close()
    73  
    74  	ps := &ProxyV1PhoneNumber{}
    75  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return ps, err
    80  }
    81  
    82  // Delete a specific Phone Number from a Service.
    83  func (c *ApiService) DeletePhoneNumber(ServiceSid string, Sid string) error {
    84  	path := "/v1/Services/{ServiceSid}/PhoneNumbers/{Sid}"
    85  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    86  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    87  
    88  	data := url.Values{}
    89  	headers := make(map[string]interface{})
    90  
    91  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	defer resp.Body.Close()
    97  
    98  	return nil
    99  }
   100  
   101  // Fetch a specific Phone Number.
   102  func (c *ApiService) FetchPhoneNumber(ServiceSid string, Sid string) (*ProxyV1PhoneNumber, error) {
   103  	path := "/v1/Services/{ServiceSid}/PhoneNumbers/{Sid}"
   104  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   105  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   106  
   107  	data := url.Values{}
   108  	headers := make(map[string]interface{})
   109  
   110  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	defer resp.Body.Close()
   116  
   117  	ps := &ProxyV1PhoneNumber{}
   118  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return ps, err
   123  }
   124  
   125  // Optional parameters for the method 'ListPhoneNumber'
   126  type ListPhoneNumberParams struct {
   127  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   128  	PageSize *int `json:"PageSize,omitempty"`
   129  	// Max number of records to return.
   130  	Limit *int `json:"limit,omitempty"`
   131  }
   132  
   133  func (params *ListPhoneNumberParams) SetPageSize(PageSize int) *ListPhoneNumberParams {
   134  	params.PageSize = &PageSize
   135  	return params
   136  }
   137  func (params *ListPhoneNumberParams) SetLimit(Limit int) *ListPhoneNumberParams {
   138  	params.Limit = &Limit
   139  	return params
   140  }
   141  
   142  // Retrieve a single page of PhoneNumber records from the API. Request is executed immediately.
   143  func (c *ApiService) PagePhoneNumber(ServiceSid string, params *ListPhoneNumberParams, pageToken, pageNumber string) (*ListPhoneNumberResponse, error) {
   144  	path := "/v1/Services/{ServiceSid}/PhoneNumbers"
   145  
   146  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   147  
   148  	data := url.Values{}
   149  	headers := make(map[string]interface{})
   150  
   151  	if params != nil && params.PageSize != nil {
   152  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   153  	}
   154  
   155  	if pageToken != "" {
   156  		data.Set("PageToken", pageToken)
   157  	}
   158  	if pageNumber != "" {
   159  		data.Set("Page", pageNumber)
   160  	}
   161  
   162  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	defer resp.Body.Close()
   168  
   169  	ps := &ListPhoneNumberResponse{}
   170  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	return ps, err
   175  }
   176  
   177  // Lists PhoneNumber records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   178  func (c *ApiService) ListPhoneNumber(ServiceSid string, params *ListPhoneNumberParams) ([]ProxyV1PhoneNumber, error) {
   179  	response, errors := c.StreamPhoneNumber(ServiceSid, params)
   180  
   181  	records := make([]ProxyV1PhoneNumber, 0)
   182  	for record := range response {
   183  		records = append(records, record)
   184  	}
   185  
   186  	if err := <-errors; err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	return records, nil
   191  }
   192  
   193  // Streams PhoneNumber records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   194  func (c *ApiService) StreamPhoneNumber(ServiceSid string, params *ListPhoneNumberParams) (chan ProxyV1PhoneNumber, chan error) {
   195  	if params == nil {
   196  		params = &ListPhoneNumberParams{}
   197  	}
   198  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   199  
   200  	recordChannel := make(chan ProxyV1PhoneNumber, 1)
   201  	errorChannel := make(chan error, 1)
   202  
   203  	response, err := c.PagePhoneNumber(ServiceSid, params, "", "")
   204  	if err != nil {
   205  		errorChannel <- err
   206  		close(recordChannel)
   207  		close(errorChannel)
   208  	} else {
   209  		go c.streamPhoneNumber(response, params, recordChannel, errorChannel)
   210  	}
   211  
   212  	return recordChannel, errorChannel
   213  }
   214  
   215  func (c *ApiService) streamPhoneNumber(response *ListPhoneNumberResponse, params *ListPhoneNumberParams, recordChannel chan ProxyV1PhoneNumber, errorChannel chan error) {
   216  	curRecord := 1
   217  
   218  	for response != nil {
   219  		responseRecords := response.PhoneNumbers
   220  		for item := range responseRecords {
   221  			recordChannel <- responseRecords[item]
   222  			curRecord += 1
   223  			if params.Limit != nil && *params.Limit < curRecord {
   224  				close(recordChannel)
   225  				close(errorChannel)
   226  				return
   227  			}
   228  		}
   229  
   230  		record, err := client.GetNext(c.baseURL, response, c.getNextListPhoneNumberResponse)
   231  		if err != nil {
   232  			errorChannel <- err
   233  			break
   234  		} else if record == nil {
   235  			break
   236  		}
   237  
   238  		response = record.(*ListPhoneNumberResponse)
   239  	}
   240  
   241  	close(recordChannel)
   242  	close(errorChannel)
   243  }
   244  
   245  func (c *ApiService) getNextListPhoneNumberResponse(nextPageUrl string) (interface{}, error) {
   246  	if nextPageUrl == "" {
   247  		return nil, nil
   248  	}
   249  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	defer resp.Body.Close()
   255  
   256  	ps := &ListPhoneNumberResponse{}
   257  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   258  		return nil, err
   259  	}
   260  	return ps, nil
   261  }
   262  
   263  // Optional parameters for the method 'UpdatePhoneNumber'
   264  type UpdatePhoneNumberParams struct {
   265  	// Whether the phone number should be reserved and not be assigned to a participant using proxy pool logic. See [Reserved Phone Numbers](https://www.twilio.com/docs/proxy/reserved-phone-numbers) for more information.
   266  	IsReserved *bool `json:"IsReserved,omitempty"`
   267  }
   268  
   269  func (params *UpdatePhoneNumberParams) SetIsReserved(IsReserved bool) *UpdatePhoneNumberParams {
   270  	params.IsReserved = &IsReserved
   271  	return params
   272  }
   273  
   274  // Update a specific Proxy Number.
   275  func (c *ApiService) UpdatePhoneNumber(ServiceSid string, Sid string, params *UpdatePhoneNumberParams) (*ProxyV1PhoneNumber, error) {
   276  	path := "/v1/Services/{ServiceSid}/PhoneNumbers/{Sid}"
   277  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   278  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   279  
   280  	data := url.Values{}
   281  	headers := make(map[string]interface{})
   282  
   283  	if params != nil && params.IsReserved != nil {
   284  		data.Set("IsReserved", fmt.Sprint(*params.IsReserved))
   285  	}
   286  
   287  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   288  	if err != nil {
   289  		return nil, err
   290  	}
   291  
   292  	defer resp.Body.Close()
   293  
   294  	ps := &ProxyV1PhoneNumber{}
   295  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   296  		return nil, err
   297  	}
   298  
   299  	return ps, err
   300  }