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