github.com/twilio/twilio-go@v1.20.1/rest/microvisor/v1/configs.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Microvisor
     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 'CreateAccountConfig'
    27  type CreateAccountConfigParams struct {
    28  	// The config key; up to 100 characters.
    29  	Key *string `json:"Key,omitempty"`
    30  	// The config value; up to 4096 characters.
    31  	Value *string `json:"Value,omitempty"`
    32  }
    33  
    34  func (params *CreateAccountConfigParams) SetKey(Key string) *CreateAccountConfigParams {
    35  	params.Key = &Key
    36  	return params
    37  }
    38  func (params *CreateAccountConfigParams) SetValue(Value string) *CreateAccountConfigParams {
    39  	params.Value = &Value
    40  	return params
    41  }
    42  
    43  // Create a config for an Account.
    44  func (c *ApiService) CreateAccountConfig(params *CreateAccountConfigParams) (*MicrovisorV1AccountConfig, error) {
    45  	path := "/v1/Configs"
    46  
    47  	data := url.Values{}
    48  	headers := make(map[string]interface{})
    49  
    50  	if params != nil && params.Key != nil {
    51  		data.Set("Key", *params.Key)
    52  	}
    53  	if params != nil && params.Value != nil {
    54  		data.Set("Value", *params.Value)
    55  	}
    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 := &MicrovisorV1AccountConfig{}
    65  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return ps, err
    70  }
    71  
    72  // Delete a config for an Account.
    73  func (c *ApiService) DeleteAccountConfig(Key string) error {
    74  	path := "/v1/Configs/{Key}"
    75  	path = strings.Replace(path, "{"+"Key"+"}", Key, -1)
    76  
    77  	data := url.Values{}
    78  	headers := make(map[string]interface{})
    79  
    80  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    81  	if err != nil {
    82  		return err
    83  	}
    84  
    85  	defer resp.Body.Close()
    86  
    87  	return nil
    88  }
    89  
    90  // Retrieve a Config for an Account.
    91  func (c *ApiService) FetchAccountConfig(Key string) (*MicrovisorV1AccountConfig, error) {
    92  	path := "/v1/Configs/{Key}"
    93  	path = strings.Replace(path, "{"+"Key"+"}", Key, -1)
    94  
    95  	data := url.Values{}
    96  	headers := make(map[string]interface{})
    97  
    98  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  
   103  	defer resp.Body.Close()
   104  
   105  	ps := &MicrovisorV1AccountConfig{}
   106  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return ps, err
   111  }
   112  
   113  // Optional parameters for the method 'ListAccountConfig'
   114  type ListAccountConfigParams struct {
   115  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   116  	PageSize *int `json:"PageSize,omitempty"`
   117  	// Max number of records to return.
   118  	Limit *int `json:"limit,omitempty"`
   119  }
   120  
   121  func (params *ListAccountConfigParams) SetPageSize(PageSize int) *ListAccountConfigParams {
   122  	params.PageSize = &PageSize
   123  	return params
   124  }
   125  func (params *ListAccountConfigParams) SetLimit(Limit int) *ListAccountConfigParams {
   126  	params.Limit = &Limit
   127  	return params
   128  }
   129  
   130  // Retrieve a single page of AccountConfig records from the API. Request is executed immediately.
   131  func (c *ApiService) PageAccountConfig(params *ListAccountConfigParams, pageToken, pageNumber string) (*ListAccountConfigResponse, error) {
   132  	path := "/v1/Configs"
   133  
   134  	data := url.Values{}
   135  	headers := make(map[string]interface{})
   136  
   137  	if params != nil && params.PageSize != nil {
   138  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   139  	}
   140  
   141  	if pageToken != "" {
   142  		data.Set("PageToken", pageToken)
   143  	}
   144  	if pageNumber != "" {
   145  		data.Set("Page", pageNumber)
   146  	}
   147  
   148  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   149  	if err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	defer resp.Body.Close()
   154  
   155  	ps := &ListAccountConfigResponse{}
   156  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	return ps, err
   161  }
   162  
   163  // Lists AccountConfig records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   164  func (c *ApiService) ListAccountConfig(params *ListAccountConfigParams) ([]MicrovisorV1AccountConfig, error) {
   165  	response, errors := c.StreamAccountConfig(params)
   166  
   167  	records := make([]MicrovisorV1AccountConfig, 0)
   168  	for record := range response {
   169  		records = append(records, record)
   170  	}
   171  
   172  	if err := <-errors; err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	return records, nil
   177  }
   178  
   179  // Streams AccountConfig records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   180  func (c *ApiService) StreamAccountConfig(params *ListAccountConfigParams) (chan MicrovisorV1AccountConfig, chan error) {
   181  	if params == nil {
   182  		params = &ListAccountConfigParams{}
   183  	}
   184  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   185  
   186  	recordChannel := make(chan MicrovisorV1AccountConfig, 1)
   187  	errorChannel := make(chan error, 1)
   188  
   189  	response, err := c.PageAccountConfig(params, "", "")
   190  	if err != nil {
   191  		errorChannel <- err
   192  		close(recordChannel)
   193  		close(errorChannel)
   194  	} else {
   195  		go c.streamAccountConfig(response, params, recordChannel, errorChannel)
   196  	}
   197  
   198  	return recordChannel, errorChannel
   199  }
   200  
   201  func (c *ApiService) streamAccountConfig(response *ListAccountConfigResponse, params *ListAccountConfigParams, recordChannel chan MicrovisorV1AccountConfig, errorChannel chan error) {
   202  	curRecord := 1
   203  
   204  	for response != nil {
   205  		responseRecords := response.Configs
   206  		for item := range responseRecords {
   207  			recordChannel <- responseRecords[item]
   208  			curRecord += 1
   209  			if params.Limit != nil && *params.Limit < curRecord {
   210  				close(recordChannel)
   211  				close(errorChannel)
   212  				return
   213  			}
   214  		}
   215  
   216  		record, err := client.GetNext(c.baseURL, response, c.getNextListAccountConfigResponse)
   217  		if err != nil {
   218  			errorChannel <- err
   219  			break
   220  		} else if record == nil {
   221  			break
   222  		}
   223  
   224  		response = record.(*ListAccountConfigResponse)
   225  	}
   226  
   227  	close(recordChannel)
   228  	close(errorChannel)
   229  }
   230  
   231  func (c *ApiService) getNextListAccountConfigResponse(nextPageUrl string) (interface{}, error) {
   232  	if nextPageUrl == "" {
   233  		return nil, nil
   234  	}
   235  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	defer resp.Body.Close()
   241  
   242  	ps := &ListAccountConfigResponse{}
   243  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   244  		return nil, err
   245  	}
   246  	return ps, nil
   247  }
   248  
   249  // Optional parameters for the method 'UpdateAccountConfig'
   250  type UpdateAccountConfigParams struct {
   251  	// The config value; up to 4096 characters.
   252  	Value *string `json:"Value,omitempty"`
   253  }
   254  
   255  func (params *UpdateAccountConfigParams) SetValue(Value string) *UpdateAccountConfigParams {
   256  	params.Value = &Value
   257  	return params
   258  }
   259  
   260  // Update a config for an Account.
   261  func (c *ApiService) UpdateAccountConfig(Key string, params *UpdateAccountConfigParams) (*MicrovisorV1AccountConfig, error) {
   262  	path := "/v1/Configs/{Key}"
   263  	path = strings.Replace(path, "{"+"Key"+"}", Key, -1)
   264  
   265  	data := url.Values{}
   266  	headers := make(map[string]interface{})
   267  
   268  	if params != nil && params.Value != nil {
   269  		data.Set("Value", *params.Value)
   270  	}
   271  
   272  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  
   277  	defer resp.Body.Close()
   278  
   279  	ps := &MicrovisorV1AccountConfig{}
   280  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   281  		return nil, err
   282  	}
   283  
   284  	return ps, err
   285  }