github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_queues_members.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Api
     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 'FetchMember'
    27  type FetchMemberParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Member resource(s) to fetch.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  }
    31  
    32  func (params *FetchMemberParams) SetPathAccountSid(PathAccountSid string) *FetchMemberParams {
    33  	params.PathAccountSid = &PathAccountSid
    34  	return params
    35  }
    36  
    37  // Fetch a specific member from the queue
    38  func (c *ApiService) FetchMember(QueueSid string, CallSid string, params *FetchMemberParams) (*ApiV2010Member, error) {
    39  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{QueueSid}/Members/{CallSid}.json"
    40  	if params != nil && params.PathAccountSid != nil {
    41  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    42  	} else {
    43  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    44  	}
    45  	path = strings.Replace(path, "{"+"QueueSid"+"}", QueueSid, -1)
    46  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
    47  
    48  	data := url.Values{}
    49  	headers := make(map[string]interface{})
    50  
    51  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	defer resp.Body.Close()
    57  
    58  	ps := &ApiV2010Member{}
    59  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return ps, err
    64  }
    65  
    66  // Optional parameters for the method 'ListMember'
    67  type ListMemberParams struct {
    68  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Member resource(s) to read.
    69  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    70  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    71  	PageSize *int `json:"PageSize,omitempty"`
    72  	// Max number of records to return.
    73  	Limit *int `json:"limit,omitempty"`
    74  }
    75  
    76  func (params *ListMemberParams) SetPathAccountSid(PathAccountSid string) *ListMemberParams {
    77  	params.PathAccountSid = &PathAccountSid
    78  	return params
    79  }
    80  func (params *ListMemberParams) SetPageSize(PageSize int) *ListMemberParams {
    81  	params.PageSize = &PageSize
    82  	return params
    83  }
    84  func (params *ListMemberParams) SetLimit(Limit int) *ListMemberParams {
    85  	params.Limit = &Limit
    86  	return params
    87  }
    88  
    89  // Retrieve a single page of Member records from the API. Request is executed immediately.
    90  func (c *ApiService) PageMember(QueueSid string, params *ListMemberParams, pageToken, pageNumber string) (*ListMemberResponse, error) {
    91  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{QueueSid}/Members.json"
    92  
    93  	if params != nil && params.PathAccountSid != nil {
    94  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    95  	} else {
    96  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    97  	}
    98  	path = strings.Replace(path, "{"+"QueueSid"+"}", QueueSid, -1)
    99  
   100  	data := url.Values{}
   101  	headers := make(map[string]interface{})
   102  
   103  	if params != nil && params.PageSize != nil {
   104  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   105  	}
   106  
   107  	if pageToken != "" {
   108  		data.Set("PageToken", pageToken)
   109  	}
   110  	if pageNumber != "" {
   111  		data.Set("Page", pageNumber)
   112  	}
   113  
   114  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	defer resp.Body.Close()
   120  
   121  	ps := &ListMemberResponse{}
   122  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	return ps, err
   127  }
   128  
   129  // Lists Member records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   130  func (c *ApiService) ListMember(QueueSid string, params *ListMemberParams) ([]ApiV2010Member, error) {
   131  	response, errors := c.StreamMember(QueueSid, params)
   132  
   133  	records := make([]ApiV2010Member, 0)
   134  	for record := range response {
   135  		records = append(records, record)
   136  	}
   137  
   138  	if err := <-errors; err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	return records, nil
   143  }
   144  
   145  // Streams Member records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   146  func (c *ApiService) StreamMember(QueueSid string, params *ListMemberParams) (chan ApiV2010Member, chan error) {
   147  	if params == nil {
   148  		params = &ListMemberParams{}
   149  	}
   150  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   151  
   152  	recordChannel := make(chan ApiV2010Member, 1)
   153  	errorChannel := make(chan error, 1)
   154  
   155  	response, err := c.PageMember(QueueSid, params, "", "")
   156  	if err != nil {
   157  		errorChannel <- err
   158  		close(recordChannel)
   159  		close(errorChannel)
   160  	} else {
   161  		go c.streamMember(response, params, recordChannel, errorChannel)
   162  	}
   163  
   164  	return recordChannel, errorChannel
   165  }
   166  
   167  func (c *ApiService) streamMember(response *ListMemberResponse, params *ListMemberParams, recordChannel chan ApiV2010Member, errorChannel chan error) {
   168  	curRecord := 1
   169  
   170  	for response != nil {
   171  		responseRecords := response.QueueMembers
   172  		for item := range responseRecords {
   173  			recordChannel <- responseRecords[item]
   174  			curRecord += 1
   175  			if params.Limit != nil && *params.Limit < curRecord {
   176  				close(recordChannel)
   177  				close(errorChannel)
   178  				return
   179  			}
   180  		}
   181  
   182  		record, err := client.GetNext(c.baseURL, response, c.getNextListMemberResponse)
   183  		if err != nil {
   184  			errorChannel <- err
   185  			break
   186  		} else if record == nil {
   187  			break
   188  		}
   189  
   190  		response = record.(*ListMemberResponse)
   191  	}
   192  
   193  	close(recordChannel)
   194  	close(errorChannel)
   195  }
   196  
   197  func (c *ApiService) getNextListMemberResponse(nextPageUrl string) (interface{}, error) {
   198  	if nextPageUrl == "" {
   199  		return nil, nil
   200  	}
   201  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  
   206  	defer resp.Body.Close()
   207  
   208  	ps := &ListMemberResponse{}
   209  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   210  		return nil, err
   211  	}
   212  	return ps, nil
   213  }
   214  
   215  // Optional parameters for the method 'UpdateMember'
   216  type UpdateMemberParams struct {
   217  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Member resource(s) to update.
   218  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   219  	// The absolute URL of the Queue resource.
   220  	Url *string `json:"Url,omitempty"`
   221  	// How to pass the update request data. Can be `GET` or `POST` and the default is `POST`. `POST` sends the data as encoded form data and `GET` sends the data as query parameters.
   222  	Method *string `json:"Method,omitempty"`
   223  }
   224  
   225  func (params *UpdateMemberParams) SetPathAccountSid(PathAccountSid string) *UpdateMemberParams {
   226  	params.PathAccountSid = &PathAccountSid
   227  	return params
   228  }
   229  func (params *UpdateMemberParams) SetUrl(Url string) *UpdateMemberParams {
   230  	params.Url = &Url
   231  	return params
   232  }
   233  func (params *UpdateMemberParams) SetMethod(Method string) *UpdateMemberParams {
   234  	params.Method = &Method
   235  	return params
   236  }
   237  
   238  // Dequeue a member from a queue and have the member's call begin executing the TwiML document at that URL
   239  func (c *ApiService) UpdateMember(QueueSid string, CallSid string, params *UpdateMemberParams) (*ApiV2010Member, error) {
   240  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{QueueSid}/Members/{CallSid}.json"
   241  	if params != nil && params.PathAccountSid != nil {
   242  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   243  	} else {
   244  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   245  	}
   246  	path = strings.Replace(path, "{"+"QueueSid"+"}", QueueSid, -1)
   247  	path = strings.Replace(path, "{"+"CallSid"+"}", CallSid, -1)
   248  
   249  	data := url.Values{}
   250  	headers := make(map[string]interface{})
   251  
   252  	if params != nil && params.Url != nil {
   253  		data.Set("Url", *params.Url)
   254  	}
   255  	if params != nil && params.Method != nil {
   256  		data.Set("Method", *params.Method)
   257  	}
   258  
   259  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   260  	if err != nil {
   261  		return nil, err
   262  	}
   263  
   264  	defer resp.Body.Close()
   265  
   266  	ps := &ApiV2010Member{}
   267  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   268  		return nil, err
   269  	}
   270  
   271  	return ps, err
   272  }