github.com/twilio/twilio-go@v1.20.1/rest/sync/v1/services.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 'CreateService'
    27  type CreateServiceParams struct {
    28  	// A string that you assign to describe the resource.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// The URL we should call when Sync objects are manipulated.
    31  	WebhookUrl *string `json:"WebhookUrl,omitempty"`
    32  	// Whether the service instance should call `webhook_url` when client endpoints connect to Sync. The default is `false`.
    33  	ReachabilityWebhooksEnabled *bool `json:"ReachabilityWebhooksEnabled,omitempty"`
    34  	// Whether token identities in the Service must be granted access to Sync objects by using the [Permissions](https://www.twilio.com/docs/sync/api/sync-permissions) resource.
    35  	AclEnabled *bool `json:"AclEnabled,omitempty"`
    36  	// Whether every `endpoint_disconnected` event should occur after a configurable delay. The default is `false`, where the `endpoint_disconnected` event occurs immediately after disconnection. When `true`, intervening reconnections can prevent the `endpoint_disconnected` event.
    37  	ReachabilityDebouncingEnabled *bool `json:"ReachabilityDebouncingEnabled,omitempty"`
    38  	// The reachability event delay in milliseconds if `reachability_debouncing_enabled` = `true`.  Must be between 1,000 and 30,000 and defaults to 5,000. This is the number of milliseconds after the last running client disconnects, and a Sync identity is declared offline, before the `webhook_url` is called if all endpoints remain offline. A reconnection from the same identity by any endpoint during this interval prevents the call to `webhook_url`.
    39  	ReachabilityDebouncingWindow *int `json:"ReachabilityDebouncingWindow,omitempty"`
    40  	// Whether the Service instance should call `webhook_url` when the REST API is used to update Sync objects. The default is `false`.
    41  	WebhooksFromRestEnabled *bool `json:"WebhooksFromRestEnabled,omitempty"`
    42  }
    43  
    44  func (params *CreateServiceParams) SetFriendlyName(FriendlyName string) *CreateServiceParams {
    45  	params.FriendlyName = &FriendlyName
    46  	return params
    47  }
    48  func (params *CreateServiceParams) SetWebhookUrl(WebhookUrl string) *CreateServiceParams {
    49  	params.WebhookUrl = &WebhookUrl
    50  	return params
    51  }
    52  func (params *CreateServiceParams) SetReachabilityWebhooksEnabled(ReachabilityWebhooksEnabled bool) *CreateServiceParams {
    53  	params.ReachabilityWebhooksEnabled = &ReachabilityWebhooksEnabled
    54  	return params
    55  }
    56  func (params *CreateServiceParams) SetAclEnabled(AclEnabled bool) *CreateServiceParams {
    57  	params.AclEnabled = &AclEnabled
    58  	return params
    59  }
    60  func (params *CreateServiceParams) SetReachabilityDebouncingEnabled(ReachabilityDebouncingEnabled bool) *CreateServiceParams {
    61  	params.ReachabilityDebouncingEnabled = &ReachabilityDebouncingEnabled
    62  	return params
    63  }
    64  func (params *CreateServiceParams) SetReachabilityDebouncingWindow(ReachabilityDebouncingWindow int) *CreateServiceParams {
    65  	params.ReachabilityDebouncingWindow = &ReachabilityDebouncingWindow
    66  	return params
    67  }
    68  func (params *CreateServiceParams) SetWebhooksFromRestEnabled(WebhooksFromRestEnabled bool) *CreateServiceParams {
    69  	params.WebhooksFromRestEnabled = &WebhooksFromRestEnabled
    70  	return params
    71  }
    72  
    73  //
    74  func (c *ApiService) CreateService(params *CreateServiceParams) (*SyncV1Service, error) {
    75  	path := "/v1/Services"
    76  
    77  	data := url.Values{}
    78  	headers := make(map[string]interface{})
    79  
    80  	if params != nil && params.FriendlyName != nil {
    81  		data.Set("FriendlyName", *params.FriendlyName)
    82  	}
    83  	if params != nil && params.WebhookUrl != nil {
    84  		data.Set("WebhookUrl", *params.WebhookUrl)
    85  	}
    86  	if params != nil && params.ReachabilityWebhooksEnabled != nil {
    87  		data.Set("ReachabilityWebhooksEnabled", fmt.Sprint(*params.ReachabilityWebhooksEnabled))
    88  	}
    89  	if params != nil && params.AclEnabled != nil {
    90  		data.Set("AclEnabled", fmt.Sprint(*params.AclEnabled))
    91  	}
    92  	if params != nil && params.ReachabilityDebouncingEnabled != nil {
    93  		data.Set("ReachabilityDebouncingEnabled", fmt.Sprint(*params.ReachabilityDebouncingEnabled))
    94  	}
    95  	if params != nil && params.ReachabilityDebouncingWindow != nil {
    96  		data.Set("ReachabilityDebouncingWindow", fmt.Sprint(*params.ReachabilityDebouncingWindow))
    97  	}
    98  	if params != nil && params.WebhooksFromRestEnabled != nil {
    99  		data.Set("WebhooksFromRestEnabled", fmt.Sprint(*params.WebhooksFromRestEnabled))
   100  	}
   101  
   102  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	defer resp.Body.Close()
   108  
   109  	ps := &SyncV1Service{}
   110  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return ps, err
   115  }
   116  
   117  //
   118  func (c *ApiService) DeleteService(Sid string) error {
   119  	path := "/v1/Services/{Sid}"
   120  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   121  
   122  	data := url.Values{}
   123  	headers := make(map[string]interface{})
   124  
   125  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	defer resp.Body.Close()
   131  
   132  	return nil
   133  }
   134  
   135  //
   136  func (c *ApiService) FetchService(Sid string) (*SyncV1Service, error) {
   137  	path := "/v1/Services/{Sid}"
   138  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   139  
   140  	data := url.Values{}
   141  	headers := make(map[string]interface{})
   142  
   143  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	defer resp.Body.Close()
   149  
   150  	ps := &SyncV1Service{}
   151  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	return ps, err
   156  }
   157  
   158  // Optional parameters for the method 'ListService'
   159  type ListServiceParams struct {
   160  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   161  	PageSize *int `json:"PageSize,omitempty"`
   162  	// Max number of records to return.
   163  	Limit *int `json:"limit,omitempty"`
   164  }
   165  
   166  func (params *ListServiceParams) SetPageSize(PageSize int) *ListServiceParams {
   167  	params.PageSize = &PageSize
   168  	return params
   169  }
   170  func (params *ListServiceParams) SetLimit(Limit int) *ListServiceParams {
   171  	params.Limit = &Limit
   172  	return params
   173  }
   174  
   175  // Retrieve a single page of Service records from the API. Request is executed immediately.
   176  func (c *ApiService) PageService(params *ListServiceParams, pageToken, pageNumber string) (*ListServiceResponse, error) {
   177  	path := "/v1/Services"
   178  
   179  	data := url.Values{}
   180  	headers := make(map[string]interface{})
   181  
   182  	if params != nil && params.PageSize != nil {
   183  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   184  	}
   185  
   186  	if pageToken != "" {
   187  		data.Set("PageToken", pageToken)
   188  	}
   189  	if pageNumber != "" {
   190  		data.Set("Page", pageNumber)
   191  	}
   192  
   193  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	defer resp.Body.Close()
   199  
   200  	ps := &ListServiceResponse{}
   201  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return ps, err
   206  }
   207  
   208  // Lists Service records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   209  func (c *ApiService) ListService(params *ListServiceParams) ([]SyncV1Service, error) {
   210  	response, errors := c.StreamService(params)
   211  
   212  	records := make([]SyncV1Service, 0)
   213  	for record := range response {
   214  		records = append(records, record)
   215  	}
   216  
   217  	if err := <-errors; err != nil {
   218  		return nil, err
   219  	}
   220  
   221  	return records, nil
   222  }
   223  
   224  // Streams Service records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   225  func (c *ApiService) StreamService(params *ListServiceParams) (chan SyncV1Service, chan error) {
   226  	if params == nil {
   227  		params = &ListServiceParams{}
   228  	}
   229  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   230  
   231  	recordChannel := make(chan SyncV1Service, 1)
   232  	errorChannel := make(chan error, 1)
   233  
   234  	response, err := c.PageService(params, "", "")
   235  	if err != nil {
   236  		errorChannel <- err
   237  		close(recordChannel)
   238  		close(errorChannel)
   239  	} else {
   240  		go c.streamService(response, params, recordChannel, errorChannel)
   241  	}
   242  
   243  	return recordChannel, errorChannel
   244  }
   245  
   246  func (c *ApiService) streamService(response *ListServiceResponse, params *ListServiceParams, recordChannel chan SyncV1Service, errorChannel chan error) {
   247  	curRecord := 1
   248  
   249  	for response != nil {
   250  		responseRecords := response.Services
   251  		for item := range responseRecords {
   252  			recordChannel <- responseRecords[item]
   253  			curRecord += 1
   254  			if params.Limit != nil && *params.Limit < curRecord {
   255  				close(recordChannel)
   256  				close(errorChannel)
   257  				return
   258  			}
   259  		}
   260  
   261  		record, err := client.GetNext(c.baseURL, response, c.getNextListServiceResponse)
   262  		if err != nil {
   263  			errorChannel <- err
   264  			break
   265  		} else if record == nil {
   266  			break
   267  		}
   268  
   269  		response = record.(*ListServiceResponse)
   270  	}
   271  
   272  	close(recordChannel)
   273  	close(errorChannel)
   274  }
   275  
   276  func (c *ApiService) getNextListServiceResponse(nextPageUrl string) (interface{}, error) {
   277  	if nextPageUrl == "" {
   278  		return nil, nil
   279  	}
   280  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   281  	if err != nil {
   282  		return nil, err
   283  	}
   284  
   285  	defer resp.Body.Close()
   286  
   287  	ps := &ListServiceResponse{}
   288  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   289  		return nil, err
   290  	}
   291  	return ps, nil
   292  }
   293  
   294  // Optional parameters for the method 'UpdateService'
   295  type UpdateServiceParams struct {
   296  	// The URL we should call when Sync objects are manipulated.
   297  	WebhookUrl *string `json:"WebhookUrl,omitempty"`
   298  	// A string that you assign to describe the resource.
   299  	FriendlyName *string `json:"FriendlyName,omitempty"`
   300  	// Whether the service instance should call `webhook_url` when client endpoints connect to Sync. The default is `false`.
   301  	ReachabilityWebhooksEnabled *bool `json:"ReachabilityWebhooksEnabled,omitempty"`
   302  	// Whether token identities in the Service must be granted access to Sync objects by using the [Permissions](https://www.twilio.com/docs/sync/api/sync-permissions) resource.
   303  	AclEnabled *bool `json:"AclEnabled,omitempty"`
   304  	// Whether every `endpoint_disconnected` event should occur after a configurable delay. The default is `false`, where the `endpoint_disconnected` event occurs immediately after disconnection. When `true`, intervening reconnections can prevent the `endpoint_disconnected` event.
   305  	ReachabilityDebouncingEnabled *bool `json:"ReachabilityDebouncingEnabled,omitempty"`
   306  	// The reachability event delay in milliseconds if `reachability_debouncing_enabled` = `true`.  Must be between 1,000 and 30,000 and defaults to 5,000. This is the number of milliseconds after the last running client disconnects, and a Sync identity is declared offline, before the webhook is called if all endpoints remain offline. A reconnection from the same identity by any endpoint during this interval prevents the webhook from being called.
   307  	ReachabilityDebouncingWindow *int `json:"ReachabilityDebouncingWindow,omitempty"`
   308  	// Whether the Service instance should call `webhook_url` when the REST API is used to update Sync objects. The default is `false`.
   309  	WebhooksFromRestEnabled *bool `json:"WebhooksFromRestEnabled,omitempty"`
   310  }
   311  
   312  func (params *UpdateServiceParams) SetWebhookUrl(WebhookUrl string) *UpdateServiceParams {
   313  	params.WebhookUrl = &WebhookUrl
   314  	return params
   315  }
   316  func (params *UpdateServiceParams) SetFriendlyName(FriendlyName string) *UpdateServiceParams {
   317  	params.FriendlyName = &FriendlyName
   318  	return params
   319  }
   320  func (params *UpdateServiceParams) SetReachabilityWebhooksEnabled(ReachabilityWebhooksEnabled bool) *UpdateServiceParams {
   321  	params.ReachabilityWebhooksEnabled = &ReachabilityWebhooksEnabled
   322  	return params
   323  }
   324  func (params *UpdateServiceParams) SetAclEnabled(AclEnabled bool) *UpdateServiceParams {
   325  	params.AclEnabled = &AclEnabled
   326  	return params
   327  }
   328  func (params *UpdateServiceParams) SetReachabilityDebouncingEnabled(ReachabilityDebouncingEnabled bool) *UpdateServiceParams {
   329  	params.ReachabilityDebouncingEnabled = &ReachabilityDebouncingEnabled
   330  	return params
   331  }
   332  func (params *UpdateServiceParams) SetReachabilityDebouncingWindow(ReachabilityDebouncingWindow int) *UpdateServiceParams {
   333  	params.ReachabilityDebouncingWindow = &ReachabilityDebouncingWindow
   334  	return params
   335  }
   336  func (params *UpdateServiceParams) SetWebhooksFromRestEnabled(WebhooksFromRestEnabled bool) *UpdateServiceParams {
   337  	params.WebhooksFromRestEnabled = &WebhooksFromRestEnabled
   338  	return params
   339  }
   340  
   341  //
   342  func (c *ApiService) UpdateService(Sid string, params *UpdateServiceParams) (*SyncV1Service, error) {
   343  	path := "/v1/Services/{Sid}"
   344  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   345  
   346  	data := url.Values{}
   347  	headers := make(map[string]interface{})
   348  
   349  	if params != nil && params.WebhookUrl != nil {
   350  		data.Set("WebhookUrl", *params.WebhookUrl)
   351  	}
   352  	if params != nil && params.FriendlyName != nil {
   353  		data.Set("FriendlyName", *params.FriendlyName)
   354  	}
   355  	if params != nil && params.ReachabilityWebhooksEnabled != nil {
   356  		data.Set("ReachabilityWebhooksEnabled", fmt.Sprint(*params.ReachabilityWebhooksEnabled))
   357  	}
   358  	if params != nil && params.AclEnabled != nil {
   359  		data.Set("AclEnabled", fmt.Sprint(*params.AclEnabled))
   360  	}
   361  	if params != nil && params.ReachabilityDebouncingEnabled != nil {
   362  		data.Set("ReachabilityDebouncingEnabled", fmt.Sprint(*params.ReachabilityDebouncingEnabled))
   363  	}
   364  	if params != nil && params.ReachabilityDebouncingWindow != nil {
   365  		data.Set("ReachabilityDebouncingWindow", fmt.Sprint(*params.ReachabilityDebouncingWindow))
   366  	}
   367  	if params != nil && params.WebhooksFromRestEnabled != nil {
   368  		data.Set("WebhooksFromRestEnabled", fmt.Sprint(*params.WebhooksFromRestEnabled))
   369  	}
   370  
   371  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   372  	if err != nil {
   373  		return nil, err
   374  	}
   375  
   376  	defer resp.Body.Close()
   377  
   378  	ps := &SyncV1Service{}
   379  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   380  		return nil, err
   381  	}
   382  
   383  	return ps, err
   384  }