github.com/twilio/twilio-go@v1.20.1/rest/intelligence/v2/services.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Intelligence
     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 'CreateService'
    27  type CreateServiceParams struct {
    28  	// Provides a unique and addressable name to be assigned to this Service, assigned by the developer, to be optionally used in addition to SID.
    29  	UniqueName *string `json:"UniqueName,omitempty"`
    30  	// Instructs the Speech Recognition service to automatically transcribe all recordings made on the account.
    31  	AutoTranscribe *bool `json:"AutoTranscribe,omitempty"`
    32  	// Data logging allows Twilio to improve the quality of the speech recognition & language understanding services through using customer data to refine, fine tune and evaluate machine learning models. Note: Data logging cannot be activated via API, only via www.twilio.com, as it requires additional consent.
    33  	DataLogging *bool `json:"DataLogging,omitempty"`
    34  	// A human readable description of this resource, up to 64 characters.
    35  	FriendlyName *string `json:"FriendlyName,omitempty"`
    36  	// The default language code of the audio.
    37  	LanguageCode *string `json:"LanguageCode,omitempty"`
    38  	// Instructs the Speech Recognition service to automatically redact PII from all transcripts made on this service.
    39  	AutoRedaction *bool `json:"AutoRedaction,omitempty"`
    40  	// Instructs the Speech Recognition service to automatically redact PII from all transcripts media made on this service. The auto_redaction flag must be enabled, results in error otherwise.
    41  	MediaRedaction *bool `json:"MediaRedaction,omitempty"`
    42  	// The URL Twilio will request when executing the Webhook.
    43  	WebhookUrl *string `json:"WebhookUrl,omitempty"`
    44  	//
    45  	WebhookHttpMethod *string `json:"WebhookHttpMethod,omitempty"`
    46  }
    47  
    48  func (params *CreateServiceParams) SetUniqueName(UniqueName string) *CreateServiceParams {
    49  	params.UniqueName = &UniqueName
    50  	return params
    51  }
    52  func (params *CreateServiceParams) SetAutoTranscribe(AutoTranscribe bool) *CreateServiceParams {
    53  	params.AutoTranscribe = &AutoTranscribe
    54  	return params
    55  }
    56  func (params *CreateServiceParams) SetDataLogging(DataLogging bool) *CreateServiceParams {
    57  	params.DataLogging = &DataLogging
    58  	return params
    59  }
    60  func (params *CreateServiceParams) SetFriendlyName(FriendlyName string) *CreateServiceParams {
    61  	params.FriendlyName = &FriendlyName
    62  	return params
    63  }
    64  func (params *CreateServiceParams) SetLanguageCode(LanguageCode string) *CreateServiceParams {
    65  	params.LanguageCode = &LanguageCode
    66  	return params
    67  }
    68  func (params *CreateServiceParams) SetAutoRedaction(AutoRedaction bool) *CreateServiceParams {
    69  	params.AutoRedaction = &AutoRedaction
    70  	return params
    71  }
    72  func (params *CreateServiceParams) SetMediaRedaction(MediaRedaction bool) *CreateServiceParams {
    73  	params.MediaRedaction = &MediaRedaction
    74  	return params
    75  }
    76  func (params *CreateServiceParams) SetWebhookUrl(WebhookUrl string) *CreateServiceParams {
    77  	params.WebhookUrl = &WebhookUrl
    78  	return params
    79  }
    80  func (params *CreateServiceParams) SetWebhookHttpMethod(WebhookHttpMethod string) *CreateServiceParams {
    81  	params.WebhookHttpMethod = &WebhookHttpMethod
    82  	return params
    83  }
    84  
    85  // Create a new Service for the given Account
    86  func (c *ApiService) CreateService(params *CreateServiceParams) (*IntelligenceV2Service, error) {
    87  	path := "/v2/Services"
    88  
    89  	data := url.Values{}
    90  	headers := make(map[string]interface{})
    91  
    92  	if params != nil && params.UniqueName != nil {
    93  		data.Set("UniqueName", *params.UniqueName)
    94  	}
    95  	if params != nil && params.AutoTranscribe != nil {
    96  		data.Set("AutoTranscribe", fmt.Sprint(*params.AutoTranscribe))
    97  	}
    98  	if params != nil && params.DataLogging != nil {
    99  		data.Set("DataLogging", fmt.Sprint(*params.DataLogging))
   100  	}
   101  	if params != nil && params.FriendlyName != nil {
   102  		data.Set("FriendlyName", *params.FriendlyName)
   103  	}
   104  	if params != nil && params.LanguageCode != nil {
   105  		data.Set("LanguageCode", *params.LanguageCode)
   106  	}
   107  	if params != nil && params.AutoRedaction != nil {
   108  		data.Set("AutoRedaction", fmt.Sprint(*params.AutoRedaction))
   109  	}
   110  	if params != nil && params.MediaRedaction != nil {
   111  		data.Set("MediaRedaction", fmt.Sprint(*params.MediaRedaction))
   112  	}
   113  	if params != nil && params.WebhookUrl != nil {
   114  		data.Set("WebhookUrl", *params.WebhookUrl)
   115  	}
   116  	if params != nil && params.WebhookHttpMethod != nil {
   117  		data.Set("WebhookHttpMethod", *params.WebhookHttpMethod)
   118  	}
   119  
   120  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  
   125  	defer resp.Body.Close()
   126  
   127  	ps := &IntelligenceV2Service{}
   128  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	return ps, err
   133  }
   134  
   135  // Delete a specific Service.
   136  func (c *ApiService) DeleteService(Sid string) error {
   137  	path := "/v2/Services/{Sid}"
   138  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   139  
   140  	data := url.Values{}
   141  	headers := make(map[string]interface{})
   142  
   143  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   144  	if err != nil {
   145  		return err
   146  	}
   147  
   148  	defer resp.Body.Close()
   149  
   150  	return nil
   151  }
   152  
   153  // Fetch a specific Service.
   154  func (c *ApiService) FetchService(Sid string) (*IntelligenceV2Service, error) {
   155  	path := "/v2/Services/{Sid}"
   156  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   157  
   158  	data := url.Values{}
   159  	headers := make(map[string]interface{})
   160  
   161  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   162  	if err != nil {
   163  		return nil, err
   164  	}
   165  
   166  	defer resp.Body.Close()
   167  
   168  	ps := &IntelligenceV2Service{}
   169  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   170  		return nil, err
   171  	}
   172  
   173  	return ps, err
   174  }
   175  
   176  // Optional parameters for the method 'ListService'
   177  type ListServiceParams struct {
   178  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   179  	PageSize *int `json:"PageSize,omitempty"`
   180  	// Max number of records to return.
   181  	Limit *int `json:"limit,omitempty"`
   182  }
   183  
   184  func (params *ListServiceParams) SetPageSize(PageSize int) *ListServiceParams {
   185  	params.PageSize = &PageSize
   186  	return params
   187  }
   188  func (params *ListServiceParams) SetLimit(Limit int) *ListServiceParams {
   189  	params.Limit = &Limit
   190  	return params
   191  }
   192  
   193  // Retrieve a single page of Service records from the API. Request is executed immediately.
   194  func (c *ApiService) PageService(params *ListServiceParams, pageToken, pageNumber string) (*ListServiceResponse, error) {
   195  	path := "/v2/Services"
   196  
   197  	data := url.Values{}
   198  	headers := make(map[string]interface{})
   199  
   200  	if params != nil && params.PageSize != nil {
   201  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   202  	}
   203  
   204  	if pageToken != "" {
   205  		data.Set("PageToken", pageToken)
   206  	}
   207  	if pageNumber != "" {
   208  		data.Set("Page", pageNumber)
   209  	}
   210  
   211  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   212  	if err != nil {
   213  		return nil, err
   214  	}
   215  
   216  	defer resp.Body.Close()
   217  
   218  	ps := &ListServiceResponse{}
   219  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	return ps, err
   224  }
   225  
   226  // Lists Service records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   227  func (c *ApiService) ListService(params *ListServiceParams) ([]IntelligenceV2Service, error) {
   228  	response, errors := c.StreamService(params)
   229  
   230  	records := make([]IntelligenceV2Service, 0)
   231  	for record := range response {
   232  		records = append(records, record)
   233  	}
   234  
   235  	if err := <-errors; err != nil {
   236  		return nil, err
   237  	}
   238  
   239  	return records, nil
   240  }
   241  
   242  // Streams Service records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   243  func (c *ApiService) StreamService(params *ListServiceParams) (chan IntelligenceV2Service, chan error) {
   244  	if params == nil {
   245  		params = &ListServiceParams{}
   246  	}
   247  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   248  
   249  	recordChannel := make(chan IntelligenceV2Service, 1)
   250  	errorChannel := make(chan error, 1)
   251  
   252  	response, err := c.PageService(params, "", "")
   253  	if err != nil {
   254  		errorChannel <- err
   255  		close(recordChannel)
   256  		close(errorChannel)
   257  	} else {
   258  		go c.streamService(response, params, recordChannel, errorChannel)
   259  	}
   260  
   261  	return recordChannel, errorChannel
   262  }
   263  
   264  func (c *ApiService) streamService(response *ListServiceResponse, params *ListServiceParams, recordChannel chan IntelligenceV2Service, errorChannel chan error) {
   265  	curRecord := 1
   266  
   267  	for response != nil {
   268  		responseRecords := response.Services
   269  		for item := range responseRecords {
   270  			recordChannel <- responseRecords[item]
   271  			curRecord += 1
   272  			if params.Limit != nil && *params.Limit < curRecord {
   273  				close(recordChannel)
   274  				close(errorChannel)
   275  				return
   276  			}
   277  		}
   278  
   279  		record, err := client.GetNext(c.baseURL, response, c.getNextListServiceResponse)
   280  		if err != nil {
   281  			errorChannel <- err
   282  			break
   283  		} else if record == nil {
   284  			break
   285  		}
   286  
   287  		response = record.(*ListServiceResponse)
   288  	}
   289  
   290  	close(recordChannel)
   291  	close(errorChannel)
   292  }
   293  
   294  func (c *ApiService) getNextListServiceResponse(nextPageUrl string) (interface{}, error) {
   295  	if nextPageUrl == "" {
   296  		return nil, nil
   297  	}
   298  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   299  	if err != nil {
   300  		return nil, err
   301  	}
   302  
   303  	defer resp.Body.Close()
   304  
   305  	ps := &ListServiceResponse{}
   306  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   307  		return nil, err
   308  	}
   309  	return ps, nil
   310  }
   311  
   312  // Optional parameters for the method 'UpdateService'
   313  type UpdateServiceParams struct {
   314  	// The If-Match HTTP request header
   315  	IfMatch *string `json:"If-Match,omitempty"`
   316  	// Instructs the Speech Recognition service to automatically transcribe all recordings made on the account.
   317  	AutoTranscribe *bool `json:"AutoTranscribe,omitempty"`
   318  	// Data logging allows Twilio to improve the quality of the speech recognition & language understanding services through using customer data to refine, fine tune and evaluate machine learning models. Note: Data logging cannot be activated via API, only via www.twilio.com, as it requires additional consent.
   319  	DataLogging *bool `json:"DataLogging,omitempty"`
   320  	// A human readable description of this resource, up to 64 characters.
   321  	FriendlyName *string `json:"FriendlyName,omitempty"`
   322  	// The default language code of the audio.
   323  	LanguageCode *string `json:"LanguageCode,omitempty"`
   324  	// Provides a unique and addressable name to be assigned to this Service, assigned by the developer, to be optionally used in addition to SID.
   325  	UniqueName *string `json:"UniqueName,omitempty"`
   326  	// Instructs the Speech Recognition service to automatically redact PII from all transcripts made on this service.
   327  	AutoRedaction *bool `json:"AutoRedaction,omitempty"`
   328  	// Instructs the Speech Recognition service to automatically redact PII from all transcripts media made on this service. The auto_redaction flag must be enabled, results in error otherwise.
   329  	MediaRedaction *bool `json:"MediaRedaction,omitempty"`
   330  	// The URL Twilio will request when executing the Webhook.
   331  	WebhookUrl *string `json:"WebhookUrl,omitempty"`
   332  	//
   333  	WebhookHttpMethod *string `json:"WebhookHttpMethod,omitempty"`
   334  }
   335  
   336  func (params *UpdateServiceParams) SetIfMatch(IfMatch string) *UpdateServiceParams {
   337  	params.IfMatch = &IfMatch
   338  	return params
   339  }
   340  func (params *UpdateServiceParams) SetAutoTranscribe(AutoTranscribe bool) *UpdateServiceParams {
   341  	params.AutoTranscribe = &AutoTranscribe
   342  	return params
   343  }
   344  func (params *UpdateServiceParams) SetDataLogging(DataLogging bool) *UpdateServiceParams {
   345  	params.DataLogging = &DataLogging
   346  	return params
   347  }
   348  func (params *UpdateServiceParams) SetFriendlyName(FriendlyName string) *UpdateServiceParams {
   349  	params.FriendlyName = &FriendlyName
   350  	return params
   351  }
   352  func (params *UpdateServiceParams) SetLanguageCode(LanguageCode string) *UpdateServiceParams {
   353  	params.LanguageCode = &LanguageCode
   354  	return params
   355  }
   356  func (params *UpdateServiceParams) SetUniqueName(UniqueName string) *UpdateServiceParams {
   357  	params.UniqueName = &UniqueName
   358  	return params
   359  }
   360  func (params *UpdateServiceParams) SetAutoRedaction(AutoRedaction bool) *UpdateServiceParams {
   361  	params.AutoRedaction = &AutoRedaction
   362  	return params
   363  }
   364  func (params *UpdateServiceParams) SetMediaRedaction(MediaRedaction bool) *UpdateServiceParams {
   365  	params.MediaRedaction = &MediaRedaction
   366  	return params
   367  }
   368  func (params *UpdateServiceParams) SetWebhookUrl(WebhookUrl string) *UpdateServiceParams {
   369  	params.WebhookUrl = &WebhookUrl
   370  	return params
   371  }
   372  func (params *UpdateServiceParams) SetWebhookHttpMethod(WebhookHttpMethod string) *UpdateServiceParams {
   373  	params.WebhookHttpMethod = &WebhookHttpMethod
   374  	return params
   375  }
   376  
   377  // Update a specific Service.
   378  func (c *ApiService) UpdateService(Sid string, params *UpdateServiceParams) (*IntelligenceV2Service, error) {
   379  	path := "/v2/Services/{Sid}"
   380  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   381  
   382  	data := url.Values{}
   383  	headers := make(map[string]interface{})
   384  
   385  	if params != nil && params.AutoTranscribe != nil {
   386  		data.Set("AutoTranscribe", fmt.Sprint(*params.AutoTranscribe))
   387  	}
   388  	if params != nil && params.DataLogging != nil {
   389  		data.Set("DataLogging", fmt.Sprint(*params.DataLogging))
   390  	}
   391  	if params != nil && params.FriendlyName != nil {
   392  		data.Set("FriendlyName", *params.FriendlyName)
   393  	}
   394  	if params != nil && params.LanguageCode != nil {
   395  		data.Set("LanguageCode", *params.LanguageCode)
   396  	}
   397  	if params != nil && params.UniqueName != nil {
   398  		data.Set("UniqueName", *params.UniqueName)
   399  	}
   400  	if params != nil && params.AutoRedaction != nil {
   401  		data.Set("AutoRedaction", fmt.Sprint(*params.AutoRedaction))
   402  	}
   403  	if params != nil && params.MediaRedaction != nil {
   404  		data.Set("MediaRedaction", fmt.Sprint(*params.MediaRedaction))
   405  	}
   406  	if params != nil && params.WebhookUrl != nil {
   407  		data.Set("WebhookUrl", *params.WebhookUrl)
   408  	}
   409  	if params != nil && params.WebhookHttpMethod != nil {
   410  		data.Set("WebhookHttpMethod", *params.WebhookHttpMethod)
   411  	}
   412  
   413  	if params != nil && params.IfMatch != nil {
   414  		headers["If-Match"] = *params.IfMatch
   415  	}
   416  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   417  	if err != nil {
   418  		return nil, err
   419  	}
   420  
   421  	defer resp.Body.Close()
   422  
   423  	ps := &IntelligenceV2Service{}
   424  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   425  		return nil, err
   426  	}
   427  
   428  	return ps, err
   429  }