github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_queues.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 'CreateQueue'
    27  type CreateQueueParams struct {
    28  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that will create the resource.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  	// A descriptive string that you created to describe this resource. It can be up to 64 characters long.
    31  	FriendlyName *string `json:"FriendlyName,omitempty"`
    32  	// The maximum number of calls allowed to be in the queue. The default is 1000. The maximum is 5000.
    33  	MaxSize *int `json:"MaxSize,omitempty"`
    34  }
    35  
    36  func (params *CreateQueueParams) SetPathAccountSid(PathAccountSid string) *CreateQueueParams {
    37  	params.PathAccountSid = &PathAccountSid
    38  	return params
    39  }
    40  func (params *CreateQueueParams) SetFriendlyName(FriendlyName string) *CreateQueueParams {
    41  	params.FriendlyName = &FriendlyName
    42  	return params
    43  }
    44  func (params *CreateQueueParams) SetMaxSize(MaxSize int) *CreateQueueParams {
    45  	params.MaxSize = &MaxSize
    46  	return params
    47  }
    48  
    49  // Create a queue
    50  func (c *ApiService) CreateQueue(params *CreateQueueParams) (*ApiV2010Queue, error) {
    51  	path := "/2010-04-01/Accounts/{AccountSid}/Queues.json"
    52  	if params != nil && params.PathAccountSid != nil {
    53  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    54  	} else {
    55  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    56  	}
    57  
    58  	data := url.Values{}
    59  	headers := make(map[string]interface{})
    60  
    61  	if params != nil && params.FriendlyName != nil {
    62  		data.Set("FriendlyName", *params.FriendlyName)
    63  	}
    64  	if params != nil && params.MaxSize != nil {
    65  		data.Set("MaxSize", fmt.Sprint(*params.MaxSize))
    66  	}
    67  
    68  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	defer resp.Body.Close()
    74  
    75  	ps := &ApiV2010Queue{}
    76  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	return ps, err
    81  }
    82  
    83  // Optional parameters for the method 'DeleteQueue'
    84  type DeleteQueueParams struct {
    85  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Queue resource to delete.
    86  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    87  }
    88  
    89  func (params *DeleteQueueParams) SetPathAccountSid(PathAccountSid string) *DeleteQueueParams {
    90  	params.PathAccountSid = &PathAccountSid
    91  	return params
    92  }
    93  
    94  // Remove an empty queue
    95  func (c *ApiService) DeleteQueue(Sid string, params *DeleteQueueParams) error {
    96  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{Sid}.json"
    97  	if params != nil && params.PathAccountSid != nil {
    98  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    99  	} else {
   100  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   101  	}
   102  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   103  
   104  	data := url.Values{}
   105  	headers := make(map[string]interface{})
   106  
   107  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	defer resp.Body.Close()
   113  
   114  	return nil
   115  }
   116  
   117  // Optional parameters for the method 'FetchQueue'
   118  type FetchQueueParams struct {
   119  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Queue resource to fetch.
   120  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   121  }
   122  
   123  func (params *FetchQueueParams) SetPathAccountSid(PathAccountSid string) *FetchQueueParams {
   124  	params.PathAccountSid = &PathAccountSid
   125  	return params
   126  }
   127  
   128  // Fetch an instance of a queue identified by the QueueSid
   129  func (c *ApiService) FetchQueue(Sid string, params *FetchQueueParams) (*ApiV2010Queue, error) {
   130  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{Sid}.json"
   131  	if params != nil && params.PathAccountSid != nil {
   132  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   133  	} else {
   134  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   135  	}
   136  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   137  
   138  	data := url.Values{}
   139  	headers := make(map[string]interface{})
   140  
   141  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   142  	if err != nil {
   143  		return nil, err
   144  	}
   145  
   146  	defer resp.Body.Close()
   147  
   148  	ps := &ApiV2010Queue{}
   149  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   150  		return nil, err
   151  	}
   152  
   153  	return ps, err
   154  }
   155  
   156  // Optional parameters for the method 'ListQueue'
   157  type ListQueueParams struct {
   158  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Queue resources to read.
   159  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   160  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   161  	PageSize *int `json:"PageSize,omitempty"`
   162  	// Max number of records to return.
   163  	Limit *int `json:"limit,omitempty"`
   164  }
   165  
   166  func (params *ListQueueParams) SetPathAccountSid(PathAccountSid string) *ListQueueParams {
   167  	params.PathAccountSid = &PathAccountSid
   168  	return params
   169  }
   170  func (params *ListQueueParams) SetPageSize(PageSize int) *ListQueueParams {
   171  	params.PageSize = &PageSize
   172  	return params
   173  }
   174  func (params *ListQueueParams) SetLimit(Limit int) *ListQueueParams {
   175  	params.Limit = &Limit
   176  	return params
   177  }
   178  
   179  // Retrieve a single page of Queue records from the API. Request is executed immediately.
   180  func (c *ApiService) PageQueue(params *ListQueueParams, pageToken, pageNumber string) (*ListQueueResponse, error) {
   181  	path := "/2010-04-01/Accounts/{AccountSid}/Queues.json"
   182  
   183  	if params != nil && params.PathAccountSid != nil {
   184  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   185  	} else {
   186  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   187  	}
   188  
   189  	data := url.Values{}
   190  	headers := make(map[string]interface{})
   191  
   192  	if params != nil && params.PageSize != nil {
   193  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   194  	}
   195  
   196  	if pageToken != "" {
   197  		data.Set("PageToken", pageToken)
   198  	}
   199  	if pageNumber != "" {
   200  		data.Set("Page", pageNumber)
   201  	}
   202  
   203  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   204  	if err != nil {
   205  		return nil, err
   206  	}
   207  
   208  	defer resp.Body.Close()
   209  
   210  	ps := &ListQueueResponse{}
   211  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   212  		return nil, err
   213  	}
   214  
   215  	return ps, err
   216  }
   217  
   218  // Lists Queue records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   219  func (c *ApiService) ListQueue(params *ListQueueParams) ([]ApiV2010Queue, error) {
   220  	response, errors := c.StreamQueue(params)
   221  
   222  	records := make([]ApiV2010Queue, 0)
   223  	for record := range response {
   224  		records = append(records, record)
   225  	}
   226  
   227  	if err := <-errors; err != nil {
   228  		return nil, err
   229  	}
   230  
   231  	return records, nil
   232  }
   233  
   234  // Streams Queue records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   235  func (c *ApiService) StreamQueue(params *ListQueueParams) (chan ApiV2010Queue, chan error) {
   236  	if params == nil {
   237  		params = &ListQueueParams{}
   238  	}
   239  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   240  
   241  	recordChannel := make(chan ApiV2010Queue, 1)
   242  	errorChannel := make(chan error, 1)
   243  
   244  	response, err := c.PageQueue(params, "", "")
   245  	if err != nil {
   246  		errorChannel <- err
   247  		close(recordChannel)
   248  		close(errorChannel)
   249  	} else {
   250  		go c.streamQueue(response, params, recordChannel, errorChannel)
   251  	}
   252  
   253  	return recordChannel, errorChannel
   254  }
   255  
   256  func (c *ApiService) streamQueue(response *ListQueueResponse, params *ListQueueParams, recordChannel chan ApiV2010Queue, errorChannel chan error) {
   257  	curRecord := 1
   258  
   259  	for response != nil {
   260  		responseRecords := response.Queues
   261  		for item := range responseRecords {
   262  			recordChannel <- responseRecords[item]
   263  			curRecord += 1
   264  			if params.Limit != nil && *params.Limit < curRecord {
   265  				close(recordChannel)
   266  				close(errorChannel)
   267  				return
   268  			}
   269  		}
   270  
   271  		record, err := client.GetNext(c.baseURL, response, c.getNextListQueueResponse)
   272  		if err != nil {
   273  			errorChannel <- err
   274  			break
   275  		} else if record == nil {
   276  			break
   277  		}
   278  
   279  		response = record.(*ListQueueResponse)
   280  	}
   281  
   282  	close(recordChannel)
   283  	close(errorChannel)
   284  }
   285  
   286  func (c *ApiService) getNextListQueueResponse(nextPageUrl string) (interface{}, error) {
   287  	if nextPageUrl == "" {
   288  		return nil, nil
   289  	}
   290  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   291  	if err != nil {
   292  		return nil, err
   293  	}
   294  
   295  	defer resp.Body.Close()
   296  
   297  	ps := &ListQueueResponse{}
   298  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   299  		return nil, err
   300  	}
   301  	return ps, nil
   302  }
   303  
   304  // Optional parameters for the method 'UpdateQueue'
   305  type UpdateQueueParams struct {
   306  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) that created the Queue resource to update.
   307  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   308  	// A descriptive string that you created to describe this resource. It can be up to 64 characters long.
   309  	FriendlyName *string `json:"FriendlyName,omitempty"`
   310  	// The maximum number of calls allowed to be in the queue. The default is 1000. The maximum is 5000.
   311  	MaxSize *int `json:"MaxSize,omitempty"`
   312  }
   313  
   314  func (params *UpdateQueueParams) SetPathAccountSid(PathAccountSid string) *UpdateQueueParams {
   315  	params.PathAccountSid = &PathAccountSid
   316  	return params
   317  }
   318  func (params *UpdateQueueParams) SetFriendlyName(FriendlyName string) *UpdateQueueParams {
   319  	params.FriendlyName = &FriendlyName
   320  	return params
   321  }
   322  func (params *UpdateQueueParams) SetMaxSize(MaxSize int) *UpdateQueueParams {
   323  	params.MaxSize = &MaxSize
   324  	return params
   325  }
   326  
   327  // Update the queue with the new parameters
   328  func (c *ApiService) UpdateQueue(Sid string, params *UpdateQueueParams) (*ApiV2010Queue, error) {
   329  	path := "/2010-04-01/Accounts/{AccountSid}/Queues/{Sid}.json"
   330  	if params != nil && params.PathAccountSid != nil {
   331  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   332  	} else {
   333  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   334  	}
   335  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   336  
   337  	data := url.Values{}
   338  	headers := make(map[string]interface{})
   339  
   340  	if params != nil && params.FriendlyName != nil {
   341  		data.Set("FriendlyName", *params.FriendlyName)
   342  	}
   343  	if params != nil && params.MaxSize != nil {
   344  		data.Set("MaxSize", fmt.Sprint(*params.MaxSize))
   345  	}
   346  
   347  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   348  	if err != nil {
   349  		return nil, err
   350  	}
   351  
   352  	defer resp.Body.Close()
   353  
   354  	ps := &ApiV2010Queue{}
   355  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   356  		return nil, err
   357  	}
   358  
   359  	return ps, err
   360  }