github.com/twilio/twilio-go@v1.20.1/rest/numbers/v2/hosted_number_orders.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Numbers
     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 'CreateHostedNumberOrder'
    27  type CreateHostedNumberOrderParams struct {
    28  	// The number to host in [+E.164](https://en.wikipedia.org/wiki/E.164) format
    29  	PhoneNumber *string `json:"PhoneNumber,omitempty"`
    30  	// The contact phone number of the person authorized to sign the Authorization Document.
    31  	ContactPhoneNumber *string `json:"ContactPhoneNumber,omitempty"`
    32  	// Optional. A 34 character string that uniquely identifies the Address resource that represents the address of the owner of this phone number.
    33  	AddressSid *string `json:"AddressSid,omitempty"`
    34  	// Optional. Email of the owner of this phone number that is being hosted.
    35  	Email *string `json:"Email,omitempty"`
    36  	// This defaults to the AccountSid of the authorization the user is using. This can be provided to specify a subaccount to add the HostedNumberOrder to.
    37  	AccountSid *string `json:"AccountSid,omitempty"`
    38  	// A 128 character string that is a human readable text that describes this resource.
    39  	FriendlyName *string `json:"FriendlyName,omitempty"`
    40  	// Optional. A list of emails that the LOA document for this HostedNumberOrder will be carbon copied to.
    41  	CcEmails *[]string `json:"CcEmails,omitempty"`
    42  	// The URL that Twilio should request when somebody sends an SMS to the phone number. This will be copied onto the IncomingPhoneNumber resource.
    43  	SmsUrl *string `json:"SmsUrl,omitempty"`
    44  	// The HTTP method that should be used to request the SmsUrl. Must be either `GET` or `POST`.  This will be copied onto the IncomingPhoneNumber resource.
    45  	SmsMethod *string `json:"SmsMethod,omitempty"`
    46  	// A URL that Twilio will request if an error occurs requesting or executing the TwiML defined by SmsUrl. This will be copied onto the IncomingPhoneNumber resource.
    47  	SmsFallbackUrl *string `json:"SmsFallbackUrl,omitempty"`
    48  	// Used to specify that the SMS capability will be hosted on Twilio's platform.
    49  	SmsCapability *bool `json:"SmsCapability,omitempty"`
    50  	// The HTTP method that should be used to request the SmsFallbackUrl. Must be either `GET` or `POST`. This will be copied onto the IncomingPhoneNumber resource.
    51  	SmsFallbackMethod *string `json:"SmsFallbackMethod,omitempty"`
    52  	// Optional. The Status Callback URL attached to the IncomingPhoneNumber resource.
    53  	StatusCallbackUrl *string `json:"StatusCallbackUrl,omitempty"`
    54  	// Optional. The Status Callback Method attached to the IncomingPhoneNumber resource.
    55  	StatusCallbackMethod *string `json:"StatusCallbackMethod,omitempty"`
    56  	// Optional. The 34 character sid of the application Twilio should use to handle SMS messages sent to this number. If a `SmsApplicationSid` is present, Twilio will ignore all of the SMS urls above and use those set on the application.
    57  	SmsApplicationSid *string `json:"SmsApplicationSid,omitempty"`
    58  	// The title of the person authorized to sign the Authorization Document for this phone number.
    59  	ContactTitle *string `json:"ContactTitle,omitempty"`
    60  }
    61  
    62  func (params *CreateHostedNumberOrderParams) SetPhoneNumber(PhoneNumber string) *CreateHostedNumberOrderParams {
    63  	params.PhoneNumber = &PhoneNumber
    64  	return params
    65  }
    66  func (params *CreateHostedNumberOrderParams) SetContactPhoneNumber(ContactPhoneNumber string) *CreateHostedNumberOrderParams {
    67  	params.ContactPhoneNumber = &ContactPhoneNumber
    68  	return params
    69  }
    70  func (params *CreateHostedNumberOrderParams) SetAddressSid(AddressSid string) *CreateHostedNumberOrderParams {
    71  	params.AddressSid = &AddressSid
    72  	return params
    73  }
    74  func (params *CreateHostedNumberOrderParams) SetEmail(Email string) *CreateHostedNumberOrderParams {
    75  	params.Email = &Email
    76  	return params
    77  }
    78  func (params *CreateHostedNumberOrderParams) SetAccountSid(AccountSid string) *CreateHostedNumberOrderParams {
    79  	params.AccountSid = &AccountSid
    80  	return params
    81  }
    82  func (params *CreateHostedNumberOrderParams) SetFriendlyName(FriendlyName string) *CreateHostedNumberOrderParams {
    83  	params.FriendlyName = &FriendlyName
    84  	return params
    85  }
    86  func (params *CreateHostedNumberOrderParams) SetCcEmails(CcEmails []string) *CreateHostedNumberOrderParams {
    87  	params.CcEmails = &CcEmails
    88  	return params
    89  }
    90  func (params *CreateHostedNumberOrderParams) SetSmsUrl(SmsUrl string) *CreateHostedNumberOrderParams {
    91  	params.SmsUrl = &SmsUrl
    92  	return params
    93  }
    94  func (params *CreateHostedNumberOrderParams) SetSmsMethod(SmsMethod string) *CreateHostedNumberOrderParams {
    95  	params.SmsMethod = &SmsMethod
    96  	return params
    97  }
    98  func (params *CreateHostedNumberOrderParams) SetSmsFallbackUrl(SmsFallbackUrl string) *CreateHostedNumberOrderParams {
    99  	params.SmsFallbackUrl = &SmsFallbackUrl
   100  	return params
   101  }
   102  func (params *CreateHostedNumberOrderParams) SetSmsCapability(SmsCapability bool) *CreateHostedNumberOrderParams {
   103  	params.SmsCapability = &SmsCapability
   104  	return params
   105  }
   106  func (params *CreateHostedNumberOrderParams) SetSmsFallbackMethod(SmsFallbackMethod string) *CreateHostedNumberOrderParams {
   107  	params.SmsFallbackMethod = &SmsFallbackMethod
   108  	return params
   109  }
   110  func (params *CreateHostedNumberOrderParams) SetStatusCallbackUrl(StatusCallbackUrl string) *CreateHostedNumberOrderParams {
   111  	params.StatusCallbackUrl = &StatusCallbackUrl
   112  	return params
   113  }
   114  func (params *CreateHostedNumberOrderParams) SetStatusCallbackMethod(StatusCallbackMethod string) *CreateHostedNumberOrderParams {
   115  	params.StatusCallbackMethod = &StatusCallbackMethod
   116  	return params
   117  }
   118  func (params *CreateHostedNumberOrderParams) SetSmsApplicationSid(SmsApplicationSid string) *CreateHostedNumberOrderParams {
   119  	params.SmsApplicationSid = &SmsApplicationSid
   120  	return params
   121  }
   122  func (params *CreateHostedNumberOrderParams) SetContactTitle(ContactTitle string) *CreateHostedNumberOrderParams {
   123  	params.ContactTitle = &ContactTitle
   124  	return params
   125  }
   126  
   127  // Host a phone number's capability on Twilio's platform.
   128  func (c *ApiService) CreateHostedNumberOrder(params *CreateHostedNumberOrderParams) (*NumbersV2HostedNumberOrder, error) {
   129  	path := "/v2/HostedNumber/Orders"
   130  
   131  	data := url.Values{}
   132  	headers := make(map[string]interface{})
   133  
   134  	if params != nil && params.PhoneNumber != nil {
   135  		data.Set("PhoneNumber", *params.PhoneNumber)
   136  	}
   137  	if params != nil && params.ContactPhoneNumber != nil {
   138  		data.Set("ContactPhoneNumber", *params.ContactPhoneNumber)
   139  	}
   140  	if params != nil && params.AddressSid != nil {
   141  		data.Set("AddressSid", *params.AddressSid)
   142  	}
   143  	if params != nil && params.Email != nil {
   144  		data.Set("Email", *params.Email)
   145  	}
   146  	if params != nil && params.AccountSid != nil {
   147  		data.Set("AccountSid", *params.AccountSid)
   148  	}
   149  	if params != nil && params.FriendlyName != nil {
   150  		data.Set("FriendlyName", *params.FriendlyName)
   151  	}
   152  	if params != nil && params.CcEmails != nil {
   153  		for _, item := range *params.CcEmails {
   154  			data.Add("CcEmails", item)
   155  		}
   156  	}
   157  	if params != nil && params.SmsUrl != nil {
   158  		data.Set("SmsUrl", *params.SmsUrl)
   159  	}
   160  	if params != nil && params.SmsMethod != nil {
   161  		data.Set("SmsMethod", *params.SmsMethod)
   162  	}
   163  	if params != nil && params.SmsFallbackUrl != nil {
   164  		data.Set("SmsFallbackUrl", *params.SmsFallbackUrl)
   165  	}
   166  	if params != nil && params.SmsCapability != nil {
   167  		data.Set("SmsCapability", fmt.Sprint(*params.SmsCapability))
   168  	}
   169  	if params != nil && params.SmsFallbackMethod != nil {
   170  		data.Set("SmsFallbackMethod", *params.SmsFallbackMethod)
   171  	}
   172  	if params != nil && params.StatusCallbackUrl != nil {
   173  		data.Set("StatusCallbackUrl", *params.StatusCallbackUrl)
   174  	}
   175  	if params != nil && params.StatusCallbackMethod != nil {
   176  		data.Set("StatusCallbackMethod", *params.StatusCallbackMethod)
   177  	}
   178  	if params != nil && params.SmsApplicationSid != nil {
   179  		data.Set("SmsApplicationSid", *params.SmsApplicationSid)
   180  	}
   181  	if params != nil && params.ContactTitle != nil {
   182  		data.Set("ContactTitle", *params.ContactTitle)
   183  	}
   184  
   185  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	defer resp.Body.Close()
   191  
   192  	ps := &NumbersV2HostedNumberOrder{}
   193  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   194  		return nil, err
   195  	}
   196  
   197  	return ps, err
   198  }
   199  
   200  // Cancel the HostedNumberOrder (only available when the status is in `received`).
   201  func (c *ApiService) DeleteHostedNumberOrder(Sid string) error {
   202  	path := "/v2/HostedNumber/Orders/{Sid}"
   203  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   204  
   205  	data := url.Values{}
   206  	headers := make(map[string]interface{})
   207  
   208  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   209  	if err != nil {
   210  		return err
   211  	}
   212  
   213  	defer resp.Body.Close()
   214  
   215  	return nil
   216  }
   217  
   218  // Fetch a specific HostedNumberOrder.
   219  func (c *ApiService) FetchHostedNumberOrder(Sid string) (*NumbersV2HostedNumberOrder, error) {
   220  	path := "/v2/HostedNumber/Orders/{Sid}"
   221  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   222  
   223  	data := url.Values{}
   224  	headers := make(map[string]interface{})
   225  
   226  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   227  	if err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	defer resp.Body.Close()
   232  
   233  	ps := &NumbersV2HostedNumberOrder{}
   234  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   235  		return nil, err
   236  	}
   237  
   238  	return ps, err
   239  }
   240  
   241  // Optional parameters for the method 'ListHostedNumberOrder'
   242  type ListHostedNumberOrderParams struct {
   243  	// The Status of this HostedNumberOrder. One of `received`, `pending-verification`, `verified`, `pending-loa`, `carrier-processing`, `testing`, `completed`, `failed`, or `action-required`.
   244  	Status *string `json:"Status,omitempty"`
   245  	// Whether the SMS capability will be hosted on our platform. Can be `true` of `false`.
   246  	SmsCapability *bool `json:"SmsCapability,omitempty"`
   247  	// An E164 formatted phone number hosted by this HostedNumberOrder.
   248  	PhoneNumber *string `json:"PhoneNumber,omitempty"`
   249  	// A 34 character string that uniquely identifies the IncomingPhoneNumber resource created by this HostedNumberOrder.
   250  	IncomingPhoneNumberSid *string `json:"IncomingPhoneNumberSid,omitempty"`
   251  	// A human readable description of this resource, up to 128 characters.
   252  	FriendlyName *string `json:"FriendlyName,omitempty"`
   253  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   254  	PageSize *int `json:"PageSize,omitempty"`
   255  	// Max number of records to return.
   256  	Limit *int `json:"limit,omitempty"`
   257  }
   258  
   259  func (params *ListHostedNumberOrderParams) SetStatus(Status string) *ListHostedNumberOrderParams {
   260  	params.Status = &Status
   261  	return params
   262  }
   263  func (params *ListHostedNumberOrderParams) SetSmsCapability(SmsCapability bool) *ListHostedNumberOrderParams {
   264  	params.SmsCapability = &SmsCapability
   265  	return params
   266  }
   267  func (params *ListHostedNumberOrderParams) SetPhoneNumber(PhoneNumber string) *ListHostedNumberOrderParams {
   268  	params.PhoneNumber = &PhoneNumber
   269  	return params
   270  }
   271  func (params *ListHostedNumberOrderParams) SetIncomingPhoneNumberSid(IncomingPhoneNumberSid string) *ListHostedNumberOrderParams {
   272  	params.IncomingPhoneNumberSid = &IncomingPhoneNumberSid
   273  	return params
   274  }
   275  func (params *ListHostedNumberOrderParams) SetFriendlyName(FriendlyName string) *ListHostedNumberOrderParams {
   276  	params.FriendlyName = &FriendlyName
   277  	return params
   278  }
   279  func (params *ListHostedNumberOrderParams) SetPageSize(PageSize int) *ListHostedNumberOrderParams {
   280  	params.PageSize = &PageSize
   281  	return params
   282  }
   283  func (params *ListHostedNumberOrderParams) SetLimit(Limit int) *ListHostedNumberOrderParams {
   284  	params.Limit = &Limit
   285  	return params
   286  }
   287  
   288  // Retrieve a single page of HostedNumberOrder records from the API. Request is executed immediately.
   289  func (c *ApiService) PageHostedNumberOrder(params *ListHostedNumberOrderParams, pageToken, pageNumber string) (*ListHostedNumberOrderResponse, error) {
   290  	path := "/v2/HostedNumber/Orders"
   291  
   292  	data := url.Values{}
   293  	headers := make(map[string]interface{})
   294  
   295  	if params != nil && params.Status != nil {
   296  		data.Set("Status", *params.Status)
   297  	}
   298  	if params != nil && params.SmsCapability != nil {
   299  		data.Set("SmsCapability", fmt.Sprint(*params.SmsCapability))
   300  	}
   301  	if params != nil && params.PhoneNumber != nil {
   302  		data.Set("PhoneNumber", *params.PhoneNumber)
   303  	}
   304  	if params != nil && params.IncomingPhoneNumberSid != nil {
   305  		data.Set("IncomingPhoneNumberSid", *params.IncomingPhoneNumberSid)
   306  	}
   307  	if params != nil && params.FriendlyName != nil {
   308  		data.Set("FriendlyName", *params.FriendlyName)
   309  	}
   310  	if params != nil && params.PageSize != nil {
   311  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   312  	}
   313  
   314  	if pageToken != "" {
   315  		data.Set("PageToken", pageToken)
   316  	}
   317  	if pageNumber != "" {
   318  		data.Set("Page", pageNumber)
   319  	}
   320  
   321  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   322  	if err != nil {
   323  		return nil, err
   324  	}
   325  
   326  	defer resp.Body.Close()
   327  
   328  	ps := &ListHostedNumberOrderResponse{}
   329  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   330  		return nil, err
   331  	}
   332  
   333  	return ps, err
   334  }
   335  
   336  // Lists HostedNumberOrder records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   337  func (c *ApiService) ListHostedNumberOrder(params *ListHostedNumberOrderParams) ([]NumbersV2HostedNumberOrder, error) {
   338  	response, errors := c.StreamHostedNumberOrder(params)
   339  
   340  	records := make([]NumbersV2HostedNumberOrder, 0)
   341  	for record := range response {
   342  		records = append(records, record)
   343  	}
   344  
   345  	if err := <-errors; err != nil {
   346  		return nil, err
   347  	}
   348  
   349  	return records, nil
   350  }
   351  
   352  // Streams HostedNumberOrder records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   353  func (c *ApiService) StreamHostedNumberOrder(params *ListHostedNumberOrderParams) (chan NumbersV2HostedNumberOrder, chan error) {
   354  	if params == nil {
   355  		params = &ListHostedNumberOrderParams{}
   356  	}
   357  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   358  
   359  	recordChannel := make(chan NumbersV2HostedNumberOrder, 1)
   360  	errorChannel := make(chan error, 1)
   361  
   362  	response, err := c.PageHostedNumberOrder(params, "", "")
   363  	if err != nil {
   364  		errorChannel <- err
   365  		close(recordChannel)
   366  		close(errorChannel)
   367  	} else {
   368  		go c.streamHostedNumberOrder(response, params, recordChannel, errorChannel)
   369  	}
   370  
   371  	return recordChannel, errorChannel
   372  }
   373  
   374  func (c *ApiService) streamHostedNumberOrder(response *ListHostedNumberOrderResponse, params *ListHostedNumberOrderParams, recordChannel chan NumbersV2HostedNumberOrder, errorChannel chan error) {
   375  	curRecord := 1
   376  
   377  	for response != nil {
   378  		responseRecords := response.Items
   379  		for item := range responseRecords {
   380  			recordChannel <- responseRecords[item]
   381  			curRecord += 1
   382  			if params.Limit != nil && *params.Limit < curRecord {
   383  				close(recordChannel)
   384  				close(errorChannel)
   385  				return
   386  			}
   387  		}
   388  
   389  		record, err := client.GetNext(c.baseURL, response, c.getNextListHostedNumberOrderResponse)
   390  		if err != nil {
   391  			errorChannel <- err
   392  			break
   393  		} else if record == nil {
   394  			break
   395  		}
   396  
   397  		response = record.(*ListHostedNumberOrderResponse)
   398  	}
   399  
   400  	close(recordChannel)
   401  	close(errorChannel)
   402  }
   403  
   404  func (c *ApiService) getNextListHostedNumberOrderResponse(nextPageUrl string) (interface{}, error) {
   405  	if nextPageUrl == "" {
   406  		return nil, nil
   407  	}
   408  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   409  	if err != nil {
   410  		return nil, err
   411  	}
   412  
   413  	defer resp.Body.Close()
   414  
   415  	ps := &ListHostedNumberOrderResponse{}
   416  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   417  		return nil, err
   418  	}
   419  	return ps, nil
   420  }