github.com/twilio/twilio-go@v1.20.1/rest/serverless/v1/services_builds.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Serverless
     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 'CreateBuild'
    27  type CreateBuildParams struct {
    28  	// The list of Asset Version resource SIDs to include in the Build.
    29  	AssetVersions *[]string `json:"AssetVersions,omitempty"`
    30  	// The list of the Function Version resource SIDs to include in the Build.
    31  	FunctionVersions *[]string `json:"FunctionVersions,omitempty"`
    32  	// A list of objects that describe the Dependencies included in the Build. Each object contains the `name` and `version` of the dependency.
    33  	Dependencies *string `json:"Dependencies,omitempty"`
    34  	// The Runtime version that will be used to run the Build resource when it is deployed.
    35  	Runtime *string `json:"Runtime,omitempty"`
    36  }
    37  
    38  func (params *CreateBuildParams) SetAssetVersions(AssetVersions []string) *CreateBuildParams {
    39  	params.AssetVersions = &AssetVersions
    40  	return params
    41  }
    42  func (params *CreateBuildParams) SetFunctionVersions(FunctionVersions []string) *CreateBuildParams {
    43  	params.FunctionVersions = &FunctionVersions
    44  	return params
    45  }
    46  func (params *CreateBuildParams) SetDependencies(Dependencies string) *CreateBuildParams {
    47  	params.Dependencies = &Dependencies
    48  	return params
    49  }
    50  func (params *CreateBuildParams) SetRuntime(Runtime string) *CreateBuildParams {
    51  	params.Runtime = &Runtime
    52  	return params
    53  }
    54  
    55  // Create a new Build resource. At least one function version or asset version is required.
    56  func (c *ApiService) CreateBuild(ServiceSid string, params *CreateBuildParams) (*ServerlessV1Build, error) {
    57  	path := "/v1/Services/{ServiceSid}/Builds"
    58  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    59  
    60  	data := url.Values{}
    61  	headers := make(map[string]interface{})
    62  
    63  	if params != nil && params.AssetVersions != nil {
    64  		for _, item := range *params.AssetVersions {
    65  			data.Add("AssetVersions", item)
    66  		}
    67  	}
    68  	if params != nil && params.FunctionVersions != nil {
    69  		for _, item := range *params.FunctionVersions {
    70  			data.Add("FunctionVersions", item)
    71  		}
    72  	}
    73  	if params != nil && params.Dependencies != nil {
    74  		data.Set("Dependencies", *params.Dependencies)
    75  	}
    76  	if params != nil && params.Runtime != nil {
    77  		data.Set("Runtime", *params.Runtime)
    78  	}
    79  
    80  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	defer resp.Body.Close()
    86  
    87  	ps := &ServerlessV1Build{}
    88  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return ps, err
    93  }
    94  
    95  // Delete a Build resource.
    96  func (c *ApiService) DeleteBuild(ServiceSid string, Sid string) error {
    97  	path := "/v1/Services/{ServiceSid}/Builds/{Sid}"
    98  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    99  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   100  
   101  	data := url.Values{}
   102  	headers := make(map[string]interface{})
   103  
   104  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	defer resp.Body.Close()
   110  
   111  	return nil
   112  }
   113  
   114  // Retrieve a specific Build resource.
   115  func (c *ApiService) FetchBuild(ServiceSid string, Sid string) (*ServerlessV1Build, error) {
   116  	path := "/v1/Services/{ServiceSid}/Builds/{Sid}"
   117  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   118  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   119  
   120  	data := url.Values{}
   121  	headers := make(map[string]interface{})
   122  
   123  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	defer resp.Body.Close()
   129  
   130  	ps := &ServerlessV1Build{}
   131  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   132  		return nil, err
   133  	}
   134  
   135  	return ps, err
   136  }
   137  
   138  // Optional parameters for the method 'ListBuild'
   139  type ListBuildParams struct {
   140  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   141  	PageSize *int `json:"PageSize,omitempty"`
   142  	// Max number of records to return.
   143  	Limit *int `json:"limit,omitempty"`
   144  }
   145  
   146  func (params *ListBuildParams) SetPageSize(PageSize int) *ListBuildParams {
   147  	params.PageSize = &PageSize
   148  	return params
   149  }
   150  func (params *ListBuildParams) SetLimit(Limit int) *ListBuildParams {
   151  	params.Limit = &Limit
   152  	return params
   153  }
   154  
   155  // Retrieve a single page of Build records from the API. Request is executed immediately.
   156  func (c *ApiService) PageBuild(ServiceSid string, params *ListBuildParams, pageToken, pageNumber string) (*ListBuildResponse, error) {
   157  	path := "/v1/Services/{ServiceSid}/Builds"
   158  
   159  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   160  
   161  	data := url.Values{}
   162  	headers := make(map[string]interface{})
   163  
   164  	if params != nil && params.PageSize != nil {
   165  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   166  	}
   167  
   168  	if pageToken != "" {
   169  		data.Set("PageToken", pageToken)
   170  	}
   171  	if pageNumber != "" {
   172  		data.Set("Page", pageNumber)
   173  	}
   174  
   175  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   176  	if err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	defer resp.Body.Close()
   181  
   182  	ps := &ListBuildResponse{}
   183  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return ps, err
   188  }
   189  
   190  // Lists Build records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   191  func (c *ApiService) ListBuild(ServiceSid string, params *ListBuildParams) ([]ServerlessV1Build, error) {
   192  	response, errors := c.StreamBuild(ServiceSid, params)
   193  
   194  	records := make([]ServerlessV1Build, 0)
   195  	for record := range response {
   196  		records = append(records, record)
   197  	}
   198  
   199  	if err := <-errors; err != nil {
   200  		return nil, err
   201  	}
   202  
   203  	return records, nil
   204  }
   205  
   206  // Streams Build records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   207  func (c *ApiService) StreamBuild(ServiceSid string, params *ListBuildParams) (chan ServerlessV1Build, chan error) {
   208  	if params == nil {
   209  		params = &ListBuildParams{}
   210  	}
   211  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   212  
   213  	recordChannel := make(chan ServerlessV1Build, 1)
   214  	errorChannel := make(chan error, 1)
   215  
   216  	response, err := c.PageBuild(ServiceSid, params, "", "")
   217  	if err != nil {
   218  		errorChannel <- err
   219  		close(recordChannel)
   220  		close(errorChannel)
   221  	} else {
   222  		go c.streamBuild(response, params, recordChannel, errorChannel)
   223  	}
   224  
   225  	return recordChannel, errorChannel
   226  }
   227  
   228  func (c *ApiService) streamBuild(response *ListBuildResponse, params *ListBuildParams, recordChannel chan ServerlessV1Build, errorChannel chan error) {
   229  	curRecord := 1
   230  
   231  	for response != nil {
   232  		responseRecords := response.Builds
   233  		for item := range responseRecords {
   234  			recordChannel <- responseRecords[item]
   235  			curRecord += 1
   236  			if params.Limit != nil && *params.Limit < curRecord {
   237  				close(recordChannel)
   238  				close(errorChannel)
   239  				return
   240  			}
   241  		}
   242  
   243  		record, err := client.GetNext(c.baseURL, response, c.getNextListBuildResponse)
   244  		if err != nil {
   245  			errorChannel <- err
   246  			break
   247  		} else if record == nil {
   248  			break
   249  		}
   250  
   251  		response = record.(*ListBuildResponse)
   252  	}
   253  
   254  	close(recordChannel)
   255  	close(errorChannel)
   256  }
   257  
   258  func (c *ApiService) getNextListBuildResponse(nextPageUrl string) (interface{}, error) {
   259  	if nextPageUrl == "" {
   260  		return nil, nil
   261  	}
   262  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   263  	if err != nil {
   264  		return nil, err
   265  	}
   266  
   267  	defer resp.Body.Close()
   268  
   269  	ps := &ListBuildResponse{}
   270  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   271  		return nil, err
   272  	}
   273  	return ps, nil
   274  }