github.com/twilio/twilio-go@v1.20.1/rest/serverless/v1/services.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 'CreateService'
    27  type CreateServiceParams struct {
    28  	// A user-defined string that uniquely identifies the Service resource. It can be used as an alternative to the `sid` in the URL path to address the Service resource. This value must be 50 characters or less in length and be unique.
    29  	UniqueName *string `json:"UniqueName,omitempty"`
    30  	// A descriptive string that you create to describe the Service resource. It can be a maximum of 255 characters.
    31  	FriendlyName *string `json:"FriendlyName,omitempty"`
    32  	// Whether to inject Account credentials into a function invocation context. The default value is `true`.
    33  	IncludeCredentials *bool `json:"IncludeCredentials,omitempty"`
    34  	// Whether the Service's properties and subresources can be edited via the UI. The default value is `false`.
    35  	UiEditable *bool `json:"UiEditable,omitempty"`
    36  }
    37  
    38  func (params *CreateServiceParams) SetUniqueName(UniqueName string) *CreateServiceParams {
    39  	params.UniqueName = &UniqueName
    40  	return params
    41  }
    42  func (params *CreateServiceParams) SetFriendlyName(FriendlyName string) *CreateServiceParams {
    43  	params.FriendlyName = &FriendlyName
    44  	return params
    45  }
    46  func (params *CreateServiceParams) SetIncludeCredentials(IncludeCredentials bool) *CreateServiceParams {
    47  	params.IncludeCredentials = &IncludeCredentials
    48  	return params
    49  }
    50  func (params *CreateServiceParams) SetUiEditable(UiEditable bool) *CreateServiceParams {
    51  	params.UiEditable = &UiEditable
    52  	return params
    53  }
    54  
    55  // Create a new Service resource.
    56  func (c *ApiService) CreateService(params *CreateServiceParams) (*ServerlessV1Service, error) {
    57  	path := "/v1/Services"
    58  
    59  	data := url.Values{}
    60  	headers := make(map[string]interface{})
    61  
    62  	if params != nil && params.UniqueName != nil {
    63  		data.Set("UniqueName", *params.UniqueName)
    64  	}
    65  	if params != nil && params.FriendlyName != nil {
    66  		data.Set("FriendlyName", *params.FriendlyName)
    67  	}
    68  	if params != nil && params.IncludeCredentials != nil {
    69  		data.Set("IncludeCredentials", fmt.Sprint(*params.IncludeCredentials))
    70  	}
    71  	if params != nil && params.UiEditable != nil {
    72  		data.Set("UiEditable", fmt.Sprint(*params.UiEditable))
    73  	}
    74  
    75  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	defer resp.Body.Close()
    81  
    82  	ps := &ServerlessV1Service{}
    83  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	return ps, err
    88  }
    89  
    90  // Delete a Service resource.
    91  func (c *ApiService) DeleteService(Sid string) error {
    92  	path := "/v1/Services/{Sid}"
    93  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	defer resp.Body.Close()
   104  
   105  	return nil
   106  }
   107  
   108  // Retrieve a specific Service resource.
   109  func (c *ApiService) FetchService(Sid string) (*ServerlessV1Service, error) {
   110  	path := "/v1/Services/{Sid}"
   111  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   112  
   113  	data := url.Values{}
   114  	headers := make(map[string]interface{})
   115  
   116  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	ps := &ServerlessV1Service{}
   124  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return ps, err
   129  }
   130  
   131  // Optional parameters for the method 'ListService'
   132  type ListServiceParams struct {
   133  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   134  	PageSize *int `json:"PageSize,omitempty"`
   135  	// Max number of records to return.
   136  	Limit *int `json:"limit,omitempty"`
   137  }
   138  
   139  func (params *ListServiceParams) SetPageSize(PageSize int) *ListServiceParams {
   140  	params.PageSize = &PageSize
   141  	return params
   142  }
   143  func (params *ListServiceParams) SetLimit(Limit int) *ListServiceParams {
   144  	params.Limit = &Limit
   145  	return params
   146  }
   147  
   148  // Retrieve a single page of Service records from the API. Request is executed immediately.
   149  func (c *ApiService) PageService(params *ListServiceParams, pageToken, pageNumber string) (*ListServiceResponse, error) {
   150  	path := "/v1/Services"
   151  
   152  	data := url.Values{}
   153  	headers := make(map[string]interface{})
   154  
   155  	if params != nil && params.PageSize != nil {
   156  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   157  	}
   158  
   159  	if pageToken != "" {
   160  		data.Set("PageToken", pageToken)
   161  	}
   162  	if pageNumber != "" {
   163  		data.Set("Page", pageNumber)
   164  	}
   165  
   166  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   167  	if err != nil {
   168  		return nil, err
   169  	}
   170  
   171  	defer resp.Body.Close()
   172  
   173  	ps := &ListServiceResponse{}
   174  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   175  		return nil, err
   176  	}
   177  
   178  	return ps, err
   179  }
   180  
   181  // Lists Service records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   182  func (c *ApiService) ListService(params *ListServiceParams) ([]ServerlessV1Service, error) {
   183  	response, errors := c.StreamService(params)
   184  
   185  	records := make([]ServerlessV1Service, 0)
   186  	for record := range response {
   187  		records = append(records, record)
   188  	}
   189  
   190  	if err := <-errors; err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return records, nil
   195  }
   196  
   197  // Streams Service records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   198  func (c *ApiService) StreamService(params *ListServiceParams) (chan ServerlessV1Service, chan error) {
   199  	if params == nil {
   200  		params = &ListServiceParams{}
   201  	}
   202  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   203  
   204  	recordChannel := make(chan ServerlessV1Service, 1)
   205  	errorChannel := make(chan error, 1)
   206  
   207  	response, err := c.PageService(params, "", "")
   208  	if err != nil {
   209  		errorChannel <- err
   210  		close(recordChannel)
   211  		close(errorChannel)
   212  	} else {
   213  		go c.streamService(response, params, recordChannel, errorChannel)
   214  	}
   215  
   216  	return recordChannel, errorChannel
   217  }
   218  
   219  func (c *ApiService) streamService(response *ListServiceResponse, params *ListServiceParams, recordChannel chan ServerlessV1Service, errorChannel chan error) {
   220  	curRecord := 1
   221  
   222  	for response != nil {
   223  		responseRecords := response.Services
   224  		for item := range responseRecords {
   225  			recordChannel <- responseRecords[item]
   226  			curRecord += 1
   227  			if params.Limit != nil && *params.Limit < curRecord {
   228  				close(recordChannel)
   229  				close(errorChannel)
   230  				return
   231  			}
   232  		}
   233  
   234  		record, err := client.GetNext(c.baseURL, response, c.getNextListServiceResponse)
   235  		if err != nil {
   236  			errorChannel <- err
   237  			break
   238  		} else if record == nil {
   239  			break
   240  		}
   241  
   242  		response = record.(*ListServiceResponse)
   243  	}
   244  
   245  	close(recordChannel)
   246  	close(errorChannel)
   247  }
   248  
   249  func (c *ApiService) getNextListServiceResponse(nextPageUrl string) (interface{}, error) {
   250  	if nextPageUrl == "" {
   251  		return nil, nil
   252  	}
   253  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  
   258  	defer resp.Body.Close()
   259  
   260  	ps := &ListServiceResponse{}
   261  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   262  		return nil, err
   263  	}
   264  	return ps, nil
   265  }
   266  
   267  // Optional parameters for the method 'UpdateService'
   268  type UpdateServiceParams struct {
   269  	// Whether to inject Account credentials into a function invocation context.
   270  	IncludeCredentials *bool `json:"IncludeCredentials,omitempty"`
   271  	// A descriptive string that you create to describe the Service resource. It can be a maximum of 255 characters.
   272  	FriendlyName *string `json:"FriendlyName,omitempty"`
   273  	// Whether the Service resource's properties and subresources can be edited via the UI. The default value is `false`.
   274  	UiEditable *bool `json:"UiEditable,omitempty"`
   275  }
   276  
   277  func (params *UpdateServiceParams) SetIncludeCredentials(IncludeCredentials bool) *UpdateServiceParams {
   278  	params.IncludeCredentials = &IncludeCredentials
   279  	return params
   280  }
   281  func (params *UpdateServiceParams) SetFriendlyName(FriendlyName string) *UpdateServiceParams {
   282  	params.FriendlyName = &FriendlyName
   283  	return params
   284  }
   285  func (params *UpdateServiceParams) SetUiEditable(UiEditable bool) *UpdateServiceParams {
   286  	params.UiEditable = &UiEditable
   287  	return params
   288  }
   289  
   290  // Update a specific Service resource.
   291  func (c *ApiService) UpdateService(Sid string, params *UpdateServiceParams) (*ServerlessV1Service, error) {
   292  	path := "/v1/Services/{Sid}"
   293  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   294  
   295  	data := url.Values{}
   296  	headers := make(map[string]interface{})
   297  
   298  	if params != nil && params.IncludeCredentials != nil {
   299  		data.Set("IncludeCredentials", fmt.Sprint(*params.IncludeCredentials))
   300  	}
   301  	if params != nil && params.FriendlyName != nil {
   302  		data.Set("FriendlyName", *params.FriendlyName)
   303  	}
   304  	if params != nil && params.UiEditable != nil {
   305  		data.Set("UiEditable", fmt.Sprint(*params.UiEditable))
   306  	}
   307  
   308  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   309  	if err != nil {
   310  		return nil, err
   311  	}
   312  
   313  	defer resp.Body.Close()
   314  
   315  	ps := &ServerlessV1Service{}
   316  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   317  		return nil, err
   318  	}
   319  
   320  	return ps, err
   321  }