github.com/twilio/twilio-go@v1.20.1/rest/chat/v1/services_channels.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Chat
     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 'CreateChannel'
    27  type CreateChannelParams struct {
    28  	// A descriptive string that you create to describe the new resource. It can be up to 64 characters long.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the resource's `sid` in the URL. This value must be 64 characters or less in length and be unique within the Service.
    31  	UniqueName *string `json:"UniqueName,omitempty"`
    32  	// A valid JSON string that contains application-specific data.
    33  	Attributes *string `json:"Attributes,omitempty"`
    34  	//
    35  	Type *string `json:"Type,omitempty"`
    36  }
    37  
    38  func (params *CreateChannelParams) SetFriendlyName(FriendlyName string) *CreateChannelParams {
    39  	params.FriendlyName = &FriendlyName
    40  	return params
    41  }
    42  func (params *CreateChannelParams) SetUniqueName(UniqueName string) *CreateChannelParams {
    43  	params.UniqueName = &UniqueName
    44  	return params
    45  }
    46  func (params *CreateChannelParams) SetAttributes(Attributes string) *CreateChannelParams {
    47  	params.Attributes = &Attributes
    48  	return params
    49  }
    50  func (params *CreateChannelParams) SetType(Type string) *CreateChannelParams {
    51  	params.Type = &Type
    52  	return params
    53  }
    54  
    55  //
    56  func (c *ApiService) CreateChannel(ServiceSid string, params *CreateChannelParams) (*ChatV1Channel, error) {
    57  	path := "/v1/Services/{ServiceSid}/Channels"
    58  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    59  
    60  	data := url.Values{}
    61  	headers := make(map[string]interface{})
    62  
    63  	if params != nil && params.FriendlyName != nil {
    64  		data.Set("FriendlyName", *params.FriendlyName)
    65  	}
    66  	if params != nil && params.UniqueName != nil {
    67  		data.Set("UniqueName", *params.UniqueName)
    68  	}
    69  	if params != nil && params.Attributes != nil {
    70  		data.Set("Attributes", *params.Attributes)
    71  	}
    72  	if params != nil && params.Type != nil {
    73  		data.Set("Type", *params.Type)
    74  	}
    75  
    76  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	defer resp.Body.Close()
    82  
    83  	ps := &ChatV1Channel{}
    84  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    85  		return nil, err
    86  	}
    87  
    88  	return ps, err
    89  }
    90  
    91  //
    92  func (c *ApiService) DeleteChannel(ServiceSid string, Sid string) error {
    93  	path := "/v1/Services/{ServiceSid}/Channels/{Sid}"
    94  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    95  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    96  
    97  	data := url.Values{}
    98  	headers := make(map[string]interface{})
    99  
   100  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	defer resp.Body.Close()
   106  
   107  	return nil
   108  }
   109  
   110  //
   111  func (c *ApiService) FetchChannel(ServiceSid string, Sid string) (*ChatV1Channel, error) {
   112  	path := "/v1/Services/{ServiceSid}/Channels/{Sid}"
   113  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   114  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   115  
   116  	data := url.Values{}
   117  	headers := make(map[string]interface{})
   118  
   119  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	defer resp.Body.Close()
   125  
   126  	ps := &ChatV1Channel{}
   127  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   128  		return nil, err
   129  	}
   130  
   131  	return ps, err
   132  }
   133  
   134  // Optional parameters for the method 'ListChannel'
   135  type ListChannelParams struct {
   136  	// The visibility of the Channels to read. Can be: `public` or `private` and defaults to `public`.
   137  	Type *[]string `json:"Type,omitempty"`
   138  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   139  	PageSize *int `json:"PageSize,omitempty"`
   140  	// Max number of records to return.
   141  	Limit *int `json:"limit,omitempty"`
   142  }
   143  
   144  func (params *ListChannelParams) SetType(Type []string) *ListChannelParams {
   145  	params.Type = &Type
   146  	return params
   147  }
   148  func (params *ListChannelParams) SetPageSize(PageSize int) *ListChannelParams {
   149  	params.PageSize = &PageSize
   150  	return params
   151  }
   152  func (params *ListChannelParams) SetLimit(Limit int) *ListChannelParams {
   153  	params.Limit = &Limit
   154  	return params
   155  }
   156  
   157  // Retrieve a single page of Channel records from the API. Request is executed immediately.
   158  func (c *ApiService) PageChannel(ServiceSid string, params *ListChannelParams, pageToken, pageNumber string) (*ListChannelResponse, error) {
   159  	path := "/v1/Services/{ServiceSid}/Channels"
   160  
   161  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   162  
   163  	data := url.Values{}
   164  	headers := make(map[string]interface{})
   165  
   166  	if params != nil && params.Type != nil {
   167  		for _, item := range *params.Type {
   168  			data.Add("Type", item)
   169  		}
   170  	}
   171  	if params != nil && params.PageSize != nil {
   172  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   173  	}
   174  
   175  	if pageToken != "" {
   176  		data.Set("PageToken", pageToken)
   177  	}
   178  	if pageNumber != "" {
   179  		data.Set("Page", pageNumber)
   180  	}
   181  
   182  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	defer resp.Body.Close()
   188  
   189  	ps := &ListChannelResponse{}
   190  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   191  		return nil, err
   192  	}
   193  
   194  	return ps, err
   195  }
   196  
   197  // Lists Channel records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   198  func (c *ApiService) ListChannel(ServiceSid string, params *ListChannelParams) ([]ChatV1Channel, error) {
   199  	response, errors := c.StreamChannel(ServiceSid, params)
   200  
   201  	records := make([]ChatV1Channel, 0)
   202  	for record := range response {
   203  		records = append(records, record)
   204  	}
   205  
   206  	if err := <-errors; err != nil {
   207  		return nil, err
   208  	}
   209  
   210  	return records, nil
   211  }
   212  
   213  // Streams Channel records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   214  func (c *ApiService) StreamChannel(ServiceSid string, params *ListChannelParams) (chan ChatV1Channel, chan error) {
   215  	if params == nil {
   216  		params = &ListChannelParams{}
   217  	}
   218  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   219  
   220  	recordChannel := make(chan ChatV1Channel, 1)
   221  	errorChannel := make(chan error, 1)
   222  
   223  	response, err := c.PageChannel(ServiceSid, params, "", "")
   224  	if err != nil {
   225  		errorChannel <- err
   226  		close(recordChannel)
   227  		close(errorChannel)
   228  	} else {
   229  		go c.streamChannel(response, params, recordChannel, errorChannel)
   230  	}
   231  
   232  	return recordChannel, errorChannel
   233  }
   234  
   235  func (c *ApiService) streamChannel(response *ListChannelResponse, params *ListChannelParams, recordChannel chan ChatV1Channel, errorChannel chan error) {
   236  	curRecord := 1
   237  
   238  	for response != nil {
   239  		responseRecords := response.Channels
   240  		for item := range responseRecords {
   241  			recordChannel <- responseRecords[item]
   242  			curRecord += 1
   243  			if params.Limit != nil && *params.Limit < curRecord {
   244  				close(recordChannel)
   245  				close(errorChannel)
   246  				return
   247  			}
   248  		}
   249  
   250  		record, err := client.GetNext(c.baseURL, response, c.getNextListChannelResponse)
   251  		if err != nil {
   252  			errorChannel <- err
   253  			break
   254  		} else if record == nil {
   255  			break
   256  		}
   257  
   258  		response = record.(*ListChannelResponse)
   259  	}
   260  
   261  	close(recordChannel)
   262  	close(errorChannel)
   263  }
   264  
   265  func (c *ApiService) getNextListChannelResponse(nextPageUrl string) (interface{}, error) {
   266  	if nextPageUrl == "" {
   267  		return nil, nil
   268  	}
   269  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   270  	if err != nil {
   271  		return nil, err
   272  	}
   273  
   274  	defer resp.Body.Close()
   275  
   276  	ps := &ListChannelResponse{}
   277  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   278  		return nil, err
   279  	}
   280  	return ps, nil
   281  }
   282  
   283  // Optional parameters for the method 'UpdateChannel'
   284  type UpdateChannelParams struct {
   285  	// A descriptive string that you create to describe the resource. It can be up to 64 characters long.
   286  	FriendlyName *string `json:"FriendlyName,omitempty"`
   287  	// An application-defined string that uniquely identifies the resource. It can be used to address the resource in place of the resource's `sid` in the URL. This value must be 64 characters or less in length and be unique within the Service.
   288  	UniqueName *string `json:"UniqueName,omitempty"`
   289  	// A valid JSON string that contains application-specific data.
   290  	Attributes *string `json:"Attributes,omitempty"`
   291  }
   292  
   293  func (params *UpdateChannelParams) SetFriendlyName(FriendlyName string) *UpdateChannelParams {
   294  	params.FriendlyName = &FriendlyName
   295  	return params
   296  }
   297  func (params *UpdateChannelParams) SetUniqueName(UniqueName string) *UpdateChannelParams {
   298  	params.UniqueName = &UniqueName
   299  	return params
   300  }
   301  func (params *UpdateChannelParams) SetAttributes(Attributes string) *UpdateChannelParams {
   302  	params.Attributes = &Attributes
   303  	return params
   304  }
   305  
   306  //
   307  func (c *ApiService) UpdateChannel(ServiceSid string, Sid string, params *UpdateChannelParams) (*ChatV1Channel, error) {
   308  	path := "/v1/Services/{ServiceSid}/Channels/{Sid}"
   309  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   310  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   311  
   312  	data := url.Values{}
   313  	headers := make(map[string]interface{})
   314  
   315  	if params != nil && params.FriendlyName != nil {
   316  		data.Set("FriendlyName", *params.FriendlyName)
   317  	}
   318  	if params != nil && params.UniqueName != nil {
   319  		data.Set("UniqueName", *params.UniqueName)
   320  	}
   321  	if params != nil && params.Attributes != nil {
   322  		data.Set("Attributes", *params.Attributes)
   323  	}
   324  
   325  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   326  	if err != nil {
   327  		return nil, err
   328  	}
   329  
   330  	defer resp.Body.Close()
   331  
   332  	ps := &ChatV1Channel{}
   333  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   334  		return nil, err
   335  	}
   336  
   337  	return ps, err
   338  }