github.com/twilio/twilio-go@v1.20.1/rest/numbers/v2/hosted_number_authorization_documents.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 'CreateAuthorizationDocument'
    27  type CreateAuthorizationDocumentParams struct {
    28  	// A 34 character string that uniquely identifies the Address resource that is associated with this AuthorizationDocument.
    29  	AddressSid *string `json:"AddressSid,omitempty"`
    30  	// Email that this AuthorizationDocument will be sent to for signing.
    31  	Email *string `json:"Email,omitempty"`
    32  	// The contact phone number of the person authorized to sign the Authorization Document.
    33  	ContactPhoneNumber *string `json:"ContactPhoneNumber,omitempty"`
    34  	// A list of HostedNumberOrder sids that this AuthorizationDocument will authorize for hosting phone number capabilities on Twilio's platform.
    35  	HostedNumberOrderSids *[]string `json:"HostedNumberOrderSids,omitempty"`
    36  	// The title of the person authorized to sign the Authorization Document for this phone number.
    37  	ContactTitle *string `json:"ContactTitle,omitempty"`
    38  	// Email recipients who will be informed when an Authorization Document has been sent and signed.
    39  	CcEmails *[]string `json:"CcEmails,omitempty"`
    40  }
    41  
    42  func (params *CreateAuthorizationDocumentParams) SetAddressSid(AddressSid string) *CreateAuthorizationDocumentParams {
    43  	params.AddressSid = &AddressSid
    44  	return params
    45  }
    46  func (params *CreateAuthorizationDocumentParams) SetEmail(Email string) *CreateAuthorizationDocumentParams {
    47  	params.Email = &Email
    48  	return params
    49  }
    50  func (params *CreateAuthorizationDocumentParams) SetContactPhoneNumber(ContactPhoneNumber string) *CreateAuthorizationDocumentParams {
    51  	params.ContactPhoneNumber = &ContactPhoneNumber
    52  	return params
    53  }
    54  func (params *CreateAuthorizationDocumentParams) SetHostedNumberOrderSids(HostedNumberOrderSids []string) *CreateAuthorizationDocumentParams {
    55  	params.HostedNumberOrderSids = &HostedNumberOrderSids
    56  	return params
    57  }
    58  func (params *CreateAuthorizationDocumentParams) SetContactTitle(ContactTitle string) *CreateAuthorizationDocumentParams {
    59  	params.ContactTitle = &ContactTitle
    60  	return params
    61  }
    62  func (params *CreateAuthorizationDocumentParams) SetCcEmails(CcEmails []string) *CreateAuthorizationDocumentParams {
    63  	params.CcEmails = &CcEmails
    64  	return params
    65  }
    66  
    67  // Create an AuthorizationDocument for authorizing the hosting of phone number capabilities on Twilio's platform.
    68  func (c *ApiService) CreateAuthorizationDocument(params *CreateAuthorizationDocumentParams) (*NumbersV2AuthorizationDocument, error) {
    69  	path := "/v2/HostedNumber/AuthorizationDocuments"
    70  
    71  	data := url.Values{}
    72  	headers := make(map[string]interface{})
    73  
    74  	if params != nil && params.AddressSid != nil {
    75  		data.Set("AddressSid", *params.AddressSid)
    76  	}
    77  	if params != nil && params.Email != nil {
    78  		data.Set("Email", *params.Email)
    79  	}
    80  	if params != nil && params.ContactPhoneNumber != nil {
    81  		data.Set("ContactPhoneNumber", *params.ContactPhoneNumber)
    82  	}
    83  	if params != nil && params.HostedNumberOrderSids != nil {
    84  		for _, item := range *params.HostedNumberOrderSids {
    85  			data.Add("HostedNumberOrderSids", item)
    86  		}
    87  	}
    88  	if params != nil && params.ContactTitle != nil {
    89  		data.Set("ContactTitle", *params.ContactTitle)
    90  	}
    91  	if params != nil && params.CcEmails != nil {
    92  		for _, item := range *params.CcEmails {
    93  			data.Add("CcEmails", item)
    94  		}
    95  	}
    96  
    97  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	defer resp.Body.Close()
   103  
   104  	ps := &NumbersV2AuthorizationDocument{}
   105  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	return ps, err
   110  }
   111  
   112  // Cancel the AuthorizationDocument request.
   113  func (c *ApiService) DeleteAuthorizationDocument(Sid string) error {
   114  	path := "/v2/HostedNumber/AuthorizationDocuments/{Sid}"
   115  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   116  
   117  	data := url.Values{}
   118  	headers := make(map[string]interface{})
   119  
   120  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   121  	if err != nil {
   122  		return err
   123  	}
   124  
   125  	defer resp.Body.Close()
   126  
   127  	return nil
   128  }
   129  
   130  // Fetch a specific AuthorizationDocument.
   131  func (c *ApiService) FetchAuthorizationDocument(Sid string) (*NumbersV2AuthorizationDocument, error) {
   132  	path := "/v2/HostedNumber/AuthorizationDocuments/{Sid}"
   133  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   134  
   135  	data := url.Values{}
   136  	headers := make(map[string]interface{})
   137  
   138  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	defer resp.Body.Close()
   144  
   145  	ps := &NumbersV2AuthorizationDocument{}
   146  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	return ps, err
   151  }
   152  
   153  // Optional parameters for the method 'ListAuthorizationDocument'
   154  type ListAuthorizationDocumentParams struct {
   155  	// Email that this AuthorizationDocument will be sent to for signing.
   156  	Email *string `json:"Email,omitempty"`
   157  	// Status of an instance resource. It can hold one of the values: 1. opened 2. signing, 3. signed LOA, 4. canceled, 5. failed. See the section entitled [Status Values](https://www.twilio.com/docs/phone-numbers/hosted-numbers/hosted-numbers-api/authorization-document-resource#status-values) for more information on each of these statuses.
   158  	Status *string `json:"Status,omitempty"`
   159  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   160  	PageSize *int `json:"PageSize,omitempty"`
   161  	// Max number of records to return.
   162  	Limit *int `json:"limit,omitempty"`
   163  }
   164  
   165  func (params *ListAuthorizationDocumentParams) SetEmail(Email string) *ListAuthorizationDocumentParams {
   166  	params.Email = &Email
   167  	return params
   168  }
   169  func (params *ListAuthorizationDocumentParams) SetStatus(Status string) *ListAuthorizationDocumentParams {
   170  	params.Status = &Status
   171  	return params
   172  }
   173  func (params *ListAuthorizationDocumentParams) SetPageSize(PageSize int) *ListAuthorizationDocumentParams {
   174  	params.PageSize = &PageSize
   175  	return params
   176  }
   177  func (params *ListAuthorizationDocumentParams) SetLimit(Limit int) *ListAuthorizationDocumentParams {
   178  	params.Limit = &Limit
   179  	return params
   180  }
   181  
   182  // Retrieve a single page of AuthorizationDocument records from the API. Request is executed immediately.
   183  func (c *ApiService) PageAuthorizationDocument(params *ListAuthorizationDocumentParams, pageToken, pageNumber string) (*ListAuthorizationDocumentResponse, error) {
   184  	path := "/v2/HostedNumber/AuthorizationDocuments"
   185  
   186  	data := url.Values{}
   187  	headers := make(map[string]interface{})
   188  
   189  	if params != nil && params.Email != nil {
   190  		data.Set("Email", *params.Email)
   191  	}
   192  	if params != nil && params.Status != nil {
   193  		data.Set("Status", *params.Status)
   194  	}
   195  	if params != nil && params.PageSize != nil {
   196  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   197  	}
   198  
   199  	if pageToken != "" {
   200  		data.Set("PageToken", pageToken)
   201  	}
   202  	if pageNumber != "" {
   203  		data.Set("Page", pageNumber)
   204  	}
   205  
   206  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   207  	if err != nil {
   208  		return nil, err
   209  	}
   210  
   211  	defer resp.Body.Close()
   212  
   213  	ps := &ListAuthorizationDocumentResponse{}
   214  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   215  		return nil, err
   216  	}
   217  
   218  	return ps, err
   219  }
   220  
   221  // Lists AuthorizationDocument records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   222  func (c *ApiService) ListAuthorizationDocument(params *ListAuthorizationDocumentParams) ([]NumbersV2AuthorizationDocument, error) {
   223  	response, errors := c.StreamAuthorizationDocument(params)
   224  
   225  	records := make([]NumbersV2AuthorizationDocument, 0)
   226  	for record := range response {
   227  		records = append(records, record)
   228  	}
   229  
   230  	if err := <-errors; err != nil {
   231  		return nil, err
   232  	}
   233  
   234  	return records, nil
   235  }
   236  
   237  // Streams AuthorizationDocument records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   238  func (c *ApiService) StreamAuthorizationDocument(params *ListAuthorizationDocumentParams) (chan NumbersV2AuthorizationDocument, chan error) {
   239  	if params == nil {
   240  		params = &ListAuthorizationDocumentParams{}
   241  	}
   242  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   243  
   244  	recordChannel := make(chan NumbersV2AuthorizationDocument, 1)
   245  	errorChannel := make(chan error, 1)
   246  
   247  	response, err := c.PageAuthorizationDocument(params, "", "")
   248  	if err != nil {
   249  		errorChannel <- err
   250  		close(recordChannel)
   251  		close(errorChannel)
   252  	} else {
   253  		go c.streamAuthorizationDocument(response, params, recordChannel, errorChannel)
   254  	}
   255  
   256  	return recordChannel, errorChannel
   257  }
   258  
   259  func (c *ApiService) streamAuthorizationDocument(response *ListAuthorizationDocumentResponse, params *ListAuthorizationDocumentParams, recordChannel chan NumbersV2AuthorizationDocument, errorChannel chan error) {
   260  	curRecord := 1
   261  
   262  	for response != nil {
   263  		responseRecords := response.Items
   264  		for item := range responseRecords {
   265  			recordChannel <- responseRecords[item]
   266  			curRecord += 1
   267  			if params.Limit != nil && *params.Limit < curRecord {
   268  				close(recordChannel)
   269  				close(errorChannel)
   270  				return
   271  			}
   272  		}
   273  
   274  		record, err := client.GetNext(c.baseURL, response, c.getNextListAuthorizationDocumentResponse)
   275  		if err != nil {
   276  			errorChannel <- err
   277  			break
   278  		} else if record == nil {
   279  			break
   280  		}
   281  
   282  		response = record.(*ListAuthorizationDocumentResponse)
   283  	}
   284  
   285  	close(recordChannel)
   286  	close(errorChannel)
   287  }
   288  
   289  func (c *ApiService) getNextListAuthorizationDocumentResponse(nextPageUrl string) (interface{}, error) {
   290  	if nextPageUrl == "" {
   291  		return nil, nil
   292  	}
   293  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   294  	if err != nil {
   295  		return nil, err
   296  	}
   297  
   298  	defer resp.Body.Close()
   299  
   300  	ps := &ListAuthorizationDocumentResponse{}
   301  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   302  		return nil, err
   303  	}
   304  	return ps, nil
   305  }