github.com/twilio/twilio-go@v1.20.1/rest/notify/v1/services_bindings.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Notify
     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 'CreateBinding'
    27  type CreateBindingParams struct {
    28  	// The `identity` value that uniquely identifies the new resource's [User](https://www.twilio.com/docs/chat/rest/user-resource) within the [Service](https://www.twilio.com/docs/notify/api/service-resource). Up to 20 Bindings can be created for the same Identity in a given Service.
    29  	Identity *string `json:"Identity,omitempty"`
    30  	//
    31  	BindingType *string `json:"BindingType,omitempty"`
    32  	// The channel-specific address. For APNS, the device token. For FCM and GCM, the registration token. For SMS, a phone number in E.164 format. For Facebook Messenger, the Messenger ID of the user or a phone number in E.164 format.
    33  	Address *string `json:"Address,omitempty"`
    34  	// A tag that can be used to select the Bindings to notify. Repeat this parameter to specify more than one tag, up to a total of 20 tags.
    35  	Tag *[]string `json:"Tag,omitempty"`
    36  	// The protocol version to use to send the notification. This defaults to the value of `default_xxxx_notification_protocol_version` for the protocol in the [Service](https://www.twilio.com/docs/notify/api/service-resource). The current version is `\\\"3\\\"` for `apn`, `fcm`, and `gcm` type Bindings. The parameter is not applicable to `sms` and `facebook-messenger` type Bindings as the data format is fixed.
    37  	NotificationProtocolVersion *string `json:"NotificationProtocolVersion,omitempty"`
    38  	// The SID of the [Credential](https://www.twilio.com/docs/notify/api/credential-resource) resource to be used to send notifications to this Binding. If present, this overrides the Credential specified in the Service resource. Applies to only `apn`, `fcm`, and `gcm` type Bindings.
    39  	CredentialSid *string `json:"CredentialSid,omitempty"`
    40  	// Deprecated.
    41  	Endpoint *string `json:"Endpoint,omitempty"`
    42  }
    43  
    44  func (params *CreateBindingParams) SetIdentity(Identity string) *CreateBindingParams {
    45  	params.Identity = &Identity
    46  	return params
    47  }
    48  func (params *CreateBindingParams) SetBindingType(BindingType string) *CreateBindingParams {
    49  	params.BindingType = &BindingType
    50  	return params
    51  }
    52  func (params *CreateBindingParams) SetAddress(Address string) *CreateBindingParams {
    53  	params.Address = &Address
    54  	return params
    55  }
    56  func (params *CreateBindingParams) SetTag(Tag []string) *CreateBindingParams {
    57  	params.Tag = &Tag
    58  	return params
    59  }
    60  func (params *CreateBindingParams) SetNotificationProtocolVersion(NotificationProtocolVersion string) *CreateBindingParams {
    61  	params.NotificationProtocolVersion = &NotificationProtocolVersion
    62  	return params
    63  }
    64  func (params *CreateBindingParams) SetCredentialSid(CredentialSid string) *CreateBindingParams {
    65  	params.CredentialSid = &CredentialSid
    66  	return params
    67  }
    68  func (params *CreateBindingParams) SetEndpoint(Endpoint string) *CreateBindingParams {
    69  	params.Endpoint = &Endpoint
    70  	return params
    71  }
    72  
    73  //
    74  func (c *ApiService) CreateBinding(ServiceSid string, params *CreateBindingParams) (*NotifyV1Binding, error) {
    75  	path := "/v1/Services/{ServiceSid}/Bindings"
    76  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    77  
    78  	data := url.Values{}
    79  	headers := make(map[string]interface{})
    80  
    81  	if params != nil && params.Identity != nil {
    82  		data.Set("Identity", *params.Identity)
    83  	}
    84  	if params != nil && params.BindingType != nil {
    85  		data.Set("BindingType", *params.BindingType)
    86  	}
    87  	if params != nil && params.Address != nil {
    88  		data.Set("Address", *params.Address)
    89  	}
    90  	if params != nil && params.Tag != nil {
    91  		for _, item := range *params.Tag {
    92  			data.Add("Tag", item)
    93  		}
    94  	}
    95  	if params != nil && params.NotificationProtocolVersion != nil {
    96  		data.Set("NotificationProtocolVersion", *params.NotificationProtocolVersion)
    97  	}
    98  	if params != nil && params.CredentialSid != nil {
    99  		data.Set("CredentialSid", *params.CredentialSid)
   100  	}
   101  	if params != nil && params.Endpoint != nil {
   102  		data.Set("Endpoint", *params.Endpoint)
   103  	}
   104  
   105  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	defer resp.Body.Close()
   111  
   112  	ps := &NotifyV1Binding{}
   113  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   114  		return nil, err
   115  	}
   116  
   117  	return ps, err
   118  }
   119  
   120  //
   121  func (c *ApiService) DeleteBinding(ServiceSid string, Sid string) error {
   122  	path := "/v1/Services/{ServiceSid}/Bindings/{Sid}"
   123  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   124  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   125  
   126  	data := url.Values{}
   127  	headers := make(map[string]interface{})
   128  
   129  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	defer resp.Body.Close()
   135  
   136  	return nil
   137  }
   138  
   139  //
   140  func (c *ApiService) FetchBinding(ServiceSid string, Sid string) (*NotifyV1Binding, error) {
   141  	path := "/v1/Services/{ServiceSid}/Bindings/{Sid}"
   142  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   143  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   144  
   145  	data := url.Values{}
   146  	headers := make(map[string]interface{})
   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 := &NotifyV1Binding{}
   156  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	return ps, err
   161  }
   162  
   163  // Optional parameters for the method 'ListBinding'
   164  type ListBindingParams struct {
   165  	// Only include usage that has occurred on or after this date. Specify the date in GMT and format as `YYYY-MM-DD`.
   166  	StartDate *string `json:"StartDate,omitempty"`
   167  	// Only include usage that occurred on or before this date. Specify the date in GMT and format as `YYYY-MM-DD`.
   168  	EndDate *string `json:"EndDate,omitempty"`
   169  	// The [User](https://www.twilio.com/docs/chat/rest/user-resource)'s `identity` value of the resources to read.
   170  	Identity *[]string `json:"Identity,omitempty"`
   171  	// Only list Bindings that have all of the specified Tags. The following implicit tags are available: `all`, `apn`, `fcm`, `gcm`, `sms`, `facebook-messenger`. Up to 5 tags are allowed.
   172  	Tag *[]string `json:"Tag,omitempty"`
   173  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   174  	PageSize *int `json:"PageSize,omitempty"`
   175  	// Max number of records to return.
   176  	Limit *int `json:"limit,omitempty"`
   177  }
   178  
   179  func (params *ListBindingParams) SetStartDate(StartDate string) *ListBindingParams {
   180  	params.StartDate = &StartDate
   181  	return params
   182  }
   183  func (params *ListBindingParams) SetEndDate(EndDate string) *ListBindingParams {
   184  	params.EndDate = &EndDate
   185  	return params
   186  }
   187  func (params *ListBindingParams) SetIdentity(Identity []string) *ListBindingParams {
   188  	params.Identity = &Identity
   189  	return params
   190  }
   191  func (params *ListBindingParams) SetTag(Tag []string) *ListBindingParams {
   192  	params.Tag = &Tag
   193  	return params
   194  }
   195  func (params *ListBindingParams) SetPageSize(PageSize int) *ListBindingParams {
   196  	params.PageSize = &PageSize
   197  	return params
   198  }
   199  func (params *ListBindingParams) SetLimit(Limit int) *ListBindingParams {
   200  	params.Limit = &Limit
   201  	return params
   202  }
   203  
   204  // Retrieve a single page of Binding records from the API. Request is executed immediately.
   205  func (c *ApiService) PageBinding(ServiceSid string, params *ListBindingParams, pageToken, pageNumber string) (*ListBindingResponse, error) {
   206  	path := "/v1/Services/{ServiceSid}/Bindings"
   207  
   208  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   209  
   210  	data := url.Values{}
   211  	headers := make(map[string]interface{})
   212  
   213  	if params != nil && params.StartDate != nil {
   214  		data.Set("StartDate", fmt.Sprint(*params.StartDate))
   215  	}
   216  	if params != nil && params.EndDate != nil {
   217  		data.Set("EndDate", fmt.Sprint(*params.EndDate))
   218  	}
   219  	if params != nil && params.Identity != nil {
   220  		for _, item := range *params.Identity {
   221  			data.Add("Identity", item)
   222  		}
   223  	}
   224  	if params != nil && params.Tag != nil {
   225  		for _, item := range *params.Tag {
   226  			data.Add("Tag", item)
   227  		}
   228  	}
   229  	if params != nil && params.PageSize != nil {
   230  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   231  	}
   232  
   233  	if pageToken != "" {
   234  		data.Set("PageToken", pageToken)
   235  	}
   236  	if pageNumber != "" {
   237  		data.Set("Page", pageNumber)
   238  	}
   239  
   240  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   241  	if err != nil {
   242  		return nil, err
   243  	}
   244  
   245  	defer resp.Body.Close()
   246  
   247  	ps := &ListBindingResponse{}
   248  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   249  		return nil, err
   250  	}
   251  
   252  	return ps, err
   253  }
   254  
   255  // Lists Binding records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   256  func (c *ApiService) ListBinding(ServiceSid string, params *ListBindingParams) ([]NotifyV1Binding, error) {
   257  	response, errors := c.StreamBinding(ServiceSid, params)
   258  
   259  	records := make([]NotifyV1Binding, 0)
   260  	for record := range response {
   261  		records = append(records, record)
   262  	}
   263  
   264  	if err := <-errors; err != nil {
   265  		return nil, err
   266  	}
   267  
   268  	return records, nil
   269  }
   270  
   271  // Streams Binding records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   272  func (c *ApiService) StreamBinding(ServiceSid string, params *ListBindingParams) (chan NotifyV1Binding, chan error) {
   273  	if params == nil {
   274  		params = &ListBindingParams{}
   275  	}
   276  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   277  
   278  	recordChannel := make(chan NotifyV1Binding, 1)
   279  	errorChannel := make(chan error, 1)
   280  
   281  	response, err := c.PageBinding(ServiceSid, params, "", "")
   282  	if err != nil {
   283  		errorChannel <- err
   284  		close(recordChannel)
   285  		close(errorChannel)
   286  	} else {
   287  		go c.streamBinding(response, params, recordChannel, errorChannel)
   288  	}
   289  
   290  	return recordChannel, errorChannel
   291  }
   292  
   293  func (c *ApiService) streamBinding(response *ListBindingResponse, params *ListBindingParams, recordChannel chan NotifyV1Binding, errorChannel chan error) {
   294  	curRecord := 1
   295  
   296  	for response != nil {
   297  		responseRecords := response.Bindings
   298  		for item := range responseRecords {
   299  			recordChannel <- responseRecords[item]
   300  			curRecord += 1
   301  			if params.Limit != nil && *params.Limit < curRecord {
   302  				close(recordChannel)
   303  				close(errorChannel)
   304  				return
   305  			}
   306  		}
   307  
   308  		record, err := client.GetNext(c.baseURL, response, c.getNextListBindingResponse)
   309  		if err != nil {
   310  			errorChannel <- err
   311  			break
   312  		} else if record == nil {
   313  			break
   314  		}
   315  
   316  		response = record.(*ListBindingResponse)
   317  	}
   318  
   319  	close(recordChannel)
   320  	close(errorChannel)
   321  }
   322  
   323  func (c *ApiService) getNextListBindingResponse(nextPageUrl string) (interface{}, error) {
   324  	if nextPageUrl == "" {
   325  		return nil, nil
   326  	}
   327  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   328  	if err != nil {
   329  		return nil, err
   330  	}
   331  
   332  	defer resp.Body.Close()
   333  
   334  	ps := &ListBindingResponse{}
   335  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   336  		return nil, err
   337  	}
   338  	return ps, nil
   339  }