github.com/twilio/twilio-go@v1.20.1/rest/trusthub/v1/customer_profiles.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Trusthub
     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 'CreateCustomerProfile'
    27  type CreateCustomerProfileParams struct {
    28  	// The string that you assigned to describe the resource.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// The email address that will receive updates when the Customer-Profile resource changes status.
    31  	Email *string `json:"Email,omitempty"`
    32  	// The unique string of a policy that is associated to the Customer-Profile resource.
    33  	PolicySid *string `json:"PolicySid,omitempty"`
    34  	// The URL we call to inform your application of status changes.
    35  	StatusCallback *string `json:"StatusCallback,omitempty"`
    36  }
    37  
    38  func (params *CreateCustomerProfileParams) SetFriendlyName(FriendlyName string) *CreateCustomerProfileParams {
    39  	params.FriendlyName = &FriendlyName
    40  	return params
    41  }
    42  func (params *CreateCustomerProfileParams) SetEmail(Email string) *CreateCustomerProfileParams {
    43  	params.Email = &Email
    44  	return params
    45  }
    46  func (params *CreateCustomerProfileParams) SetPolicySid(PolicySid string) *CreateCustomerProfileParams {
    47  	params.PolicySid = &PolicySid
    48  	return params
    49  }
    50  func (params *CreateCustomerProfileParams) SetStatusCallback(StatusCallback string) *CreateCustomerProfileParams {
    51  	params.StatusCallback = &StatusCallback
    52  	return params
    53  }
    54  
    55  // Create a new Customer-Profile.
    56  func (c *ApiService) CreateCustomerProfile(params *CreateCustomerProfileParams) (*TrusthubV1CustomerProfile, error) {
    57  	path := "/v1/CustomerProfiles"
    58  
    59  	data := url.Values{}
    60  	headers := make(map[string]interface{})
    61  
    62  	if params != nil && params.FriendlyName != nil {
    63  		data.Set("FriendlyName", *params.FriendlyName)
    64  	}
    65  	if params != nil && params.Email != nil {
    66  		data.Set("Email", *params.Email)
    67  	}
    68  	if params != nil && params.PolicySid != nil {
    69  		data.Set("PolicySid", *params.PolicySid)
    70  	}
    71  	if params != nil && params.StatusCallback != nil {
    72  		data.Set("StatusCallback", *params.StatusCallback)
    73  	}
    74  
    75  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	defer resp.Body.Close()
    81  
    82  	ps := &TrusthubV1CustomerProfile{}
    83  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return ps, err
    88  }
    89  
    90  // Delete a specific Customer-Profile.
    91  func (c *ApiService) DeleteCustomerProfile(Sid string) error {
    92  	path := "/v1/CustomerProfiles/{Sid}"
    93  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	defer resp.Body.Close()
   104  
   105  	return nil
   106  }
   107  
   108  // Fetch a specific Customer-Profile instance.
   109  func (c *ApiService) FetchCustomerProfile(Sid string) (*TrusthubV1CustomerProfile, error) {
   110  	path := "/v1/CustomerProfiles/{Sid}"
   111  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   112  
   113  	data := url.Values{}
   114  	headers := make(map[string]interface{})
   115  
   116  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	ps := &TrusthubV1CustomerProfile{}
   124  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return ps, err
   129  }
   130  
   131  // Optional parameters for the method 'ListCustomerProfile'
   132  type ListCustomerProfileParams struct {
   133  	// The verification status of the Customer-Profile resource.
   134  	Status *string `json:"Status,omitempty"`
   135  	// The string that you assigned to describe the resource.
   136  	FriendlyName *string `json:"FriendlyName,omitempty"`
   137  	// The unique string of a policy that is associated to the Customer-Profile resource.
   138  	PolicySid *string `json:"PolicySid,omitempty"`
   139  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   140  	PageSize *int `json:"PageSize,omitempty"`
   141  	// Max number of records to return.
   142  	Limit *int `json:"limit,omitempty"`
   143  }
   144  
   145  func (params *ListCustomerProfileParams) SetStatus(Status string) *ListCustomerProfileParams {
   146  	params.Status = &Status
   147  	return params
   148  }
   149  func (params *ListCustomerProfileParams) SetFriendlyName(FriendlyName string) *ListCustomerProfileParams {
   150  	params.FriendlyName = &FriendlyName
   151  	return params
   152  }
   153  func (params *ListCustomerProfileParams) SetPolicySid(PolicySid string) *ListCustomerProfileParams {
   154  	params.PolicySid = &PolicySid
   155  	return params
   156  }
   157  func (params *ListCustomerProfileParams) SetPageSize(PageSize int) *ListCustomerProfileParams {
   158  	params.PageSize = &PageSize
   159  	return params
   160  }
   161  func (params *ListCustomerProfileParams) SetLimit(Limit int) *ListCustomerProfileParams {
   162  	params.Limit = &Limit
   163  	return params
   164  }
   165  
   166  // Retrieve a single page of CustomerProfile records from the API. Request is executed immediately.
   167  func (c *ApiService) PageCustomerProfile(params *ListCustomerProfileParams, pageToken, pageNumber string) (*ListCustomerProfileResponse, error) {
   168  	path := "/v1/CustomerProfiles"
   169  
   170  	data := url.Values{}
   171  	headers := make(map[string]interface{})
   172  
   173  	if params != nil && params.Status != nil {
   174  		data.Set("Status", *params.Status)
   175  	}
   176  	if params != nil && params.FriendlyName != nil {
   177  		data.Set("FriendlyName", *params.FriendlyName)
   178  	}
   179  	if params != nil && params.PolicySid != nil {
   180  		data.Set("PolicySid", *params.PolicySid)
   181  	}
   182  	if params != nil && params.PageSize != nil {
   183  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   184  	}
   185  
   186  	if pageToken != "" {
   187  		data.Set("PageToken", pageToken)
   188  	}
   189  	if pageNumber != "" {
   190  		data.Set("Page", pageNumber)
   191  	}
   192  
   193  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	defer resp.Body.Close()
   199  
   200  	ps := &ListCustomerProfileResponse{}
   201  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return ps, err
   206  }
   207  
   208  // Lists CustomerProfile records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   209  func (c *ApiService) ListCustomerProfile(params *ListCustomerProfileParams) ([]TrusthubV1CustomerProfile, error) {
   210  	response, errors := c.StreamCustomerProfile(params)
   211  
   212  	records := make([]TrusthubV1CustomerProfile, 0)
   213  	for record := range response {
   214  		records = append(records, record)
   215  	}
   216  
   217  	if err := <-errors; err != nil {
   218  		return nil, err
   219  	}
   220  
   221  	return records, nil
   222  }
   223  
   224  // Streams CustomerProfile records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   225  func (c *ApiService) StreamCustomerProfile(params *ListCustomerProfileParams) (chan TrusthubV1CustomerProfile, chan error) {
   226  	if params == nil {
   227  		params = &ListCustomerProfileParams{}
   228  	}
   229  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   230  
   231  	recordChannel := make(chan TrusthubV1CustomerProfile, 1)
   232  	errorChannel := make(chan error, 1)
   233  
   234  	response, err := c.PageCustomerProfile(params, "", "")
   235  	if err != nil {
   236  		errorChannel <- err
   237  		close(recordChannel)
   238  		close(errorChannel)
   239  	} else {
   240  		go c.streamCustomerProfile(response, params, recordChannel, errorChannel)
   241  	}
   242  
   243  	return recordChannel, errorChannel
   244  }
   245  
   246  func (c *ApiService) streamCustomerProfile(response *ListCustomerProfileResponse, params *ListCustomerProfileParams, recordChannel chan TrusthubV1CustomerProfile, errorChannel chan error) {
   247  	curRecord := 1
   248  
   249  	for response != nil {
   250  		responseRecords := response.Results
   251  		for item := range responseRecords {
   252  			recordChannel <- responseRecords[item]
   253  			curRecord += 1
   254  			if params.Limit != nil && *params.Limit < curRecord {
   255  				close(recordChannel)
   256  				close(errorChannel)
   257  				return
   258  			}
   259  		}
   260  
   261  		record, err := client.GetNext(c.baseURL, response, c.getNextListCustomerProfileResponse)
   262  		if err != nil {
   263  			errorChannel <- err
   264  			break
   265  		} else if record == nil {
   266  			break
   267  		}
   268  
   269  		response = record.(*ListCustomerProfileResponse)
   270  	}
   271  
   272  	close(recordChannel)
   273  	close(errorChannel)
   274  }
   275  
   276  func (c *ApiService) getNextListCustomerProfileResponse(nextPageUrl string) (interface{}, error) {
   277  	if nextPageUrl == "" {
   278  		return nil, nil
   279  	}
   280  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   281  	if err != nil {
   282  		return nil, err
   283  	}
   284  
   285  	defer resp.Body.Close()
   286  
   287  	ps := &ListCustomerProfileResponse{}
   288  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   289  		return nil, err
   290  	}
   291  	return ps, nil
   292  }
   293  
   294  // Optional parameters for the method 'UpdateCustomerProfile'
   295  type UpdateCustomerProfileParams struct {
   296  	//
   297  	Status *string `json:"Status,omitempty"`
   298  	// The URL we call to inform your application of status changes.
   299  	StatusCallback *string `json:"StatusCallback,omitempty"`
   300  	// The string that you assigned to describe the resource.
   301  	FriendlyName *string `json:"FriendlyName,omitempty"`
   302  	// The email address that will receive updates when the Customer-Profile resource changes status.
   303  	Email *string `json:"Email,omitempty"`
   304  }
   305  
   306  func (params *UpdateCustomerProfileParams) SetStatus(Status string) *UpdateCustomerProfileParams {
   307  	params.Status = &Status
   308  	return params
   309  }
   310  func (params *UpdateCustomerProfileParams) SetStatusCallback(StatusCallback string) *UpdateCustomerProfileParams {
   311  	params.StatusCallback = &StatusCallback
   312  	return params
   313  }
   314  func (params *UpdateCustomerProfileParams) SetFriendlyName(FriendlyName string) *UpdateCustomerProfileParams {
   315  	params.FriendlyName = &FriendlyName
   316  	return params
   317  }
   318  func (params *UpdateCustomerProfileParams) SetEmail(Email string) *UpdateCustomerProfileParams {
   319  	params.Email = &Email
   320  	return params
   321  }
   322  
   323  // Updates a Customer-Profile in an account.
   324  func (c *ApiService) UpdateCustomerProfile(Sid string, params *UpdateCustomerProfileParams) (*TrusthubV1CustomerProfile, error) {
   325  	path := "/v1/CustomerProfiles/{Sid}"
   326  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   327  
   328  	data := url.Values{}
   329  	headers := make(map[string]interface{})
   330  
   331  	if params != nil && params.Status != nil {
   332  		data.Set("Status", *params.Status)
   333  	}
   334  	if params != nil && params.StatusCallback != nil {
   335  		data.Set("StatusCallback", *params.StatusCallback)
   336  	}
   337  	if params != nil && params.FriendlyName != nil {
   338  		data.Set("FriendlyName", *params.FriendlyName)
   339  	}
   340  	if params != nil && params.Email != nil {
   341  		data.Set("Email", *params.Email)
   342  	}
   343  
   344  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   345  	if err != nil {
   346  		return nil, err
   347  	}
   348  
   349  	defer resp.Body.Close()
   350  
   351  	ps := &TrusthubV1CustomerProfile{}
   352  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   353  		return nil, err
   354  	}
   355  
   356  	return ps, err
   357  }