github.com/twilio/twilio-go@v1.20.1/rest/trusthub/v1/customer_profiles_entity_assignments.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 'CreateCustomerProfileEntityAssignment'
    27  type CreateCustomerProfileEntityAssignmentParams struct {
    28  	// The SID of an object bag that holds information of the different items.
    29  	ObjectSid *string `json:"ObjectSid,omitempty"`
    30  }
    31  
    32  func (params *CreateCustomerProfileEntityAssignmentParams) SetObjectSid(ObjectSid string) *CreateCustomerProfileEntityAssignmentParams {
    33  	params.ObjectSid = &ObjectSid
    34  	return params
    35  }
    36  
    37  // Create a new Assigned Item.
    38  func (c *ApiService) CreateCustomerProfileEntityAssignment(CustomerProfileSid string, params *CreateCustomerProfileEntityAssignmentParams) (*TrusthubV1CustomerProfileEntityAssignment, error) {
    39  	path := "/v1/CustomerProfiles/{CustomerProfileSid}/EntityAssignments"
    40  	path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1)
    41  
    42  	data := url.Values{}
    43  	headers := make(map[string]interface{})
    44  
    45  	if params != nil && params.ObjectSid != nil {
    46  		data.Set("ObjectSid", *params.ObjectSid)
    47  	}
    48  
    49  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	defer resp.Body.Close()
    55  
    56  	ps := &TrusthubV1CustomerProfileEntityAssignment{}
    57  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return ps, err
    62  }
    63  
    64  // Remove an Assignment Item Instance.
    65  func (c *ApiService) DeleteCustomerProfileEntityAssignment(CustomerProfileSid string, Sid string) error {
    66  	path := "/v1/CustomerProfiles/{CustomerProfileSid}/EntityAssignments/{Sid}"
    67  	path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1)
    68  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    69  
    70  	data := url.Values{}
    71  	headers := make(map[string]interface{})
    72  
    73  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	defer resp.Body.Close()
    79  
    80  	return nil
    81  }
    82  
    83  // Fetch specific Assigned Item Instance.
    84  func (c *ApiService) FetchCustomerProfileEntityAssignment(CustomerProfileSid string, Sid string) (*TrusthubV1CustomerProfileEntityAssignment, error) {
    85  	path := "/v1/CustomerProfiles/{CustomerProfileSid}/EntityAssignments/{Sid}"
    86  	path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1)
    87  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    88  
    89  	data := url.Values{}
    90  	headers := make(map[string]interface{})
    91  
    92  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	defer resp.Body.Close()
    98  
    99  	ps := &TrusthubV1CustomerProfileEntityAssignment{}
   100  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   101  		return nil, err
   102  	}
   103  
   104  	return ps, err
   105  }
   106  
   107  // Optional parameters for the method 'ListCustomerProfileEntityAssignment'
   108  type ListCustomerProfileEntityAssignmentParams struct {
   109  	// A string to filter the results by (EndUserType or SupportingDocumentType) machine-name. This is useful when you want to retrieve the entity-assignment of a specific end-user or supporting document.
   110  	ObjectType *string `json:"ObjectType,omitempty"`
   111  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   112  	PageSize *int `json:"PageSize,omitempty"`
   113  	// Max number of records to return.
   114  	Limit *int `json:"limit,omitempty"`
   115  }
   116  
   117  func (params *ListCustomerProfileEntityAssignmentParams) SetObjectType(ObjectType string) *ListCustomerProfileEntityAssignmentParams {
   118  	params.ObjectType = &ObjectType
   119  	return params
   120  }
   121  func (params *ListCustomerProfileEntityAssignmentParams) SetPageSize(PageSize int) *ListCustomerProfileEntityAssignmentParams {
   122  	params.PageSize = &PageSize
   123  	return params
   124  }
   125  func (params *ListCustomerProfileEntityAssignmentParams) SetLimit(Limit int) *ListCustomerProfileEntityAssignmentParams {
   126  	params.Limit = &Limit
   127  	return params
   128  }
   129  
   130  // Retrieve a single page of CustomerProfileEntityAssignment records from the API. Request is executed immediately.
   131  func (c *ApiService) PageCustomerProfileEntityAssignment(CustomerProfileSid string, params *ListCustomerProfileEntityAssignmentParams, pageToken, pageNumber string) (*ListCustomerProfileEntityAssignmentResponse, error) {
   132  	path := "/v1/CustomerProfiles/{CustomerProfileSid}/EntityAssignments"
   133  
   134  	path = strings.Replace(path, "{"+"CustomerProfileSid"+"}", CustomerProfileSid, -1)
   135  
   136  	data := url.Values{}
   137  	headers := make(map[string]interface{})
   138  
   139  	if params != nil && params.ObjectType != nil {
   140  		data.Set("ObjectType", *params.ObjectType)
   141  	}
   142  	if params != nil && params.PageSize != nil {
   143  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   144  	}
   145  
   146  	if pageToken != "" {
   147  		data.Set("PageToken", pageToken)
   148  	}
   149  	if pageNumber != "" {
   150  		data.Set("Page", pageNumber)
   151  	}
   152  
   153  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   154  	if err != nil {
   155  		return nil, err
   156  	}
   157  
   158  	defer resp.Body.Close()
   159  
   160  	ps := &ListCustomerProfileEntityAssignmentResponse{}
   161  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	return ps, err
   166  }
   167  
   168  // Lists CustomerProfileEntityAssignment records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   169  func (c *ApiService) ListCustomerProfileEntityAssignment(CustomerProfileSid string, params *ListCustomerProfileEntityAssignmentParams) ([]TrusthubV1CustomerProfileEntityAssignment, error) {
   170  	response, errors := c.StreamCustomerProfileEntityAssignment(CustomerProfileSid, params)
   171  
   172  	records := make([]TrusthubV1CustomerProfileEntityAssignment, 0)
   173  	for record := range response {
   174  		records = append(records, record)
   175  	}
   176  
   177  	if err := <-errors; err != nil {
   178  		return nil, err
   179  	}
   180  
   181  	return records, nil
   182  }
   183  
   184  // Streams CustomerProfileEntityAssignment records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   185  func (c *ApiService) StreamCustomerProfileEntityAssignment(CustomerProfileSid string, params *ListCustomerProfileEntityAssignmentParams) (chan TrusthubV1CustomerProfileEntityAssignment, chan error) {
   186  	if params == nil {
   187  		params = &ListCustomerProfileEntityAssignmentParams{}
   188  	}
   189  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   190  
   191  	recordChannel := make(chan TrusthubV1CustomerProfileEntityAssignment, 1)
   192  	errorChannel := make(chan error, 1)
   193  
   194  	response, err := c.PageCustomerProfileEntityAssignment(CustomerProfileSid, params, "", "")
   195  	if err != nil {
   196  		errorChannel <- err
   197  		close(recordChannel)
   198  		close(errorChannel)
   199  	} else {
   200  		go c.streamCustomerProfileEntityAssignment(response, params, recordChannel, errorChannel)
   201  	}
   202  
   203  	return recordChannel, errorChannel
   204  }
   205  
   206  func (c *ApiService) streamCustomerProfileEntityAssignment(response *ListCustomerProfileEntityAssignmentResponse, params *ListCustomerProfileEntityAssignmentParams, recordChannel chan TrusthubV1CustomerProfileEntityAssignment, errorChannel chan error) {
   207  	curRecord := 1
   208  
   209  	for response != nil {
   210  		responseRecords := response.Results
   211  		for item := range responseRecords {
   212  			recordChannel <- responseRecords[item]
   213  			curRecord += 1
   214  			if params.Limit != nil && *params.Limit < curRecord {
   215  				close(recordChannel)
   216  				close(errorChannel)
   217  				return
   218  			}
   219  		}
   220  
   221  		record, err := client.GetNext(c.baseURL, response, c.getNextListCustomerProfileEntityAssignmentResponse)
   222  		if err != nil {
   223  			errorChannel <- err
   224  			break
   225  		} else if record == nil {
   226  			break
   227  		}
   228  
   229  		response = record.(*ListCustomerProfileEntityAssignmentResponse)
   230  	}
   231  
   232  	close(recordChannel)
   233  	close(errorChannel)
   234  }
   235  
   236  func (c *ApiService) getNextListCustomerProfileEntityAssignmentResponse(nextPageUrl string) (interface{}, error) {
   237  	if nextPageUrl == "" {
   238  		return nil, nil
   239  	}
   240  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  
   245  	defer resp.Body.Close()
   246  
   247  	ps := &ListCustomerProfileEntityAssignmentResponse{}
   248  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   249  		return nil, err
   250  	}
   251  	return ps, nil
   252  }