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