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

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Conversations
     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  // Remove a push notification binding from the conversation service
    27  func (c *ApiService) DeleteServiceBinding(ChatServiceSid string, Sid string) error {
    28  	path := "/v1/Services/{ChatServiceSid}/Bindings/{Sid}"
    29  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
    30  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    31  
    32  	data := url.Values{}
    33  	headers := make(map[string]interface{})
    34  
    35  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	defer resp.Body.Close()
    41  
    42  	return nil
    43  }
    44  
    45  // Fetch a push notification binding from the conversation service
    46  func (c *ApiService) FetchServiceBinding(ChatServiceSid string, Sid string) (*ConversationsV1ServiceBinding, error) {
    47  	path := "/v1/Services/{ChatServiceSid}/Bindings/{Sid}"
    48  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
    49  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    50  
    51  	data := url.Values{}
    52  	headers := make(map[string]interface{})
    53  
    54  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	defer resp.Body.Close()
    60  
    61  	ps := &ConversationsV1ServiceBinding{}
    62  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	return ps, err
    67  }
    68  
    69  // Optional parameters for the method 'ListServiceBinding'
    70  type ListServiceBindingParams struct {
    71  	// The push technology used by the Binding resources to read.  Can be: `apn`, `gcm`, or `fcm`.  See [push notification configuration](https://www.twilio.com/docs/chat/push-notification-configuration) for more info.
    72  	BindingType *[]string `json:"BindingType,omitempty"`
    73  	// The identity of a [Conversation User](https://www.twilio.com/docs/conversations/api/user-resource) this binding belongs to. See [access tokens](https://www.twilio.com/docs/conversations/create-tokens) for more details.
    74  	Identity *[]string `json:"Identity,omitempty"`
    75  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    76  	PageSize *int `json:"PageSize,omitempty"`
    77  	// Max number of records to return.
    78  	Limit *int `json:"limit,omitempty"`
    79  }
    80  
    81  func (params *ListServiceBindingParams) SetBindingType(BindingType []string) *ListServiceBindingParams {
    82  	params.BindingType = &BindingType
    83  	return params
    84  }
    85  func (params *ListServiceBindingParams) SetIdentity(Identity []string) *ListServiceBindingParams {
    86  	params.Identity = &Identity
    87  	return params
    88  }
    89  func (params *ListServiceBindingParams) SetPageSize(PageSize int) *ListServiceBindingParams {
    90  	params.PageSize = &PageSize
    91  	return params
    92  }
    93  func (params *ListServiceBindingParams) SetLimit(Limit int) *ListServiceBindingParams {
    94  	params.Limit = &Limit
    95  	return params
    96  }
    97  
    98  // Retrieve a single page of ServiceBinding records from the API. Request is executed immediately.
    99  func (c *ApiService) PageServiceBinding(ChatServiceSid string, params *ListServiceBindingParams, pageToken, pageNumber string) (*ListServiceBindingResponse, error) {
   100  	path := "/v1/Services/{ChatServiceSid}/Bindings"
   101  
   102  	path = strings.Replace(path, "{"+"ChatServiceSid"+"}", ChatServiceSid, -1)
   103  
   104  	data := url.Values{}
   105  	headers := make(map[string]interface{})
   106  
   107  	if params != nil && params.BindingType != nil {
   108  		for _, item := range *params.BindingType {
   109  			data.Add("BindingType", item)
   110  		}
   111  	}
   112  	if params != nil && params.Identity != nil {
   113  		for _, item := range *params.Identity {
   114  			data.Add("Identity", item)
   115  		}
   116  	}
   117  	if params != nil && params.PageSize != nil {
   118  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   119  	}
   120  
   121  	if pageToken != "" {
   122  		data.Set("PageToken", pageToken)
   123  	}
   124  	if pageNumber != "" {
   125  		data.Set("Page", pageNumber)
   126  	}
   127  
   128  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	defer resp.Body.Close()
   134  
   135  	ps := &ListServiceBindingResponse{}
   136  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	return ps, err
   141  }
   142  
   143  // Lists ServiceBinding records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   144  func (c *ApiService) ListServiceBinding(ChatServiceSid string, params *ListServiceBindingParams) ([]ConversationsV1ServiceBinding, error) {
   145  	response, errors := c.StreamServiceBinding(ChatServiceSid, params)
   146  
   147  	records := make([]ConversationsV1ServiceBinding, 0)
   148  	for record := range response {
   149  		records = append(records, record)
   150  	}
   151  
   152  	if err := <-errors; err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	return records, nil
   157  }
   158  
   159  // Streams ServiceBinding records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   160  func (c *ApiService) StreamServiceBinding(ChatServiceSid string, params *ListServiceBindingParams) (chan ConversationsV1ServiceBinding, chan error) {
   161  	if params == nil {
   162  		params = &ListServiceBindingParams{}
   163  	}
   164  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   165  
   166  	recordChannel := make(chan ConversationsV1ServiceBinding, 1)
   167  	errorChannel := make(chan error, 1)
   168  
   169  	response, err := c.PageServiceBinding(ChatServiceSid, params, "", "")
   170  	if err != nil {
   171  		errorChannel <- err
   172  		close(recordChannel)
   173  		close(errorChannel)
   174  	} else {
   175  		go c.streamServiceBinding(response, params, recordChannel, errorChannel)
   176  	}
   177  
   178  	return recordChannel, errorChannel
   179  }
   180  
   181  func (c *ApiService) streamServiceBinding(response *ListServiceBindingResponse, params *ListServiceBindingParams, recordChannel chan ConversationsV1ServiceBinding, errorChannel chan error) {
   182  	curRecord := 1
   183  
   184  	for response != nil {
   185  		responseRecords := response.Bindings
   186  		for item := range responseRecords {
   187  			recordChannel <- responseRecords[item]
   188  			curRecord += 1
   189  			if params.Limit != nil && *params.Limit < curRecord {
   190  				close(recordChannel)
   191  				close(errorChannel)
   192  				return
   193  			}
   194  		}
   195  
   196  		record, err := client.GetNext(c.baseURL, response, c.getNextListServiceBindingResponse)
   197  		if err != nil {
   198  			errorChannel <- err
   199  			break
   200  		} else if record == nil {
   201  			break
   202  		}
   203  
   204  		response = record.(*ListServiceBindingResponse)
   205  	}
   206  
   207  	close(recordChannel)
   208  	close(errorChannel)
   209  }
   210  
   211  func (c *ApiService) getNextListServiceBindingResponse(nextPageUrl string) (interface{}, error) {
   212  	if nextPageUrl == "" {
   213  		return nil, nil
   214  	}
   215  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  
   220  	defer resp.Body.Close()
   221  
   222  	ps := &ListServiceBindingResponse{}
   223  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   224  		return nil, err
   225  	}
   226  	return ps, nil
   227  }