github.com/twilio/twilio-go@v1.20.1/rest/flex/v1/insights_quality_management_questionnaires.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Flex
     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 'CreateInsightsQuestionnaires'
    27  type CreateInsightsQuestionnairesParams struct {
    28  	// The Authorization HTTP request header
    29  	Authorization *string `json:"Authorization,omitempty"`
    30  	// The name of this questionnaire
    31  	Name *string `json:"Name,omitempty"`
    32  	// The description of this questionnaire
    33  	Description *string `json:"Description,omitempty"`
    34  	// The flag to enable or disable questionnaire
    35  	Active *bool `json:"Active,omitempty"`
    36  	// The list of questions sids under a questionnaire
    37  	QuestionSids *[]string `json:"QuestionSids,omitempty"`
    38  }
    39  
    40  func (params *CreateInsightsQuestionnairesParams) SetAuthorization(Authorization string) *CreateInsightsQuestionnairesParams {
    41  	params.Authorization = &Authorization
    42  	return params
    43  }
    44  func (params *CreateInsightsQuestionnairesParams) SetName(Name string) *CreateInsightsQuestionnairesParams {
    45  	params.Name = &Name
    46  	return params
    47  }
    48  func (params *CreateInsightsQuestionnairesParams) SetDescription(Description string) *CreateInsightsQuestionnairesParams {
    49  	params.Description = &Description
    50  	return params
    51  }
    52  func (params *CreateInsightsQuestionnairesParams) SetActive(Active bool) *CreateInsightsQuestionnairesParams {
    53  	params.Active = &Active
    54  	return params
    55  }
    56  func (params *CreateInsightsQuestionnairesParams) SetQuestionSids(QuestionSids []string) *CreateInsightsQuestionnairesParams {
    57  	params.QuestionSids = &QuestionSids
    58  	return params
    59  }
    60  
    61  // To create a Questionnaire
    62  func (c *ApiService) CreateInsightsQuestionnaires(params *CreateInsightsQuestionnairesParams) (*FlexV1InsightsQuestionnaires, error) {
    63  	path := "/v1/Insights/QualityManagement/Questionnaires"
    64  
    65  	data := url.Values{}
    66  	headers := make(map[string]interface{})
    67  
    68  	if params != nil && params.Name != nil {
    69  		data.Set("Name", *params.Name)
    70  	}
    71  	if params != nil && params.Description != nil {
    72  		data.Set("Description", *params.Description)
    73  	}
    74  	if params != nil && params.Active != nil {
    75  		data.Set("Active", fmt.Sprint(*params.Active))
    76  	}
    77  	if params != nil && params.QuestionSids != nil {
    78  		for _, item := range *params.QuestionSids {
    79  			data.Add("QuestionSids", item)
    80  		}
    81  	}
    82  
    83  	if params != nil && params.Authorization != nil {
    84  		headers["Authorization"] = *params.Authorization
    85  	}
    86  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    87  	if err != nil {
    88  		return nil, err
    89  	}
    90  
    91  	defer resp.Body.Close()
    92  
    93  	ps := &FlexV1InsightsQuestionnaires{}
    94  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	return ps, err
    99  }
   100  
   101  // Optional parameters for the method 'DeleteInsightsQuestionnaires'
   102  type DeleteInsightsQuestionnairesParams struct {
   103  	// The Authorization HTTP request header
   104  	Authorization *string `json:"Authorization,omitempty"`
   105  }
   106  
   107  func (params *DeleteInsightsQuestionnairesParams) SetAuthorization(Authorization string) *DeleteInsightsQuestionnairesParams {
   108  	params.Authorization = &Authorization
   109  	return params
   110  }
   111  
   112  // To delete the questionnaire
   113  func (c *ApiService) DeleteInsightsQuestionnaires(QuestionnaireSid string, params *DeleteInsightsQuestionnairesParams) error {
   114  	path := "/v1/Insights/QualityManagement/Questionnaires/{QuestionnaireSid}"
   115  	path = strings.Replace(path, "{"+"QuestionnaireSid"+"}", QuestionnaireSid, -1)
   116  
   117  	data := url.Values{}
   118  	headers := make(map[string]interface{})
   119  
   120  	if params != nil && params.Authorization != nil {
   121  		headers["Authorization"] = *params.Authorization
   122  	}
   123  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   124  	if err != nil {
   125  		return err
   126  	}
   127  
   128  	defer resp.Body.Close()
   129  
   130  	return nil
   131  }
   132  
   133  // Optional parameters for the method 'FetchInsightsQuestionnaires'
   134  type FetchInsightsQuestionnairesParams struct {
   135  	// The Authorization HTTP request header
   136  	Authorization *string `json:"Authorization,omitempty"`
   137  }
   138  
   139  func (params *FetchInsightsQuestionnairesParams) SetAuthorization(Authorization string) *FetchInsightsQuestionnairesParams {
   140  	params.Authorization = &Authorization
   141  	return params
   142  }
   143  
   144  // To get the Questionnaire Detail
   145  func (c *ApiService) FetchInsightsQuestionnaires(QuestionnaireSid string, params *FetchInsightsQuestionnairesParams) (*FlexV1InsightsQuestionnaires, error) {
   146  	path := "/v1/Insights/QualityManagement/Questionnaires/{QuestionnaireSid}"
   147  	path = strings.Replace(path, "{"+"QuestionnaireSid"+"}", QuestionnaireSid, -1)
   148  
   149  	data := url.Values{}
   150  	headers := make(map[string]interface{})
   151  
   152  	if params != nil && params.Authorization != nil {
   153  		headers["Authorization"] = *params.Authorization
   154  	}
   155  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   156  	if err != nil {
   157  		return nil, err
   158  	}
   159  
   160  	defer resp.Body.Close()
   161  
   162  	ps := &FlexV1InsightsQuestionnaires{}
   163  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   164  		return nil, err
   165  	}
   166  
   167  	return ps, err
   168  }
   169  
   170  // Optional parameters for the method 'ListInsightsQuestionnaires'
   171  type ListInsightsQuestionnairesParams struct {
   172  	// The Authorization HTTP request header
   173  	Authorization *string `json:"Authorization,omitempty"`
   174  	// Flag indicating whether to include inactive questionnaires or not
   175  	IncludeInactive *bool `json:"IncludeInactive,omitempty"`
   176  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   177  	PageSize *int `json:"PageSize,omitempty"`
   178  	// Max number of records to return.
   179  	Limit *int `json:"limit,omitempty"`
   180  }
   181  
   182  func (params *ListInsightsQuestionnairesParams) SetAuthorization(Authorization string) *ListInsightsQuestionnairesParams {
   183  	params.Authorization = &Authorization
   184  	return params
   185  }
   186  func (params *ListInsightsQuestionnairesParams) SetIncludeInactive(IncludeInactive bool) *ListInsightsQuestionnairesParams {
   187  	params.IncludeInactive = &IncludeInactive
   188  	return params
   189  }
   190  func (params *ListInsightsQuestionnairesParams) SetPageSize(PageSize int) *ListInsightsQuestionnairesParams {
   191  	params.PageSize = &PageSize
   192  	return params
   193  }
   194  func (params *ListInsightsQuestionnairesParams) SetLimit(Limit int) *ListInsightsQuestionnairesParams {
   195  	params.Limit = &Limit
   196  	return params
   197  }
   198  
   199  // Retrieve a single page of InsightsQuestionnaires records from the API. Request is executed immediately.
   200  func (c *ApiService) PageInsightsQuestionnaires(params *ListInsightsQuestionnairesParams, pageToken, pageNumber string) (*ListInsightsQuestionnairesResponse, error) {
   201  	path := "/v1/Insights/QualityManagement/Questionnaires"
   202  
   203  	data := url.Values{}
   204  	headers := make(map[string]interface{})
   205  
   206  	if params != nil && params.IncludeInactive != nil {
   207  		data.Set("IncludeInactive", fmt.Sprint(*params.IncludeInactive))
   208  	}
   209  	if params != nil && params.PageSize != nil {
   210  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   211  	}
   212  
   213  	if pageToken != "" {
   214  		data.Set("PageToken", pageToken)
   215  	}
   216  	if pageNumber != "" {
   217  		data.Set("Page", pageNumber)
   218  	}
   219  
   220  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   221  	if err != nil {
   222  		return nil, err
   223  	}
   224  
   225  	defer resp.Body.Close()
   226  
   227  	ps := &ListInsightsQuestionnairesResponse{}
   228  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   229  		return nil, err
   230  	}
   231  
   232  	return ps, err
   233  }
   234  
   235  // Lists InsightsQuestionnaires records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   236  func (c *ApiService) ListInsightsQuestionnaires(params *ListInsightsQuestionnairesParams) ([]FlexV1InsightsQuestionnaires, error) {
   237  	response, errors := c.StreamInsightsQuestionnaires(params)
   238  
   239  	records := make([]FlexV1InsightsQuestionnaires, 0)
   240  	for record := range response {
   241  		records = append(records, record)
   242  	}
   243  
   244  	if err := <-errors; err != nil {
   245  		return nil, err
   246  	}
   247  
   248  	return records, nil
   249  }
   250  
   251  // Streams InsightsQuestionnaires records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   252  func (c *ApiService) StreamInsightsQuestionnaires(params *ListInsightsQuestionnairesParams) (chan FlexV1InsightsQuestionnaires, chan error) {
   253  	if params == nil {
   254  		params = &ListInsightsQuestionnairesParams{}
   255  	}
   256  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   257  
   258  	recordChannel := make(chan FlexV1InsightsQuestionnaires, 1)
   259  	errorChannel := make(chan error, 1)
   260  
   261  	response, err := c.PageInsightsQuestionnaires(params, "", "")
   262  	if err != nil {
   263  		errorChannel <- err
   264  		close(recordChannel)
   265  		close(errorChannel)
   266  	} else {
   267  		go c.streamInsightsQuestionnaires(response, params, recordChannel, errorChannel)
   268  	}
   269  
   270  	return recordChannel, errorChannel
   271  }
   272  
   273  func (c *ApiService) streamInsightsQuestionnaires(response *ListInsightsQuestionnairesResponse, params *ListInsightsQuestionnairesParams, recordChannel chan FlexV1InsightsQuestionnaires, errorChannel chan error) {
   274  	curRecord := 1
   275  
   276  	for response != nil {
   277  		responseRecords := response.Questionnaires
   278  		for item := range responseRecords {
   279  			recordChannel <- responseRecords[item]
   280  			curRecord += 1
   281  			if params.Limit != nil && *params.Limit < curRecord {
   282  				close(recordChannel)
   283  				close(errorChannel)
   284  				return
   285  			}
   286  		}
   287  
   288  		record, err := client.GetNext(c.baseURL, response, c.getNextListInsightsQuestionnairesResponse)
   289  		if err != nil {
   290  			errorChannel <- err
   291  			break
   292  		} else if record == nil {
   293  			break
   294  		}
   295  
   296  		response = record.(*ListInsightsQuestionnairesResponse)
   297  	}
   298  
   299  	close(recordChannel)
   300  	close(errorChannel)
   301  }
   302  
   303  func (c *ApiService) getNextListInsightsQuestionnairesResponse(nextPageUrl string) (interface{}, error) {
   304  	if nextPageUrl == "" {
   305  		return nil, nil
   306  	}
   307  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   308  	if err != nil {
   309  		return nil, err
   310  	}
   311  
   312  	defer resp.Body.Close()
   313  
   314  	ps := &ListInsightsQuestionnairesResponse{}
   315  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   316  		return nil, err
   317  	}
   318  	return ps, nil
   319  }
   320  
   321  // Optional parameters for the method 'UpdateInsightsQuestionnaires'
   322  type UpdateInsightsQuestionnairesParams struct {
   323  	// The Authorization HTTP request header
   324  	Authorization *string `json:"Authorization,omitempty"`
   325  	// The flag to enable or disable questionnaire
   326  	Active *bool `json:"Active,omitempty"`
   327  	// The name of this questionnaire
   328  	Name *string `json:"Name,omitempty"`
   329  	// The description of this questionnaire
   330  	Description *string `json:"Description,omitempty"`
   331  	// The list of questions sids under a questionnaire
   332  	QuestionSids *[]string `json:"QuestionSids,omitempty"`
   333  }
   334  
   335  func (params *UpdateInsightsQuestionnairesParams) SetAuthorization(Authorization string) *UpdateInsightsQuestionnairesParams {
   336  	params.Authorization = &Authorization
   337  	return params
   338  }
   339  func (params *UpdateInsightsQuestionnairesParams) SetActive(Active bool) *UpdateInsightsQuestionnairesParams {
   340  	params.Active = &Active
   341  	return params
   342  }
   343  func (params *UpdateInsightsQuestionnairesParams) SetName(Name string) *UpdateInsightsQuestionnairesParams {
   344  	params.Name = &Name
   345  	return params
   346  }
   347  func (params *UpdateInsightsQuestionnairesParams) SetDescription(Description string) *UpdateInsightsQuestionnairesParams {
   348  	params.Description = &Description
   349  	return params
   350  }
   351  func (params *UpdateInsightsQuestionnairesParams) SetQuestionSids(QuestionSids []string) *UpdateInsightsQuestionnairesParams {
   352  	params.QuestionSids = &QuestionSids
   353  	return params
   354  }
   355  
   356  // To update the questionnaire
   357  func (c *ApiService) UpdateInsightsQuestionnaires(QuestionnaireSid string, params *UpdateInsightsQuestionnairesParams) (*FlexV1InsightsQuestionnaires, error) {
   358  	path := "/v1/Insights/QualityManagement/Questionnaires/{QuestionnaireSid}"
   359  	path = strings.Replace(path, "{"+"QuestionnaireSid"+"}", QuestionnaireSid, -1)
   360  
   361  	data := url.Values{}
   362  	headers := make(map[string]interface{})
   363  
   364  	if params != nil && params.Active != nil {
   365  		data.Set("Active", fmt.Sprint(*params.Active))
   366  	}
   367  	if params != nil && params.Name != nil {
   368  		data.Set("Name", *params.Name)
   369  	}
   370  	if params != nil && params.Description != nil {
   371  		data.Set("Description", *params.Description)
   372  	}
   373  	if params != nil && params.QuestionSids != nil {
   374  		for _, item := range *params.QuestionSids {
   375  			data.Add("QuestionSids", item)
   376  		}
   377  	}
   378  
   379  	if params != nil && params.Authorization != nil {
   380  		headers["Authorization"] = *params.Authorization
   381  	}
   382  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   383  	if err != nil {
   384  		return nil, err
   385  	}
   386  
   387  	defer resp.Body.Close()
   388  
   389  	ps := &FlexV1InsightsQuestionnaires{}
   390  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   391  		return nil, err
   392  	}
   393  
   394  	return ps, err
   395  }