github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/plugin_service_configurations_plugins.go (about)

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