github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services_lists.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 'CreateSyncList'
    27  type CreateSyncListParams struct {
    28  	// An application-defined string that uniquely identifies the resource. This value must be unique within its Service and it can be up to 320 characters long. The `unique_name` value can be used as an alternative to the `sid` in the URL path to address the resource.
    29  	UniqueName *string `json:"UniqueName,omitempty"`
    30  	// Alias for collection_ttl. If both 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 Sync List expires (time-to-live) and is deleted.
    33  	CollectionTtl *int `json:"CollectionTtl,omitempty"`
    34  }
    35  
    36  func (params *CreateSyncListParams) SetUniqueName(UniqueName string) *CreateSyncListParams {
    37  	params.UniqueName = &UniqueName
    38  	return params
    39  }
    40  func (params *CreateSyncListParams) SetTtl(Ttl int) *CreateSyncListParams {
    41  	params.Ttl = &Ttl
    42  	return params
    43  }
    44  func (params *CreateSyncListParams) SetCollectionTtl(CollectionTtl int) *CreateSyncListParams {
    45  	params.CollectionTtl = &CollectionTtl
    46  	return params
    47  }
    48  
    49  //
    50  func (c *ApiService) CreateSyncList(ServiceSid string, params *CreateSyncListParams) (*SyncV1SyncList, error) {
    51  	path := "/v1/Services/{ServiceSid}/Lists"
    52  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	if params != nil && params.UniqueName != nil {
    58  		data.Set("UniqueName", *params.UniqueName)
    59  	}
    60  	if params != nil && params.Ttl != nil {
    61  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
    62  	}
    63  	if params != nil && params.CollectionTtl != nil {
    64  		data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl))
    65  	}
    66  
    67  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    68  	if err != nil {
    69  		return nil, err
    70  	}
    71  
    72  	defer resp.Body.Close()
    73  
    74  	ps := &SyncV1SyncList{}
    75  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	return ps, err
    80  }
    81  
    82  //
    83  func (c *ApiService) DeleteSyncList(ServiceSid string, Sid string) error {
    84  	path := "/v1/Services/{ServiceSid}/Lists/{Sid}"
    85  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    86  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    87  
    88  	data := url.Values{}
    89  	headers := make(map[string]interface{})
    90  
    91  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    92  	if err != nil {
    93  		return err
    94  	}
    95  
    96  	defer resp.Body.Close()
    97  
    98  	return nil
    99  }
   100  
   101  //
   102  func (c *ApiService) FetchSyncList(ServiceSid string, Sid string) (*SyncV1SyncList, error) {
   103  	path := "/v1/Services/{ServiceSid}/Lists/{Sid}"
   104  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   105  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   106  
   107  	data := url.Values{}
   108  	headers := make(map[string]interface{})
   109  
   110  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	defer resp.Body.Close()
   116  
   117  	ps := &SyncV1SyncList{}
   118  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return ps, err
   123  }
   124  
   125  // Optional parameters for the method 'ListSyncList'
   126  type ListSyncListParams struct {
   127  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   128  	PageSize *int `json:"PageSize,omitempty"`
   129  	// Max number of records to return.
   130  	Limit *int `json:"limit,omitempty"`
   131  }
   132  
   133  func (params *ListSyncListParams) SetPageSize(PageSize int) *ListSyncListParams {
   134  	params.PageSize = &PageSize
   135  	return params
   136  }
   137  func (params *ListSyncListParams) SetLimit(Limit int) *ListSyncListParams {
   138  	params.Limit = &Limit
   139  	return params
   140  }
   141  
   142  // Retrieve a single page of SyncList records from the API. Request is executed immediately.
   143  func (c *ApiService) PageSyncList(ServiceSid string, params *ListSyncListParams, pageToken, pageNumber string) (*ListSyncListResponse, error) {
   144  	path := "/v1/Services/{ServiceSid}/Lists"
   145  
   146  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   147  
   148  	data := url.Values{}
   149  	headers := make(map[string]interface{})
   150  
   151  	if params != nil && params.PageSize != nil {
   152  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   153  	}
   154  
   155  	if pageToken != "" {
   156  		data.Set("PageToken", pageToken)
   157  	}
   158  	if pageNumber != "" {
   159  		data.Set("Page", pageNumber)
   160  	}
   161  
   162  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   163  	if err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	defer resp.Body.Close()
   168  
   169  	ps := &ListSyncListResponse{}
   170  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   171  		return nil, err
   172  	}
   173  
   174  	return ps, err
   175  }
   176  
   177  // Lists SyncList records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   178  func (c *ApiService) ListSyncList(ServiceSid string, params *ListSyncListParams) ([]SyncV1SyncList, error) {
   179  	response, errors := c.StreamSyncList(ServiceSid, params)
   180  
   181  	records := make([]SyncV1SyncList, 0)
   182  	for record := range response {
   183  		records = append(records, record)
   184  	}
   185  
   186  	if err := <-errors; err != nil {
   187  		return nil, err
   188  	}
   189  
   190  	return records, nil
   191  }
   192  
   193  // Streams SyncList records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   194  func (c *ApiService) StreamSyncList(ServiceSid string, params *ListSyncListParams) (chan SyncV1SyncList, chan error) {
   195  	if params == nil {
   196  		params = &ListSyncListParams{}
   197  	}
   198  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   199  
   200  	recordChannel := make(chan SyncV1SyncList, 1)
   201  	errorChannel := make(chan error, 1)
   202  
   203  	response, err := c.PageSyncList(ServiceSid, params, "", "")
   204  	if err != nil {
   205  		errorChannel <- err
   206  		close(recordChannel)
   207  		close(errorChannel)
   208  	} else {
   209  		go c.streamSyncList(response, params, recordChannel, errorChannel)
   210  	}
   211  
   212  	return recordChannel, errorChannel
   213  }
   214  
   215  func (c *ApiService) streamSyncList(response *ListSyncListResponse, params *ListSyncListParams, recordChannel chan SyncV1SyncList, errorChannel chan error) {
   216  	curRecord := 1
   217  
   218  	for response != nil {
   219  		responseRecords := response.Lists
   220  		for item := range responseRecords {
   221  			recordChannel <- responseRecords[item]
   222  			curRecord += 1
   223  			if params.Limit != nil && *params.Limit < curRecord {
   224  				close(recordChannel)
   225  				close(errorChannel)
   226  				return
   227  			}
   228  		}
   229  
   230  		record, err := client.GetNext(c.baseURL, response, c.getNextListSyncListResponse)
   231  		if err != nil {
   232  			errorChannel <- err
   233  			break
   234  		} else if record == nil {
   235  			break
   236  		}
   237  
   238  		response = record.(*ListSyncListResponse)
   239  	}
   240  
   241  	close(recordChannel)
   242  	close(errorChannel)
   243  }
   244  
   245  func (c *ApiService) getNextListSyncListResponse(nextPageUrl string) (interface{}, error) {
   246  	if nextPageUrl == "" {
   247  		return nil, nil
   248  	}
   249  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	defer resp.Body.Close()
   255  
   256  	ps := &ListSyncListResponse{}
   257  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   258  		return nil, err
   259  	}
   260  	return ps, nil
   261  }
   262  
   263  // Optional parameters for the method 'UpdateSyncList'
   264  type UpdateSyncListParams struct {
   265  	// An alias for `collection_ttl`. If both are provided, this value is ignored.
   266  	Ttl *int `json:"Ttl,omitempty"`
   267  	// How long, [in seconds](https://www.twilio.com/docs/sync/limits#sync-payload-limits), before the Sync List expires (time-to-live) and is deleted.
   268  	CollectionTtl *int `json:"CollectionTtl,omitempty"`
   269  }
   270  
   271  func (params *UpdateSyncListParams) SetTtl(Ttl int) *UpdateSyncListParams {
   272  	params.Ttl = &Ttl
   273  	return params
   274  }
   275  func (params *UpdateSyncListParams) SetCollectionTtl(CollectionTtl int) *UpdateSyncListParams {
   276  	params.CollectionTtl = &CollectionTtl
   277  	return params
   278  }
   279  
   280  //
   281  func (c *ApiService) UpdateSyncList(ServiceSid string, Sid string, params *UpdateSyncListParams) (*SyncV1SyncList, error) {
   282  	path := "/v1/Services/{ServiceSid}/Lists/{Sid}"
   283  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   284  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   285  
   286  	data := url.Values{}
   287  	headers := make(map[string]interface{})
   288  
   289  	if params != nil && params.Ttl != nil {
   290  		data.Set("Ttl", fmt.Sprint(*params.Ttl))
   291  	}
   292  	if params != nil && params.CollectionTtl != nil {
   293  		data.Set("CollectionTtl", fmt.Sprint(*params.CollectionTtl))
   294  	}
   295  
   296  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   297  	if err != nil {
   298  		return nil, err
   299  	}
   300  
   301  	defer resp.Body.Close()
   302  
   303  	ps := &SyncV1SyncList{}
   304  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   305  		return nil, err
   306  	}
   307  
   308  	return ps, err
   309  }