github.com/twilio/twilio-go@v1.20.1/rest/taskrouter/v1/workspaces_activities.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Taskrouter
     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 'CreateActivity'
    27  type CreateActivityParams struct {
    28  	// A descriptive string that you create to describe the Activity resource. It can be up to 64 characters long. These names are used to calculate and expose statistics about Workers, and provide visibility into the state of each Worker. Examples of friendly names include: `on-call`, `break`, and `email`.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	// Whether the Worker should be eligible to receive a Task when it occupies the Activity. A value of `true`, `1`, or `yes` specifies the Activity is available. All other values specify that it is not. The value cannot be changed after the Activity is created.
    31  	Available *bool `json:"Available,omitempty"`
    32  }
    33  
    34  func (params *CreateActivityParams) SetFriendlyName(FriendlyName string) *CreateActivityParams {
    35  	params.FriendlyName = &FriendlyName
    36  	return params
    37  }
    38  func (params *CreateActivityParams) SetAvailable(Available bool) *CreateActivityParams {
    39  	params.Available = &Available
    40  	return params
    41  }
    42  
    43  //
    44  func (c *ApiService) CreateActivity(WorkspaceSid string, params *CreateActivityParams) (*TaskrouterV1Activity, error) {
    45  	path := "/v1/Workspaces/{WorkspaceSid}/Activities"
    46  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    47  
    48  	data := url.Values{}
    49  	headers := make(map[string]interface{})
    50  
    51  	if params != nil && params.FriendlyName != nil {
    52  		data.Set("FriendlyName", *params.FriendlyName)
    53  	}
    54  	if params != nil && params.Available != nil {
    55  		data.Set("Available", fmt.Sprint(*params.Available))
    56  	}
    57  
    58  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	defer resp.Body.Close()
    64  
    65  	ps := &TaskrouterV1Activity{}
    66  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return ps, err
    71  }
    72  
    73  //
    74  func (c *ApiService) DeleteActivity(WorkspaceSid string, Sid string) error {
    75  	path := "/v1/Workspaces/{WorkspaceSid}/Activities/{Sid}"
    76  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    77  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    78  
    79  	data := url.Values{}
    80  	headers := make(map[string]interface{})
    81  
    82  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	defer resp.Body.Close()
    88  
    89  	return nil
    90  }
    91  
    92  //
    93  func (c *ApiService) FetchActivity(WorkspaceSid string, Sid string) (*TaskrouterV1Activity, error) {
    94  	path := "/v1/Workspaces/{WorkspaceSid}/Activities/{Sid}"
    95  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
    96  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    97  
    98  	data := url.Values{}
    99  	headers := make(map[string]interface{})
   100  
   101  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  
   106  	defer resp.Body.Close()
   107  
   108  	ps := &TaskrouterV1Activity{}
   109  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	return ps, err
   114  }
   115  
   116  // Optional parameters for the method 'ListActivity'
   117  type ListActivityParams struct {
   118  	// The `friendly_name` of the Activity resources to read.
   119  	FriendlyName *string `json:"FriendlyName,omitempty"`
   120  	// Whether return only Activity resources that are available or unavailable. A value of `true` returns only available activities. Values of '1' or `yes` also indicate `true`. All other values represent `false` and return activities that are unavailable.
   121  	Available *string `json:"Available,omitempty"`
   122  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   123  	PageSize *int `json:"PageSize,omitempty"`
   124  	// Max number of records to return.
   125  	Limit *int `json:"limit,omitempty"`
   126  }
   127  
   128  func (params *ListActivityParams) SetFriendlyName(FriendlyName string) *ListActivityParams {
   129  	params.FriendlyName = &FriendlyName
   130  	return params
   131  }
   132  func (params *ListActivityParams) SetAvailable(Available string) *ListActivityParams {
   133  	params.Available = &Available
   134  	return params
   135  }
   136  func (params *ListActivityParams) SetPageSize(PageSize int) *ListActivityParams {
   137  	params.PageSize = &PageSize
   138  	return params
   139  }
   140  func (params *ListActivityParams) SetLimit(Limit int) *ListActivityParams {
   141  	params.Limit = &Limit
   142  	return params
   143  }
   144  
   145  // Retrieve a single page of Activity records from the API. Request is executed immediately.
   146  func (c *ApiService) PageActivity(WorkspaceSid string, params *ListActivityParams, pageToken, pageNumber string) (*ListActivityResponse, error) {
   147  	path := "/v1/Workspaces/{WorkspaceSid}/Activities"
   148  
   149  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   150  
   151  	data := url.Values{}
   152  	headers := make(map[string]interface{})
   153  
   154  	if params != nil && params.FriendlyName != nil {
   155  		data.Set("FriendlyName", *params.FriendlyName)
   156  	}
   157  	if params != nil && params.Available != nil {
   158  		data.Set("Available", *params.Available)
   159  	}
   160  	if params != nil && params.PageSize != nil {
   161  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   162  	}
   163  
   164  	if pageToken != "" {
   165  		data.Set("PageToken", pageToken)
   166  	}
   167  	if pageNumber != "" {
   168  		data.Set("Page", pageNumber)
   169  	}
   170  
   171  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	defer resp.Body.Close()
   177  
   178  	ps := &ListActivityResponse{}
   179  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   180  		return nil, err
   181  	}
   182  
   183  	return ps, err
   184  }
   185  
   186  // Lists Activity records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   187  func (c *ApiService) ListActivity(WorkspaceSid string, params *ListActivityParams) ([]TaskrouterV1Activity, error) {
   188  	response, errors := c.StreamActivity(WorkspaceSid, params)
   189  
   190  	records := make([]TaskrouterV1Activity, 0)
   191  	for record := range response {
   192  		records = append(records, record)
   193  	}
   194  
   195  	if err := <-errors; err != nil {
   196  		return nil, err
   197  	}
   198  
   199  	return records, nil
   200  }
   201  
   202  // Streams Activity records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   203  func (c *ApiService) StreamActivity(WorkspaceSid string, params *ListActivityParams) (chan TaskrouterV1Activity, chan error) {
   204  	if params == nil {
   205  		params = &ListActivityParams{}
   206  	}
   207  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   208  
   209  	recordChannel := make(chan TaskrouterV1Activity, 1)
   210  	errorChannel := make(chan error, 1)
   211  
   212  	response, err := c.PageActivity(WorkspaceSid, params, "", "")
   213  	if err != nil {
   214  		errorChannel <- err
   215  		close(recordChannel)
   216  		close(errorChannel)
   217  	} else {
   218  		go c.streamActivity(response, params, recordChannel, errorChannel)
   219  	}
   220  
   221  	return recordChannel, errorChannel
   222  }
   223  
   224  func (c *ApiService) streamActivity(response *ListActivityResponse, params *ListActivityParams, recordChannel chan TaskrouterV1Activity, errorChannel chan error) {
   225  	curRecord := 1
   226  
   227  	for response != nil {
   228  		responseRecords := response.Activities
   229  		for item := range responseRecords {
   230  			recordChannel <- responseRecords[item]
   231  			curRecord += 1
   232  			if params.Limit != nil && *params.Limit < curRecord {
   233  				close(recordChannel)
   234  				close(errorChannel)
   235  				return
   236  			}
   237  		}
   238  
   239  		record, err := client.GetNext(c.baseURL, response, c.getNextListActivityResponse)
   240  		if err != nil {
   241  			errorChannel <- err
   242  			break
   243  		} else if record == nil {
   244  			break
   245  		}
   246  
   247  		response = record.(*ListActivityResponse)
   248  	}
   249  
   250  	close(recordChannel)
   251  	close(errorChannel)
   252  }
   253  
   254  func (c *ApiService) getNextListActivityResponse(nextPageUrl string) (interface{}, error) {
   255  	if nextPageUrl == "" {
   256  		return nil, nil
   257  	}
   258  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  
   263  	defer resp.Body.Close()
   264  
   265  	ps := &ListActivityResponse{}
   266  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   267  		return nil, err
   268  	}
   269  	return ps, nil
   270  }
   271  
   272  // Optional parameters for the method 'UpdateActivity'
   273  type UpdateActivityParams struct {
   274  	// A descriptive string that you create to describe the Activity resource. It can be up to 64 characters long. These names are used to calculate and expose statistics about Workers, and provide visibility into the state of each Worker. Examples of friendly names include: `on-call`, `break`, and `email`.
   275  	FriendlyName *string `json:"FriendlyName,omitempty"`
   276  }
   277  
   278  func (params *UpdateActivityParams) SetFriendlyName(FriendlyName string) *UpdateActivityParams {
   279  	params.FriendlyName = &FriendlyName
   280  	return params
   281  }
   282  
   283  //
   284  func (c *ApiService) UpdateActivity(WorkspaceSid string, Sid string, params *UpdateActivityParams) (*TaskrouterV1Activity, error) {
   285  	path := "/v1/Workspaces/{WorkspaceSid}/Activities/{Sid}"
   286  	path = strings.Replace(path, "{"+"WorkspaceSid"+"}", WorkspaceSid, -1)
   287  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   288  
   289  	data := url.Values{}
   290  	headers := make(map[string]interface{})
   291  
   292  	if params != nil && params.FriendlyName != nil {
   293  		data.Set("FriendlyName", *params.FriendlyName)
   294  	}
   295  
   296  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   297  	if err != nil {
   298  		return nil, err
   299  	}
   300  
   301  	defer resp.Body.Close()
   302  
   303  	ps := &TaskrouterV1Activity{}
   304  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   305  		return nil, err
   306  	}
   307  
   308  	return ps, err
   309  }