github.com/twilio/twilio-go@v1.20.1/rest/chat/v1/services_channels_invites.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 'CreateInvite'
    27  type CreateInviteParams struct {
    28  	// The `identity` value that uniquely identifies the new resource's [User](https://www.twilio.com/docs/api/chat/rest/v1/user) within the [Service](https://www.twilio.com/docs/api/chat/rest/v1/service). See [access tokens](https://www.twilio.com/docs/api/chat/guides/create-tokens) for more info.
    29  	Identity *string `json:"Identity,omitempty"`
    30  	// The SID of the [Role](https://www.twilio.com/docs/api/chat/rest/roles) assigned to the new member.
    31  	RoleSid *string `json:"RoleSid,omitempty"`
    32  }
    33  
    34  func (params *CreateInviteParams) SetIdentity(Identity string) *CreateInviteParams {
    35  	params.Identity = &Identity
    36  	return params
    37  }
    38  func (params *CreateInviteParams) SetRoleSid(RoleSid string) *CreateInviteParams {
    39  	params.RoleSid = &RoleSid
    40  	return params
    41  }
    42  
    43  //
    44  func (c *ApiService) CreateInvite(ServiceSid string, ChannelSid string, params *CreateInviteParams) (*ChatV1Invite, error) {
    45  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Invites"
    46  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    47  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    48  
    49  	data := url.Values{}
    50  	headers := make(map[string]interface{})
    51  
    52  	if params != nil && params.Identity != nil {
    53  		data.Set("Identity", *params.Identity)
    54  	}
    55  	if params != nil && params.RoleSid != nil {
    56  		data.Set("RoleSid", *params.RoleSid)
    57  	}
    58  
    59  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	defer resp.Body.Close()
    65  
    66  	ps := &ChatV1Invite{}
    67  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	return ps, err
    72  }
    73  
    74  //
    75  func (c *ApiService) DeleteInvite(ServiceSid string, ChannelSid string, Sid string) error {
    76  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Invites/{Sid}"
    77  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    78  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    79  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    80  
    81  	data := url.Values{}
    82  	headers := make(map[string]interface{})
    83  
    84  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	defer resp.Body.Close()
    90  
    91  	return nil
    92  }
    93  
    94  //
    95  func (c *ApiService) FetchInvite(ServiceSid string, ChannelSid string, Sid string) (*ChatV1Invite, error) {
    96  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Invites/{Sid}"
    97  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    98  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
    99  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   100  
   101  	data := url.Values{}
   102  	headers := make(map[string]interface{})
   103  
   104  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	defer resp.Body.Close()
   110  
   111  	ps := &ChatV1Invite{}
   112  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return ps, err
   117  }
   118  
   119  // Optional parameters for the method 'ListInvite'
   120  type ListInviteParams struct {
   121  	// The [User](https://www.twilio.com/docs/api/chat/rest/v1/user)'s `identity` value of the resources to read. See [access tokens](https://www.twilio.com/docs/api/chat/guides/create-tokens) for more details.
   122  	Identity *[]string `json:"Identity,omitempty"`
   123  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   124  	PageSize *int `json:"PageSize,omitempty"`
   125  	// Max number of records to return.
   126  	Limit *int `json:"limit,omitempty"`
   127  }
   128  
   129  func (params *ListInviteParams) SetIdentity(Identity []string) *ListInviteParams {
   130  	params.Identity = &Identity
   131  	return params
   132  }
   133  func (params *ListInviteParams) SetPageSize(PageSize int) *ListInviteParams {
   134  	params.PageSize = &PageSize
   135  	return params
   136  }
   137  func (params *ListInviteParams) SetLimit(Limit int) *ListInviteParams {
   138  	params.Limit = &Limit
   139  	return params
   140  }
   141  
   142  // Retrieve a single page of Invite records from the API. Request is executed immediately.
   143  func (c *ApiService) PageInvite(ServiceSid string, ChannelSid string, params *ListInviteParams, pageToken, pageNumber string) (*ListInviteResponse, error) {
   144  	path := "/v1/Services/{ServiceSid}/Channels/{ChannelSid}/Invites"
   145  
   146  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   147  	path = strings.Replace(path, "{"+"ChannelSid"+"}", ChannelSid, -1)
   148  
   149  	data := url.Values{}
   150  	headers := make(map[string]interface{})
   151  
   152  	if params != nil && params.Identity != nil {
   153  		for _, item := range *params.Identity {
   154  			data.Add("Identity", item)
   155  		}
   156  	}
   157  	if params != nil && params.PageSize != nil {
   158  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   159  	}
   160  
   161  	if pageToken != "" {
   162  		data.Set("PageToken", pageToken)
   163  	}
   164  	if pageNumber != "" {
   165  		data.Set("Page", pageNumber)
   166  	}
   167  
   168  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  
   173  	defer resp.Body.Close()
   174  
   175  	ps := &ListInviteResponse{}
   176  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   177  		return nil, err
   178  	}
   179  
   180  	return ps, err
   181  }
   182  
   183  // Lists Invite records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   184  func (c *ApiService) ListInvite(ServiceSid string, ChannelSid string, params *ListInviteParams) ([]ChatV1Invite, error) {
   185  	response, errors := c.StreamInvite(ServiceSid, ChannelSid, params)
   186  
   187  	records := make([]ChatV1Invite, 0)
   188  	for record := range response {
   189  		records = append(records, record)
   190  	}
   191  
   192  	if err := <-errors; err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return records, nil
   197  }
   198  
   199  // Streams Invite records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   200  func (c *ApiService) StreamInvite(ServiceSid string, ChannelSid string, params *ListInviteParams) (chan ChatV1Invite, chan error) {
   201  	if params == nil {
   202  		params = &ListInviteParams{}
   203  	}
   204  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   205  
   206  	recordChannel := make(chan ChatV1Invite, 1)
   207  	errorChannel := make(chan error, 1)
   208  
   209  	response, err := c.PageInvite(ServiceSid, ChannelSid, params, "", "")
   210  	if err != nil {
   211  		errorChannel <- err
   212  		close(recordChannel)
   213  		close(errorChannel)
   214  	} else {
   215  		go c.streamInvite(response, params, recordChannel, errorChannel)
   216  	}
   217  
   218  	return recordChannel, errorChannel
   219  }
   220  
   221  func (c *ApiService) streamInvite(response *ListInviteResponse, params *ListInviteParams, recordChannel chan ChatV1Invite, errorChannel chan error) {
   222  	curRecord := 1
   223  
   224  	for response != nil {
   225  		responseRecords := response.Invites
   226  		for item := range responseRecords {
   227  			recordChannel <- responseRecords[item]
   228  			curRecord += 1
   229  			if params.Limit != nil && *params.Limit < curRecord {
   230  				close(recordChannel)
   231  				close(errorChannel)
   232  				return
   233  			}
   234  		}
   235  
   236  		record, err := client.GetNext(c.baseURL, response, c.getNextListInviteResponse)
   237  		if err != nil {
   238  			errorChannel <- err
   239  			break
   240  		} else if record == nil {
   241  			break
   242  		}
   243  
   244  		response = record.(*ListInviteResponse)
   245  	}
   246  
   247  	close(recordChannel)
   248  	close(errorChannel)
   249  }
   250  
   251  func (c *ApiService) getNextListInviteResponse(nextPageUrl string) (interface{}, error) {
   252  	if nextPageUrl == "" {
   253  		return nil, nil
   254  	}
   255  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   256  	if err != nil {
   257  		return nil, err
   258  	}
   259  
   260  	defer resp.Body.Close()
   261  
   262  	ps := &ListInviteResponse{}
   263  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   264  		return nil, err
   265  	}
   266  	return ps, nil
   267  }