github.com/twilio/twilio-go@v1.20.1/rest/ip_messaging/v2/services_users_channels.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Ip_messaging
     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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  //
    28  func (c *ApiService) DeleteUserChannel(ServiceSid string, UserSid string, ChannelSid string) error {
    29  	path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}"
    30  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    31  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    32  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    33  
    34  	data := url.Values{}
    35  	headers := make(map[string]interface{})
    36  
    37  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    38  	if err != nil {
    39  		return err
    40  	}
    41  
    42  	defer resp.Body.Close()
    43  
    44  	return nil
    45  }
    46  
    47  //
    48  func (c *ApiService) FetchUserChannel(ServiceSid string, UserSid string, ChannelSid string) (*IpMessagingV2UserChannel, error) {
    49  	path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}"
    50  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    51  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    52  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    53  
    54  	data := url.Values{}
    55  	headers := make(map[string]interface{})
    56  
    57  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	defer resp.Body.Close()
    63  
    64  	ps := &IpMessagingV2UserChannel{}
    65  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return ps, err
    70  }
    71  
    72  // Optional parameters for the method 'ListUserChannel'
    73  type ListUserChannelParams struct {
    74  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    75  	PageSize *int `json:"PageSize,omitempty"`
    76  	// Max number of records to return.
    77  	Limit *int `json:"limit,omitempty"`
    78  }
    79  
    80  func (params *ListUserChannelParams) SetPageSize(PageSize int) *ListUserChannelParams {
    81  	params.PageSize = &PageSize
    82  	return params
    83  }
    84  func (params *ListUserChannelParams) SetLimit(Limit int) *ListUserChannelParams {
    85  	params.Limit = &Limit
    86  	return params
    87  }
    88  
    89  // Retrieve a single page of UserChannel records from the API. Request is executed immediately.
    90  func (c *ApiService) PageUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams, pageToken, pageNumber string) (*ListUserChannelResponse, error) {
    91  	path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels"
    92  
    93  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    94  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
    95  
    96  	data := url.Values{}
    97  	headers := make(map[string]interface{})
    98  
    99  	if params != nil && params.PageSize != nil {
   100  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   101  	}
   102  
   103  	if pageToken != "" {
   104  		data.Set("PageToken", pageToken)
   105  	}
   106  	if pageNumber != "" {
   107  		data.Set("Page", pageNumber)
   108  	}
   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 := &ListUserChannelResponse{}
   118  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return ps, err
   123  }
   124  
   125  // Lists UserChannel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   126  func (c *ApiService) ListUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) ([]IpMessagingV2UserChannel, error) {
   127  	response, errors := c.StreamUserChannel(ServiceSid, UserSid, params)
   128  
   129  	records := make([]IpMessagingV2UserChannel, 0)
   130  	for record := range response {
   131  		records = append(records, record)
   132  	}
   133  
   134  	if err := <-errors; err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	return records, nil
   139  }
   140  
   141  // Streams UserChannel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   142  func (c *ApiService) StreamUserChannel(ServiceSid string, UserSid string, params *ListUserChannelParams) (chan IpMessagingV2UserChannel, chan error) {
   143  	if params == nil {
   144  		params = &ListUserChannelParams{}
   145  	}
   146  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   147  
   148  	recordChannel := make(chan IpMessagingV2UserChannel, 1)
   149  	errorChannel := make(chan error, 1)
   150  
   151  	response, err := c.PageUserChannel(ServiceSid, UserSid, params, "", "")
   152  	if err != nil {
   153  		errorChannel <- err
   154  		close(recordChannel)
   155  		close(errorChannel)
   156  	} else {
   157  		go c.streamUserChannel(response, params, recordChannel, errorChannel)
   158  	}
   159  
   160  	return recordChannel, errorChannel
   161  }
   162  
   163  func (c *ApiService) streamUserChannel(response *ListUserChannelResponse, params *ListUserChannelParams, recordChannel chan IpMessagingV2UserChannel, errorChannel chan error) {
   164  	curRecord := 1
   165  
   166  	for response != nil {
   167  		responseRecords := response.Channels
   168  		for item := range responseRecords {
   169  			recordChannel <- responseRecords[item]
   170  			curRecord += 1
   171  			if params.Limit != nil && *params.Limit < curRecord {
   172  				close(recordChannel)
   173  				close(errorChannel)
   174  				return
   175  			}
   176  		}
   177  
   178  		record, err := client.GetNext(c.baseURL, response, c.getNextListUserChannelResponse)
   179  		if err != nil {
   180  			errorChannel <- err
   181  			break
   182  		} else if record == nil {
   183  			break
   184  		}
   185  
   186  		response = record.(*ListUserChannelResponse)
   187  	}
   188  
   189  	close(recordChannel)
   190  	close(errorChannel)
   191  }
   192  
   193  func (c *ApiService) getNextListUserChannelResponse(nextPageUrl string) (interface{}, error) {
   194  	if nextPageUrl == "" {
   195  		return nil, nil
   196  	}
   197  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   198  	if err != nil {
   199  		return nil, err
   200  	}
   201  
   202  	defer resp.Body.Close()
   203  
   204  	ps := &ListUserChannelResponse{}
   205  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   206  		return nil, err
   207  	}
   208  	return ps, nil
   209  }
   210  
   211  // Optional parameters for the method 'UpdateUserChannel'
   212  type UpdateUserChannelParams struct {
   213  	//
   214  	NotificationLevel *string `json:"NotificationLevel,omitempty"`
   215  	//
   216  	LastConsumedMessageIndex *int `json:"LastConsumedMessageIndex,omitempty"`
   217  	//
   218  	LastConsumptionTimestamp *time.Time `json:"LastConsumptionTimestamp,omitempty"`
   219  }
   220  
   221  func (params *UpdateUserChannelParams) SetNotificationLevel(NotificationLevel string) *UpdateUserChannelParams {
   222  	params.NotificationLevel = &NotificationLevel
   223  	return params
   224  }
   225  func (params *UpdateUserChannelParams) SetLastConsumedMessageIndex(LastConsumedMessageIndex int) *UpdateUserChannelParams {
   226  	params.LastConsumedMessageIndex = &LastConsumedMessageIndex
   227  	return params
   228  }
   229  func (params *UpdateUserChannelParams) SetLastConsumptionTimestamp(LastConsumptionTimestamp time.Time) *UpdateUserChannelParams {
   230  	params.LastConsumptionTimestamp = &LastConsumptionTimestamp
   231  	return params
   232  }
   233  
   234  //
   235  func (c *ApiService) UpdateUserChannel(ServiceSid string, UserSid string, ChannelSid string, params *UpdateUserChannelParams) (*IpMessagingV2UserChannel, error) {
   236  	path := "/v2/Services/{ServiceSid}/Users/{UserSid}/Channels/{ChannelSid}"
   237  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   238  	path = strings.Replace(path, "{"+"UserSid"+"}", UserSid, -1)
   239  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   240  
   241  	data := url.Values{}
   242  	headers := make(map[string]interface{})
   243  
   244  	if params != nil && params.NotificationLevel != nil {
   245  		data.Set("NotificationLevel", *params.NotificationLevel)
   246  	}
   247  	if params != nil && params.LastConsumedMessageIndex != nil {
   248  		data.Set("LastConsumedMessageIndex", fmt.Sprint(*params.LastConsumedMessageIndex))
   249  	}
   250  	if params != nil && params.LastConsumptionTimestamp != nil {
   251  		data.Set("LastConsumptionTimestamp", fmt.Sprint((*params.LastConsumptionTimestamp).Format(time.RFC3339)))
   252  	}
   253  
   254  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   255  	if err != nil {
   256  		return nil, err
   257  	}
   258  
   259  	defer resp.Body.Close()
   260  
   261  	ps := &IpMessagingV2UserChannel{}
   262  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   263  		return nil, err
   264  	}
   265  
   266  	return ps, err
   267  }