github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_lists_items.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Sync
     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 'CreateSyncListItem'
    27  type CreateSyncListItemParams struct {
    28  	// A JSON string that represents an arbitrary, schema-less object that the List Item stores. Can be up to 16 KiB in length.
    29  	Data *interface{} `json:"Data,omitempty"`
    30  	// An alias for `item_ttl`. If both parameters are provided, this value is ignored.
    31  	Ttl *int `json:"Ttl,omitempty"`
    32  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the List Item expires (time-to-live) and is deleted.
    33  	ItemTtl *int `json:"ItemTtl,omitempty"`
    34  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the List Item's parent Sync List expires (time-to-live) and is deleted.
    35  	CollectionTtl *int `json:"CollectionTtl,omitempty"`
    36  }
    37  
    38  func (params *CreateSyncListItemParams) SetData(Data interface{}) *CreateSyncListItemParams {
    39  	params.Data = &Data
    40  	return params
    41  }
    42  func (params *CreateSyncListItemParams) SetTtl(Ttl int) *CreateSyncListItemParams {
    43  	params.Ttl = &Ttl
    44  	return params
    45  }
    46  func (params *CreateSyncListItemParams) SetItemTtl(ItemTtl int) *CreateSyncListItemParams {
    47  	params.ItemTtl = &ItemTtl
    48  	return params
    49  }
    50  func (params *CreateSyncListItemParams) SetCollectionTtl(CollectionTtl int) *CreateSyncListItemParams {
    51  	params.CollectionTtl = &CollectionTtl
    52  	return params
    53  }
    54  
    55  //
    56  func (c *ApiService) CreateSyncListItem(ServiceSid string, ListSid string, params *CreateSyncListItemParams) (*SyncV1SyncListItem, error) {
    57  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Items"
    58  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    59  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
    60  
    61  	data := url.Values{}
    62  	headers := make(map[string]interface{})
    63  
    64  	if params != nil && params.Data != nil {
    65  		v, err := json.Marshal(params.Data)
    66  
    67  		if err != nil {
    68  			return nil, err
    69  		}
    70  
    71  		data.Set("Data", string(v))
    72  	}
    73  	if params != nil && params.Ttl != nil {
    74  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
    75  	}
    76  	if params != nil && params.ItemTtl != nil {
    77  		data.Set("ItemTtl", fmt.Sprint(*params.ItemTtl))
    78  	}
    79  	if params != nil && params.CollectionTtl != nil {
    80  		data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl))
    81  	}
    82  
    83  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	defer resp.Body.Close()
    89  
    90  	ps := &SyncV1SyncListItem{}
    91  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	return ps, err
    96  }
    97  
    98  // Optional parameters for the method 'DeleteSyncListItem'
    99  type DeleteSyncListItemParams struct {
   100  	// If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match).
   101  	IfMatch *string `json:"If-Match,omitempty"`
   102  }
   103  
   104  func (params *DeleteSyncListItemParams) SetIfMatch(IfMatch string) *DeleteSyncListItemParams {
   105  	params.IfMatch = &IfMatch
   106  	return params
   107  }
   108  
   109  //
   110  func (c *ApiService) DeleteSyncListItem(ServiceSid string, ListSid string, Index int, params *DeleteSyncListItemParams) error {
   111  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}"
   112  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   113  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
   114  	path = strings.Replace(path, "{"+"Index"+"}", fmt.Sprint(Index), -1)
   115  
   116  	data := url.Values{}
   117  	headers := make(map[string]interface{})
   118  
   119  	if params != nil && params.IfMatch != nil {
   120  		headers["If-Match"] = *params.IfMatch
   121  	}
   122  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   123  	if err != nil {
   124  		return err
   125  	}
   126  
   127  	defer resp.Body.Close()
   128  
   129  	return nil
   130  }
   131  
   132  //
   133  func (c *ApiService) FetchSyncListItem(ServiceSid string, ListSid string, Index int) (*SyncV1SyncListItem, error) {
   134  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}"
   135  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   136  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
   137  	path = strings.Replace(path, "{"+"Index"+"}", fmt.Sprint(Index), -1)
   138  
   139  	data := url.Values{}
   140  	headers := make(map[string]interface{})
   141  
   142  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	defer resp.Body.Close()
   148  
   149  	ps := &SyncV1SyncListItem{}
   150  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	return ps, err
   155  }
   156  
   157  // Optional parameters for the method 'ListSyncListItem'
   158  type ListSyncListItemParams struct {
   159  	// How to order the List Items returned by their `index` value. Can be: `asc` (ascending) or `desc` (descending) and the default is ascending.
   160  	Order *string `json:"Order,omitempty"`
   161  	// The `index` of the first Sync List Item resource to read. See also `bounds`.
   162  	From *string `json:"From,omitempty"`
   163  	// Whether to include the List Item referenced by the `from` parameter. Can be: `inclusive` to include the List Item referenced by the `from` parameter or `exclusive` to start with the next List Item. The default value is `inclusive`.
   164  	Bounds *string `json:"Bounds,omitempty"`
   165  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   166  	PageSize *int `json:"PageSize,omitempty"`
   167  	// Max number of records to return.
   168  	Limit *int `json:"limit,omitempty"`
   169  }
   170  
   171  func (params *ListSyncListItemParams) SetOrder(Order string) *ListSyncListItemParams {
   172  	params.Order = &Order
   173  	return params
   174  }
   175  func (params *ListSyncListItemParams) SetFrom(From string) *ListSyncListItemParams {
   176  	params.From = &From
   177  	return params
   178  }
   179  func (params *ListSyncListItemParams) SetBounds(Bounds string) *ListSyncListItemParams {
   180  	params.Bounds = &Bounds
   181  	return params
   182  }
   183  func (params *ListSyncListItemParams) SetPageSize(PageSize int) *ListSyncListItemParams {
   184  	params.PageSize = &PageSize
   185  	return params
   186  }
   187  func (params *ListSyncListItemParams) SetLimit(Limit int) *ListSyncListItemParams {
   188  	params.Limit = &Limit
   189  	return params
   190  }
   191  
   192  // Retrieve a single page of SyncListItem records from the API. Request is executed immediately.
   193  func (c *ApiService) PageSyncListItem(ServiceSid string, ListSid string, params *ListSyncListItemParams, pageToken, pageNumber string) (*ListSyncListItemResponse, error) {
   194  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Items"
   195  
   196  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   197  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
   198  
   199  	data := url.Values{}
   200  	headers := make(map[string]interface{})
   201  
   202  	if params != nil && params.Order != nil {
   203  		data.Set("Order", *params.Order)
   204  	}
   205  	if params != nil && params.From != nil {
   206  		data.Set("From", *params.From)
   207  	}
   208  	if params != nil && params.Bounds != nil {
   209  		data.Set("Bounds", *params.Bounds)
   210  	}
   211  	if params != nil && params.PageSize != nil {
   212  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   213  	}
   214  
   215  	if pageToken != "" {
   216  		data.Set("PageToken", pageToken)
   217  	}
   218  	if pageNumber != "" {
   219  		data.Set("Page", pageNumber)
   220  	}
   221  
   222  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  
   227  	defer resp.Body.Close()
   228  
   229  	ps := &ListSyncListItemResponse{}
   230  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   231  		return nil, err
   232  	}
   233  
   234  	return ps, err
   235  }
   236  
   237  // Lists SyncListItem records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   238  func (c *ApiService) ListSyncListItem(ServiceSid string, ListSid string, params *ListSyncListItemParams) ([]SyncV1SyncListItem, error) {
   239  	response, errors := c.StreamSyncListItem(ServiceSid, ListSid, params)
   240  
   241  	records := make([]SyncV1SyncListItem, 0)
   242  	for record := range response {
   243  		records = append(records, record)
   244  	}
   245  
   246  	if err := <-errors; err != nil {
   247  		return nil, err
   248  	}
   249  
   250  	return records, nil
   251  }
   252  
   253  // Streams SyncListItem records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   254  func (c *ApiService) StreamSyncListItem(ServiceSid string, ListSid string, params *ListSyncListItemParams) (chan SyncV1SyncListItem, chan error) {
   255  	if params == nil {
   256  		params = &ListSyncListItemParams{}
   257  	}
   258  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   259  
   260  	recordChannel := make(chan SyncV1SyncListItem, 1)
   261  	errorChannel := make(chan error, 1)
   262  
   263  	response, err := c.PageSyncListItem(ServiceSid, ListSid, params, "", "")
   264  	if err != nil {
   265  		errorChannel <- err
   266  		close(recordChannel)
   267  		close(errorChannel)
   268  	} else {
   269  		go c.streamSyncListItem(response, params, recordChannel, errorChannel)
   270  	}
   271  
   272  	return recordChannel, errorChannel
   273  }
   274  
   275  func (c *ApiService) streamSyncListItem(response *ListSyncListItemResponse, params *ListSyncListItemParams, recordChannel chan SyncV1SyncListItem, errorChannel chan error) {
   276  	curRecord := 1
   277  
   278  	for response != nil {
   279  		responseRecords := response.Items
   280  		for item := range responseRecords {
   281  			recordChannel <- responseRecords[item]
   282  			curRecord += 1
   283  			if params.Limit != nil && *params.Limit < curRecord {
   284  				close(recordChannel)
   285  				close(errorChannel)
   286  				return
   287  			}
   288  		}
   289  
   290  		record, err := client.GetNext(c.baseURL, response, c.getNextListSyncListItemResponse)
   291  		if err != nil {
   292  			errorChannel <- err
   293  			break
   294  		} else if record == nil {
   295  			break
   296  		}
   297  
   298  		response = record.(*ListSyncListItemResponse)
   299  	}
   300  
   301  	close(recordChannel)
   302  	close(errorChannel)
   303  }
   304  
   305  func (c *ApiService) getNextListSyncListItemResponse(nextPageUrl string) (interface{}, error) {
   306  	if nextPageUrl == "" {
   307  		return nil, nil
   308  	}
   309  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   310  	if err != nil {
   311  		return nil, err
   312  	}
   313  
   314  	defer resp.Body.Close()
   315  
   316  	ps := &ListSyncListItemResponse{}
   317  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   318  		return nil, err
   319  	}
   320  	return ps, nil
   321  }
   322  
   323  // Optional parameters for the method 'UpdateSyncListItem'
   324  type UpdateSyncListItemParams struct {
   325  	// If provided, applies this mutation if (and only if) the “revision” field of this [map item] matches the provided value. This matches the semantics of (and is implemented with) the HTTP [If-Match header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match).
   326  	IfMatch *string `json:"If-Match,omitempty"`
   327  	// A JSON string that represents an arbitrary, schema-less object that the List Item stores. Can be up to 16 KiB in length.
   328  	Data *interface{} `json:"Data,omitempty"`
   329  	// An alias for `item_ttl`. If both parameters are provided, this value is ignored.
   330  	Ttl *int `json:"Ttl,omitempty"`
   331  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the List Item expires (time-to-live) and is deleted.
   332  	ItemTtl *int `json:"ItemTtl,omitempty"`
   333  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the List Item's parent Sync List expires (time-to-live) and is deleted. This parameter can only be used when the List Item's `data` or `ttl` is updated in the same request.
   334  	CollectionTtl *int `json:"CollectionTtl,omitempty"`
   335  }
   336  
   337  func (params *UpdateSyncListItemParams) SetIfMatch(IfMatch string) *UpdateSyncListItemParams {
   338  	params.IfMatch = &IfMatch
   339  	return params
   340  }
   341  func (params *UpdateSyncListItemParams) SetData(Data interface{}) *UpdateSyncListItemParams {
   342  	params.Data = &Data
   343  	return params
   344  }
   345  func (params *UpdateSyncListItemParams) SetTtl(Ttl int) *UpdateSyncListItemParams {
   346  	params.Ttl = &Ttl
   347  	return params
   348  }
   349  func (params *UpdateSyncListItemParams) SetItemTtl(ItemTtl int) *UpdateSyncListItemParams {
   350  	params.ItemTtl = &ItemTtl
   351  	return params
   352  }
   353  func (params *UpdateSyncListItemParams) SetCollectionTtl(CollectionTtl int) *UpdateSyncListItemParams {
   354  	params.CollectionTtl = &CollectionTtl
   355  	return params
   356  }
   357  
   358  //
   359  func (c *ApiService) UpdateSyncListItem(ServiceSid string, ListSid string, Index int, params *UpdateSyncListItemParams) (*SyncV1SyncListItem, error) {
   360  	path := "/v1/Services/{ServiceSid}/Lists/{ListSid}/Items/{Index}"
   361  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   362  	path = strings.Replace(path, "{"+"ListSid"+"}", ListSid, -1)
   363  	path = strings.Replace(path, "{"+"Index"+"}", fmt.Sprint(Index), -1)
   364  
   365  	data := url.Values{}
   366  	headers := make(map[string]interface{})
   367  
   368  	if params != nil && params.Data != nil {
   369  		v, err := json.Marshal(params.Data)
   370  
   371  		if err != nil {
   372  			return nil, err
   373  		}
   374  
   375  		data.Set("Data", string(v))
   376  	}
   377  	if params != nil && params.Ttl != nil {
   378  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
   379  	}
   380  	if params != nil && params.ItemTtl != nil {
   381  		data.Set("ItemTtl", fmt.Sprint(*params.ItemTtl))
   382  	}
   383  	if params != nil && params.CollectionTtl != nil {
   384  		data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl))
   385  	}
   386  
   387  	if params != nil && params.IfMatch != nil {
   388  		headers["If-Match"] = *params.IfMatch
   389  	}
   390  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   391  	if err != nil {
   392  		return nil, err
   393  	}
   394  
   395  	defer resp.Body.Close()
   396  
   397  	ps := &SyncV1SyncListItem{}
   398  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   399  		return nil, err
   400  	}
   401  
   402  	return ps, err
   403  }