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

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Proxy
     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  	// An application-defined string that uniquely identifies the resource. This value must be 191 characters or fewer in length and be unique. **This value should not have PII.**
    29  	UniqueName *string `json:"UniqueName,omitempty"`
    30  	// The default `ttl` value to set for Sessions created in the Service. The TTL (time to live) is measured in seconds after the Session's last create or last Interaction. The default value of `0` indicates an unlimited Session length. You can override a Session's default TTL value by setting its `ttl` value.
    31  	DefaultTtl *int `json:"DefaultTtl,omitempty"`
    32  	// The URL we should call when the interaction status changes.
    33  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
    34  	//
    35  	GeoMatchLevel *string `json:"GeoMatchLevel,omitempty"`
    36  	//
    37  	NumberSelectionBehavior *string `json:"NumberSelectionBehavior,omitempty"`
    38  	// The URL we call on each interaction. If we receive a 403 status, we block the interaction; otherwise the interaction continues.
    39  	InterceptCallbackUrl *string `json:"InterceptCallbackUrl,omitempty"`
    40  	// The URL we should call when an inbound call or SMS action occurs on a closed or non-existent Session. If your server (or a Twilio [function](https://www.twilio.com/en-us/serverless/functions)) responds with valid [TwiML](https://www.twilio.com/docs/voice/twiml), we will process it. This means it is possible, for example, to play a message for a call, send an automated text message response, or redirect a call to another Phone Number. See [Out-of-Session Callback Response Guide](https://www.twilio.com/docs/proxy/out-session-callback-response-guide) for more information.
    41  	OutOfSessionCallbackUrl *string `json:"OutOfSessionCallbackUrl,omitempty"`
    42  	// The SID of the Chat Service Instance managed by Proxy Service. The Chat Service enables Proxy to forward SMS and channel messages to this chat instance. This is a one-to-one relationship.
    43  	ChatInstanceSid *string `json:"ChatInstanceSid,omitempty"`
    44  }
    45  
    46  func (params *CreateServiceParams) SetUniqueName(UniqueName string) *CreateServiceParams {
    47  	params.UniqueName = &UniqueName
    48  	return params
    49  }
    50  func (params *CreateServiceParams) SetDefaultTtl(DefaultTtl int) *CreateServiceParams {
    51  	params.DefaultTtl = &DefaultTtl
    52  	return params
    53  }
    54  func (params *CreateServiceParams) SetCallbackUrl(CallbackUrl string) *CreateServiceParams {
    55  	params.CallbackUrl = &CallbackUrl
    56  	return params
    57  }
    58  func (params *CreateServiceParams) SetGeoMatchLevel(GeoMatchLevel string) *CreateServiceParams {
    59  	params.GeoMatchLevel = &GeoMatchLevel
    60  	return params
    61  }
    62  func (params *CreateServiceParams) SetNumberSelectionBehavior(NumberSelectionBehavior string) *CreateServiceParams {
    63  	params.NumberSelectionBehavior = &NumberSelectionBehavior
    64  	return params
    65  }
    66  func (params *CreateServiceParams) SetInterceptCallbackUrl(InterceptCallbackUrl string) *CreateServiceParams {
    67  	params.InterceptCallbackUrl = &InterceptCallbackUrl
    68  	return params
    69  }
    70  func (params *CreateServiceParams) SetOutOfSessionCallbackUrl(OutOfSessionCallbackUrl string) *CreateServiceParams {
    71  	params.OutOfSessionCallbackUrl = &OutOfSessionCallbackUrl
    72  	return params
    73  }
    74  func (params *CreateServiceParams) SetChatInstanceSid(ChatInstanceSid string) *CreateServiceParams {
    75  	params.ChatInstanceSid = &ChatInstanceSid
    76  	return params
    77  }
    78  
    79  // Create a new Service for Twilio Proxy
    80  func (c *ApiService) CreateService(params *CreateServiceParams) (*ProxyV1Service, error) {
    81  	path := "/v1/Services"
    82  
    83  	data := url.Values{}
    84  	headers := make(map[string]interface{})
    85  
    86  	if params != nil && params.UniqueName != nil {
    87  		data.Set("UniqueName", *params.UniqueName)
    88  	}
    89  	if params != nil && params.DefaultTtl != nil {
    90  		data.Set("DefaultTtl", fmt.Sprint(*params.DefaultTtl))
    91  	}
    92  	if params != nil && params.CallbackUrl != nil {
    93  		data.Set("CallbackUrl", *params.CallbackUrl)
    94  	}
    95  	if params != nil && params.GeoMatchLevel != nil {
    96  		data.Set("GeoMatchLevel", *params.GeoMatchLevel)
    97  	}
    98  	if params != nil && params.NumberSelectionBehavior != nil {
    99  		data.Set("NumberSelectionBehavior", *params.NumberSelectionBehavior)
   100  	}
   101  	if params != nil && params.InterceptCallbackUrl != nil {
   102  		data.Set("InterceptCallbackUrl", *params.InterceptCallbackUrl)
   103  	}
   104  	if params != nil && params.OutOfSessionCallbackUrl != nil {
   105  		data.Set("OutOfSessionCallbackUrl", *params.OutOfSessionCallbackUrl)
   106  	}
   107  	if params != nil && params.ChatInstanceSid != nil {
   108  		data.Set("ChatInstanceSid", *params.ChatInstanceSid)
   109  	}
   110  
   111  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	defer resp.Body.Close()
   117  
   118  	ps := &ProxyV1Service{}
   119  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return ps, err
   124  }
   125  
   126  // Delete a specific Service.
   127  func (c *ApiService) DeleteService(Sid string) error {
   128  	path := "/v1/Services/{Sid}"
   129  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   130  
   131  	data := url.Values{}
   132  	headers := make(map[string]interface{})
   133  
   134  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   135  	if err != nil {
   136  		return err
   137  	}
   138  
   139  	defer resp.Body.Close()
   140  
   141  	return nil
   142  }
   143  
   144  // Fetch a specific Service.
   145  func (c *ApiService) FetchService(Sid string) (*ProxyV1Service, error) {
   146  	path := "/v1/Services/{Sid}"
   147  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   148  
   149  	data := url.Values{}
   150  	headers := make(map[string]interface{})
   151  
   152  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  
   157  	defer resp.Body.Close()
   158  
   159  	ps := &ProxyV1Service{}
   160  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   161  		return nil, err
   162  	}
   163  
   164  	return ps, err
   165  }
   166  
   167  // Optional parameters for the method 'ListService'
   168  type ListServiceParams struct {
   169  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   170  	PageSize *int `json:"PageSize,omitempty"`
   171  	// Max number of records to return.
   172  	Limit *int `json:"limit,omitempty"`
   173  }
   174  
   175  func (params *ListServiceParams) SetPageSize(PageSize int) *ListServiceParams {
   176  	params.PageSize = &PageSize
   177  	return params
   178  }
   179  func (params *ListServiceParams) SetLimit(Limit int) *ListServiceParams {
   180  	params.Limit = &Limit
   181  	return params
   182  }
   183  
   184  // Retrieve a single page of Service records from the API. Request is executed immediately.
   185  func (c *ApiService) PageService(params *ListServiceParams, pageToken, pageNumber string) (*ListServiceResponse, error) {
   186  	path := "/v1/Services"
   187  
   188  	data := url.Values{}
   189  	headers := make(map[string]interface{})
   190  
   191  	if params != nil && params.PageSize != nil {
   192  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   193  	}
   194  
   195  	if pageToken != "" {
   196  		data.Set("PageToken", pageToken)
   197  	}
   198  	if pageNumber != "" {
   199  		data.Set("Page", pageNumber)
   200  	}
   201  
   202  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  
   207  	defer resp.Body.Close()
   208  
   209  	ps := &ListServiceResponse{}
   210  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   211  		return nil, err
   212  	}
   213  
   214  	return ps, err
   215  }
   216  
   217  // Lists Service records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   218  func (c *ApiService) ListService(params *ListServiceParams) ([]ProxyV1Service, error) {
   219  	response, errors := c.StreamService(params)
   220  
   221  	records := make([]ProxyV1Service, 0)
   222  	for record := range response {
   223  		records = append(records, record)
   224  	}
   225  
   226  	if err := <-errors; err != nil {
   227  		return nil, err
   228  	}
   229  
   230  	return records, nil
   231  }
   232  
   233  // Streams Service records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   234  func (c *ApiService) StreamService(params *ListServiceParams) (chan ProxyV1Service, chan error) {
   235  	if params == nil {
   236  		params = &ListServiceParams{}
   237  	}
   238  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   239  
   240  	recordChannel := make(chan ProxyV1Service, 1)
   241  	errorChannel := make(chan error, 1)
   242  
   243  	response, err := c.PageService(params, "", "")
   244  	if err != nil {
   245  		errorChannel <- err
   246  		close(recordChannel)
   247  		close(errorChannel)
   248  	} else {
   249  		go c.streamService(response, params, recordChannel, errorChannel)
   250  	}
   251  
   252  	return recordChannel, errorChannel
   253  }
   254  
   255  func (c *ApiService) streamService(response *ListServiceResponse, params *ListServiceParams, recordChannel chan ProxyV1Service, errorChannel chan error) {
   256  	curRecord := 1
   257  
   258  	for response != nil {
   259  		responseRecords := response.Services
   260  		for item := range responseRecords {
   261  			recordChannel <- responseRecords[item]
   262  			curRecord += 1
   263  			if params.Limit != nil && *params.Limit < curRecord {
   264  				close(recordChannel)
   265  				close(errorChannel)
   266  				return
   267  			}
   268  		}
   269  
   270  		record, err := client.GetNext(c.baseURL, response, c.getNextListServiceResponse)
   271  		if err != nil {
   272  			errorChannel <- err
   273  			break
   274  		} else if record == nil {
   275  			break
   276  		}
   277  
   278  		response = record.(*ListServiceResponse)
   279  	}
   280  
   281  	close(recordChannel)
   282  	close(errorChannel)
   283  }
   284  
   285  func (c *ApiService) getNextListServiceResponse(nextPageUrl string) (interface{}, error) {
   286  	if nextPageUrl == "" {
   287  		return nil, nil
   288  	}
   289  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   290  	if err != nil {
   291  		return nil, err
   292  	}
   293  
   294  	defer resp.Body.Close()
   295  
   296  	ps := &ListServiceResponse{}
   297  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   298  		return nil, err
   299  	}
   300  	return ps, nil
   301  }
   302  
   303  // Optional parameters for the method 'UpdateService'
   304  type UpdateServiceParams struct {
   305  	// An application-defined string that uniquely identifies the resource. This value must be 191 characters or fewer in length and be unique. **This value should not have PII.**
   306  	UniqueName *string `json:"UniqueName,omitempty"`
   307  	// The default `ttl` value to set for Sessions created in the Service. The TTL (time to live) is measured in seconds after the Session's last create or last Interaction. The default value of `0` indicates an unlimited Session length. You can override a Session's default TTL value by setting its `ttl` value.
   308  	DefaultTtl *int `json:"DefaultTtl,omitempty"`
   309  	// The URL we should call when the interaction status changes.
   310  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
   311  	//
   312  	GeoMatchLevel *string `json:"GeoMatchLevel,omitempty"`
   313  	//
   314  	NumberSelectionBehavior *string `json:"NumberSelectionBehavior,omitempty"`
   315  	// The URL we call on each interaction. If we receive a 403 status, we block the interaction; otherwise the interaction continues.
   316  	InterceptCallbackUrl *string `json:"InterceptCallbackUrl,omitempty"`
   317  	// The URL we should call when an inbound call or SMS action occurs on a closed or non-existent Session. If your server (or a Twilio [function](https://www.twilio.com/en-us/serverless/functions)) responds with valid [TwiML](https://www.twilio.com/docs/voice/twiml), we will process it. This means it is possible, for example, to play a message for a call, send an automated text message response, or redirect a call to another Phone Number. See [Out-of-Session Callback Response Guide](https://www.twilio.com/docs/proxy/out-session-callback-response-guide) for more information.
   318  	OutOfSessionCallbackUrl *string `json:"OutOfSessionCallbackUrl,omitempty"`
   319  	// The SID of the Chat Service Instance managed by Proxy Service. The Chat Service enables Proxy to forward SMS and channel messages to this chat instance. This is a one-to-one relationship.
   320  	ChatInstanceSid *string `json:"ChatInstanceSid,omitempty"`
   321  }
   322  
   323  func (params *UpdateServiceParams) SetUniqueName(UniqueName string) *UpdateServiceParams {
   324  	params.UniqueName = &UniqueName
   325  	return params
   326  }
   327  func (params *UpdateServiceParams) SetDefaultTtl(DefaultTtl int) *UpdateServiceParams {
   328  	params.DefaultTtl = &DefaultTtl
   329  	return params
   330  }
   331  func (params *UpdateServiceParams) SetCallbackUrl(CallbackUrl string) *UpdateServiceParams {
   332  	params.CallbackUrl = &CallbackUrl
   333  	return params
   334  }
   335  func (params *UpdateServiceParams) SetGeoMatchLevel(GeoMatchLevel string) *UpdateServiceParams {
   336  	params.GeoMatchLevel = &GeoMatchLevel
   337  	return params
   338  }
   339  func (params *UpdateServiceParams) SetNumberSelectionBehavior(NumberSelectionBehavior string) *UpdateServiceParams {
   340  	params.NumberSelectionBehavior = &NumberSelectionBehavior
   341  	return params
   342  }
   343  func (params *UpdateServiceParams) SetInterceptCallbackUrl(InterceptCallbackUrl string) *UpdateServiceParams {
   344  	params.InterceptCallbackUrl = &InterceptCallbackUrl
   345  	return params
   346  }
   347  func (params *UpdateServiceParams) SetOutOfSessionCallbackUrl(OutOfSessionCallbackUrl string) *UpdateServiceParams {
   348  	params.OutOfSessionCallbackUrl = &OutOfSessionCallbackUrl
   349  	return params
   350  }
   351  func (params *UpdateServiceParams) SetChatInstanceSid(ChatInstanceSid string) *UpdateServiceParams {
   352  	params.ChatInstanceSid = &ChatInstanceSid
   353  	return params
   354  }
   355  
   356  // Update a specific Service.
   357  func (c *ApiService) UpdateService(Sid string, params *UpdateServiceParams) (*ProxyV1Service, error) {
   358  	path := "/v1/Services/{Sid}"
   359  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   360  
   361  	data := url.Values{}
   362  	headers := make(map[string]interface{})
   363  
   364  	if params != nil && params.UniqueName != nil {
   365  		data.Set("UniqueName", *params.UniqueName)
   366  	}
   367  	if params != nil && params.DefaultTtl != nil {
   368  		data.Set("DefaultTtl", fmt.Sprint(*params.DefaultTtl))
   369  	}
   370  	if params != nil && params.CallbackUrl != nil {
   371  		data.Set("CallbackUrl", *params.CallbackUrl)
   372  	}
   373  	if params != nil && params.GeoMatchLevel != nil {
   374  		data.Set("GeoMatchLevel", *params.GeoMatchLevel)
   375  	}
   376  	if params != nil && params.NumberSelectionBehavior != nil {
   377  		data.Set("NumberSelectionBehavior", *params.NumberSelectionBehavior)
   378  	}
   379  	if params != nil && params.InterceptCallbackUrl != nil {
   380  		data.Set("InterceptCallbackUrl", *params.InterceptCallbackUrl)
   381  	}
   382  	if params != nil && params.OutOfSessionCallbackUrl != nil {
   383  		data.Set("OutOfSessionCallbackUrl", *params.OutOfSessionCallbackUrl)
   384  	}
   385  	if params != nil && params.ChatInstanceSid != nil {
   386  		data.Set("ChatInstanceSid", *params.ChatInstanceSid)
   387  	}
   388  
   389  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   390  	if err != nil {
   391  		return nil, err
   392  	}
   393  
   394  	defer resp.Body.Close()
   395  
   396  	ps := &ProxyV1Service{}
   397  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   398  		return nil, err
   399  	}
   400  
   401  	return ps, err
   402  }