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