github.com/twilio/twilio-go@v1.20.1/rest/trusthub/v1/trust_products_evaluations.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 'CreateTrustProductEvaluation'
    27  type CreateTrustProductEvaluationParams struct {
    28  	// The unique string of a policy that is associated to the customer_profile resource.
    29  	PolicySid *string `json:"PolicySid,omitempty"`
    30  }
    31  
    32  func (params *CreateTrustProductEvaluationParams) SetPolicySid(PolicySid string) *CreateTrustProductEvaluationParams {
    33  	params.PolicySid = &PolicySid
    34  	return params
    35  }
    36  
    37  // Create a new Evaluation
    38  func (c *ApiService) CreateTrustProductEvaluation(TrustProductSid string, params *CreateTrustProductEvaluationParams) (*TrusthubV1TrustProductEvaluation, error) {
    39  	path := "/v1/TrustProducts/{TrustProductSid}/Evaluations"
    40  	path = strings.Replace(path, "{"+"TrustProductSid"+"}", TrustProductSid, -1)
    41  
    42  	data := url.Values{}
    43  	headers := make(map[string]interface{})
    44  
    45  	if params != nil && params.PolicySid != nil {
    46  		data.Set("PolicySid", *params.PolicySid)
    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 := &TrusthubV1TrustProductEvaluation{}
    57  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return ps, err
    62  }
    63  
    64  // Fetch specific Evaluation Instance.
    65  func (c *ApiService) FetchTrustProductEvaluation(TrustProductSid string, Sid string) (*TrusthubV1TrustProductEvaluation, error) {
    66  	path := "/v1/TrustProducts/{TrustProductSid}/Evaluations/{Sid}"
    67  	path = strings.Replace(path, "{"+"TrustProductSid"+"}", TrustProductSid, -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.Get(c.baseURL+path, data, headers)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	defer resp.Body.Close()
    79  
    80  	ps := &TrusthubV1TrustProductEvaluation{}
    81  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	return ps, err
    86  }
    87  
    88  // Optional parameters for the method 'ListTrustProductEvaluation'
    89  type ListTrustProductEvaluationParams struct {
    90  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    91  	PageSize *int `json:"PageSize,omitempty"`
    92  	// Max number of records to return.
    93  	Limit *int `json:"limit,omitempty"`
    94  }
    95  
    96  func (params *ListTrustProductEvaluationParams) SetPageSize(PageSize int) *ListTrustProductEvaluationParams {
    97  	params.PageSize = &PageSize
    98  	return params
    99  }
   100  func (params *ListTrustProductEvaluationParams) SetLimit(Limit int) *ListTrustProductEvaluationParams {
   101  	params.Limit = &Limit
   102  	return params
   103  }
   104  
   105  // Retrieve a single page of TrustProductEvaluation records from the API. Request is executed immediately.
   106  func (c *ApiService) PageTrustProductEvaluation(TrustProductSid string, params *ListTrustProductEvaluationParams, pageToken, pageNumber string) (*ListTrustProductEvaluationResponse, error) {
   107  	path := "/v1/TrustProducts/{TrustProductSid}/Evaluations"
   108  
   109  	path = strings.Replace(path, "{"+"TrustProductSid"+"}", TrustProductSid, -1)
   110  
   111  	data := url.Values{}
   112  	headers := make(map[string]interface{})
   113  
   114  	if params != nil && params.PageSize != nil {
   115  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   116  	}
   117  
   118  	if pageToken != "" {
   119  		data.Set("PageToken", pageToken)
   120  	}
   121  	if pageNumber != "" {
   122  		data.Set("Page", pageNumber)
   123  	}
   124  
   125  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	defer resp.Body.Close()
   131  
   132  	ps := &ListTrustProductEvaluationResponse{}
   133  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	return ps, err
   138  }
   139  
   140  // Lists TrustProductEvaluation records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   141  func (c *ApiService) ListTrustProductEvaluation(TrustProductSid string, params *ListTrustProductEvaluationParams) ([]TrusthubV1TrustProductEvaluation, error) {
   142  	response, errors := c.StreamTrustProductEvaluation(TrustProductSid, params)
   143  
   144  	records := make([]TrusthubV1TrustProductEvaluation, 0)
   145  	for record := range response {
   146  		records = append(records, record)
   147  	}
   148  
   149  	if err := <-errors; err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	return records, nil
   154  }
   155  
   156  // Streams TrustProductEvaluation records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   157  func (c *ApiService) StreamTrustProductEvaluation(TrustProductSid string, params *ListTrustProductEvaluationParams) (chan TrusthubV1TrustProductEvaluation, chan error) {
   158  	if params == nil {
   159  		params = &ListTrustProductEvaluationParams{}
   160  	}
   161  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   162  
   163  	recordChannel := make(chan TrusthubV1TrustProductEvaluation, 1)
   164  	errorChannel := make(chan error, 1)
   165  
   166  	response, err := c.PageTrustProductEvaluation(TrustProductSid, params, "", "")
   167  	if err != nil {
   168  		errorChannel <- err
   169  		close(recordChannel)
   170  		close(errorChannel)
   171  	} else {
   172  		go c.streamTrustProductEvaluation(response, params, recordChannel, errorChannel)
   173  	}
   174  
   175  	return recordChannel, errorChannel
   176  }
   177  
   178  func (c *ApiService) streamTrustProductEvaluation(response *ListTrustProductEvaluationResponse, params *ListTrustProductEvaluationParams, recordChannel chan TrusthubV1TrustProductEvaluation, errorChannel chan error) {
   179  	curRecord := 1
   180  
   181  	for response != nil {
   182  		responseRecords := response.Results
   183  		for item := range responseRecords {
   184  			recordChannel <- responseRecords[item]
   185  			curRecord += 1
   186  			if params.Limit != nil && *params.Limit < curRecord {
   187  				close(recordChannel)
   188  				close(errorChannel)
   189  				return
   190  			}
   191  		}
   192  
   193  		record, err := client.GetNext(c.baseURL, response, c.getNextListTrustProductEvaluationResponse)
   194  		if err != nil {
   195  			errorChannel <- err
   196  			break
   197  		} else if record == nil {
   198  			break
   199  		}
   200  
   201  		response = record.(*ListTrustProductEvaluationResponse)
   202  	}
   203  
   204  	close(recordChannel)
   205  	close(errorChannel)
   206  }
   207  
   208  func (c *ApiService) getNextListTrustProductEvaluationResponse(nextPageUrl string) (interface{}, error) {
   209  	if nextPageUrl == "" {
   210  		return nil, nil
   211  	}
   212  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	defer resp.Body.Close()
   218  
   219  	ps := &ListTrustProductEvaluationResponse{}
   220  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   221  		return nil, err
   222  	}
   223  	return ps, nil
   224  }