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