github.com/twilio/twilio-go@v1.20.1/rest/conversations/v1/configuration_addresses.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Conversations
     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 'CreateConfigurationAddress'
    27  type CreateConfigurationAddressParams struct {
    28  	//
    29  	Type *string `json:"Type,omitempty"`
    30  	// The unique address to be configured. The address can be a whatsapp address or phone number
    31  	Address *string `json:"Address,omitempty"`
    32  	// The human-readable name of this configuration, limited to 256 characters. Optional.
    33  	FriendlyName *string `json:"FriendlyName,omitempty"`
    34  	// Enable/Disable auto-creating conversations for messages to this address
    35  	AutoCreationEnabled *bool `json:"AutoCreation.Enabled,omitempty"`
    36  	//
    37  	AutoCreationType *string `json:"AutoCreation.Type,omitempty"`
    38  	// Conversation Service for the auto-created conversation. If not set, the conversation is created in the default service.
    39  	AutoCreationConversationServiceSid *string `json:"AutoCreation.ConversationServiceSid,omitempty"`
    40  	// For type `webhook`, the url for the webhook request.
    41  	AutoCreationWebhookUrl *string `json:"AutoCreation.WebhookUrl,omitempty"`
    42  	//
    43  	AutoCreationWebhookMethod *string `json:"AutoCreation.WebhookMethod,omitempty"`
    44  	// The list of events, firing webhook event for this Conversation. Values can be any of the following: `onMessageAdded`, `onMessageUpdated`, `onMessageRemoved`, `onConversationUpdated`, `onConversationStateUpdated`, `onConversationRemoved`, `onParticipantAdded`, `onParticipantUpdated`, `onParticipantRemoved`, `onDeliveryUpdated`
    45  	AutoCreationWebhookFilters *[]string `json:"AutoCreation.WebhookFilters,omitempty"`
    46  	// For type `studio`, the studio flow SID where the webhook should be sent to.
    47  	AutoCreationStudioFlowSid *string `json:"AutoCreation.StudioFlowSid,omitempty"`
    48  	// For type `studio`, number of times to retry the webhook request
    49  	AutoCreationStudioRetryCount *int `json:"AutoCreation.StudioRetryCount,omitempty"`
    50  	// An ISO 3166-1 alpha-2n country code which the address belongs to. This is currently only applicable to short code addresses.
    51  	AddressCountry *string `json:"AddressCountry,omitempty"`
    52  }
    53  
    54  func (params *CreateConfigurationAddressParams) SetType(Type string) *CreateConfigurationAddressParams {
    55  	params.Type = &Type
    56  	return params
    57  }
    58  func (params *CreateConfigurationAddressParams) SetAddress(Address string) *CreateConfigurationAddressParams {
    59  	params.Address = &Address
    60  	return params
    61  }
    62  func (params *CreateConfigurationAddressParams) SetFriendlyName(FriendlyName string) *CreateConfigurationAddressParams {
    63  	params.FriendlyName = &FriendlyName
    64  	return params
    65  }
    66  func (params *CreateConfigurationAddressParams) SetAutoCreationEnabled(AutoCreationEnabled bool) *CreateConfigurationAddressParams {
    67  	params.AutoCreationEnabled = &AutoCreationEnabled
    68  	return params
    69  }
    70  func (params *CreateConfigurationAddressParams) SetAutoCreationType(AutoCreationType string) *CreateConfigurationAddressParams {
    71  	params.AutoCreationType = &AutoCreationType
    72  	return params
    73  }
    74  func (params *CreateConfigurationAddressParams) SetAutoCreationConversationServiceSid(AutoCreationConversationServiceSid string) *CreateConfigurationAddressParams {
    75  	params.AutoCreationConversationServiceSid = &AutoCreationConversationServiceSid
    76  	return params
    77  }
    78  func (params *CreateConfigurationAddressParams) SetAutoCreationWebhookUrl(AutoCreationWebhookUrl string) *CreateConfigurationAddressParams {
    79  	params.AutoCreationWebhookUrl = &AutoCreationWebhookUrl
    80  	return params
    81  }
    82  func (params *CreateConfigurationAddressParams) SetAutoCreationWebhookMethod(AutoCreationWebhookMethod string) *CreateConfigurationAddressParams {
    83  	params.AutoCreationWebhookMethod = &AutoCreationWebhookMethod
    84  	return params
    85  }
    86  func (params *CreateConfigurationAddressParams) SetAutoCreationWebhookFilters(AutoCreationWebhookFilters []string) *CreateConfigurationAddressParams {
    87  	params.AutoCreationWebhookFilters = &AutoCreationWebhookFilters
    88  	return params
    89  }
    90  func (params *CreateConfigurationAddressParams) SetAutoCreationStudioFlowSid(AutoCreationStudioFlowSid string) *CreateConfigurationAddressParams {
    91  	params.AutoCreationStudioFlowSid = &AutoCreationStudioFlowSid
    92  	return params
    93  }
    94  func (params *CreateConfigurationAddressParams) SetAutoCreationStudioRetryCount(AutoCreationStudioRetryCount int) *CreateConfigurationAddressParams {
    95  	params.AutoCreationStudioRetryCount = &AutoCreationStudioRetryCount
    96  	return params
    97  }
    98  func (params *CreateConfigurationAddressParams) SetAddressCountry(AddressCountry string) *CreateConfigurationAddressParams {
    99  	params.AddressCountry = &AddressCountry
   100  	return params
   101  }
   102  
   103  // Create a new address configuration
   104  func (c *ApiService) CreateConfigurationAddress(params *CreateConfigurationAddressParams) (*ConversationsV1ConfigurationAddress, error) {
   105  	path := "/v1/Configuration/Addresses"
   106  
   107  	data := url.Values{}
   108  	headers := make(map[string]interface{})
   109  
   110  	if params != nil && params.Type != nil {
   111  		data.Set("Type", *params.Type)
   112  	}
   113  	if params != nil && params.Address != nil {
   114  		data.Set("Address", *params.Address)
   115  	}
   116  	if params != nil && params.FriendlyName != nil {
   117  		data.Set("FriendlyName", *params.FriendlyName)
   118  	}
   119  	if params != nil && params.AutoCreationEnabled != nil {
   120  		data.Set("AutoCreation.Enabled", fmt.Sprint(*params.AutoCreationEnabled))
   121  	}
   122  	if params != nil && params.AutoCreationType != nil {
   123  		data.Set("AutoCreation.Type", *params.AutoCreationType)
   124  	}
   125  	if params != nil && params.AutoCreationConversationServiceSid != nil {
   126  		data.Set("AutoCreation.ConversationServiceSid", *params.AutoCreationConversationServiceSid)
   127  	}
   128  	if params != nil && params.AutoCreationWebhookUrl != nil {
   129  		data.Set("AutoCreation.WebhookUrl", *params.AutoCreationWebhookUrl)
   130  	}
   131  	if params != nil && params.AutoCreationWebhookMethod != nil {
   132  		data.Set("AutoCreation.WebhookMethod", *params.AutoCreationWebhookMethod)
   133  	}
   134  	if params != nil && params.AutoCreationWebhookFilters != nil {
   135  		for _, item := range *params.AutoCreationWebhookFilters {
   136  			data.Add("AutoCreation.WebhookFilters", item)
   137  		}
   138  	}
   139  	if params != nil && params.AutoCreationStudioFlowSid != nil {
   140  		data.Set("AutoCreation.StudioFlowSid", *params.AutoCreationStudioFlowSid)
   141  	}
   142  	if params != nil && params.AutoCreationStudioRetryCount != nil {
   143  		data.Set("AutoCreation.StudioRetryCount", fmt.Sprint(*params.AutoCreationStudioRetryCount))
   144  	}
   145  	if params != nil && params.AddressCountry != nil {
   146  		data.Set("AddressCountry", *params.AddressCountry)
   147  	}
   148  
   149  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	defer resp.Body.Close()
   155  
   156  	ps := &ConversationsV1ConfigurationAddress{}
   157  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   158  		return nil, err
   159  	}
   160  
   161  	return ps, err
   162  }
   163  
   164  // Remove an existing address configuration
   165  func (c *ApiService) DeleteConfigurationAddress(Sid string) error {
   166  	path := "/v1/Configuration/Addresses/{Sid}"
   167  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   168  
   169  	data := url.Values{}
   170  	headers := make(map[string]interface{})
   171  
   172  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   173  	if err != nil {
   174  		return err
   175  	}
   176  
   177  	defer resp.Body.Close()
   178  
   179  	return nil
   180  }
   181  
   182  // Fetch an address configuration
   183  func (c *ApiService) FetchConfigurationAddress(Sid string) (*ConversationsV1ConfigurationAddress, error) {
   184  	path := "/v1/Configuration/Addresses/{Sid}"
   185  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   186  
   187  	data := url.Values{}
   188  	headers := make(map[string]interface{})
   189  
   190  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   191  	if err != nil {
   192  		return nil, err
   193  	}
   194  
   195  	defer resp.Body.Close()
   196  
   197  	ps := &ConversationsV1ConfigurationAddress{}
   198  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   199  		return nil, err
   200  	}
   201  
   202  	return ps, err
   203  }
   204  
   205  // Optional parameters for the method 'ListConfigurationAddress'
   206  type ListConfigurationAddressParams struct {
   207  	// Filter the address configurations by its type. This value can be one of: `whatsapp`, `sms`.
   208  	Type *string `json:"Type,omitempty"`
   209  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   210  	PageSize *int `json:"PageSize,omitempty"`
   211  	// Max number of records to return.
   212  	Limit *int `json:"limit,omitempty"`
   213  }
   214  
   215  func (params *ListConfigurationAddressParams) SetType(Type string) *ListConfigurationAddressParams {
   216  	params.Type = &Type
   217  	return params
   218  }
   219  func (params *ListConfigurationAddressParams) SetPageSize(PageSize int) *ListConfigurationAddressParams {
   220  	params.PageSize = &PageSize
   221  	return params
   222  }
   223  func (params *ListConfigurationAddressParams) SetLimit(Limit int) *ListConfigurationAddressParams {
   224  	params.Limit = &Limit
   225  	return params
   226  }
   227  
   228  // Retrieve a single page of ConfigurationAddress records from the API. Request is executed immediately.
   229  func (c *ApiService) PageConfigurationAddress(params *ListConfigurationAddressParams, pageToken, pageNumber string) (*ListConfigurationAddressResponse, error) {
   230  	path := "/v1/Configuration/Addresses"
   231  
   232  	data := url.Values{}
   233  	headers := make(map[string]interface{})
   234  
   235  	if params != nil && params.Type != nil {
   236  		data.Set("Type", *params.Type)
   237  	}
   238  	if params != nil && params.PageSize != nil {
   239  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   240  	}
   241  
   242  	if pageToken != "" {
   243  		data.Set("PageToken", pageToken)
   244  	}
   245  	if pageNumber != "" {
   246  		data.Set("Page", pageNumber)
   247  	}
   248  
   249  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	defer resp.Body.Close()
   255  
   256  	ps := &ListConfigurationAddressResponse{}
   257  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   258  		return nil, err
   259  	}
   260  
   261  	return ps, err
   262  }
   263  
   264  // Lists ConfigurationAddress records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   265  func (c *ApiService) ListConfigurationAddress(params *ListConfigurationAddressParams) ([]ConversationsV1ConfigurationAddress, error) {
   266  	response, errors := c.StreamConfigurationAddress(params)
   267  
   268  	records := make([]ConversationsV1ConfigurationAddress, 0)
   269  	for record := range response {
   270  		records = append(records, record)
   271  	}
   272  
   273  	if err := <-errors; err != nil {
   274  		return nil, err
   275  	}
   276  
   277  	return records, nil
   278  }
   279  
   280  // Streams ConfigurationAddress records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   281  func (c *ApiService) StreamConfigurationAddress(params *ListConfigurationAddressParams) (chan ConversationsV1ConfigurationAddress, chan error) {
   282  	if params == nil {
   283  		params = &ListConfigurationAddressParams{}
   284  	}
   285  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   286  
   287  	recordChannel := make(chan ConversationsV1ConfigurationAddress, 1)
   288  	errorChannel := make(chan error, 1)
   289  
   290  	response, err := c.PageConfigurationAddress(params, "", "")
   291  	if err != nil {
   292  		errorChannel <- err
   293  		close(recordChannel)
   294  		close(errorChannel)
   295  	} else {
   296  		go c.streamConfigurationAddress(response, params, recordChannel, errorChannel)
   297  	}
   298  
   299  	return recordChannel, errorChannel
   300  }
   301  
   302  func (c *ApiService) streamConfigurationAddress(response *ListConfigurationAddressResponse, params *ListConfigurationAddressParams, recordChannel chan ConversationsV1ConfigurationAddress, errorChannel chan error) {
   303  	curRecord := 1
   304  
   305  	for response != nil {
   306  		responseRecords := response.AddressConfigurations
   307  		for item := range responseRecords {
   308  			recordChannel <- responseRecords[item]
   309  			curRecord += 1
   310  			if params.Limit != nil && *params.Limit < curRecord {
   311  				close(recordChannel)
   312  				close(errorChannel)
   313  				return
   314  			}
   315  		}
   316  
   317  		record, err := client.GetNext(c.baseURL, response, c.getNextListConfigurationAddressResponse)
   318  		if err != nil {
   319  			errorChannel <- err
   320  			break
   321  		} else if record == nil {
   322  			break
   323  		}
   324  
   325  		response = record.(*ListConfigurationAddressResponse)
   326  	}
   327  
   328  	close(recordChannel)
   329  	close(errorChannel)
   330  }
   331  
   332  func (c *ApiService) getNextListConfigurationAddressResponse(nextPageUrl string) (interface{}, error) {
   333  	if nextPageUrl == "" {
   334  		return nil, nil
   335  	}
   336  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   337  	if err != nil {
   338  		return nil, err
   339  	}
   340  
   341  	defer resp.Body.Close()
   342  
   343  	ps := &ListConfigurationAddressResponse{}
   344  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   345  		return nil, err
   346  	}
   347  	return ps, nil
   348  }
   349  
   350  // Optional parameters for the method 'UpdateConfigurationAddress'
   351  type UpdateConfigurationAddressParams struct {
   352  	// The human-readable name of this configuration, limited to 256 characters. Optional.
   353  	FriendlyName *string `json:"FriendlyName,omitempty"`
   354  	// Enable/Disable auto-creating conversations for messages to this address
   355  	AutoCreationEnabled *bool `json:"AutoCreation.Enabled,omitempty"`
   356  	//
   357  	AutoCreationType *string `json:"AutoCreation.Type,omitempty"`
   358  	// Conversation Service for the auto-created conversation. If not set, the conversation is created in the default service.
   359  	AutoCreationConversationServiceSid *string `json:"AutoCreation.ConversationServiceSid,omitempty"`
   360  	// For type `webhook`, the url for the webhook request.
   361  	AutoCreationWebhookUrl *string `json:"AutoCreation.WebhookUrl,omitempty"`
   362  	//
   363  	AutoCreationWebhookMethod *string `json:"AutoCreation.WebhookMethod,omitempty"`
   364  	// The list of events, firing webhook event for this Conversation. Values can be any of the following: `onMessageAdded`, `onMessageUpdated`, `onMessageRemoved`, `onConversationUpdated`, `onConversationStateUpdated`, `onConversationRemoved`, `onParticipantAdded`, `onParticipantUpdated`, `onParticipantRemoved`, `onDeliveryUpdated`
   365  	AutoCreationWebhookFilters *[]string `json:"AutoCreation.WebhookFilters,omitempty"`
   366  	// For type `studio`, the studio flow SID where the webhook should be sent to.
   367  	AutoCreationStudioFlowSid *string `json:"AutoCreation.StudioFlowSid,omitempty"`
   368  	// For type `studio`, number of times to retry the webhook request
   369  	AutoCreationStudioRetryCount *int `json:"AutoCreation.StudioRetryCount,omitempty"`
   370  }
   371  
   372  func (params *UpdateConfigurationAddressParams) SetFriendlyName(FriendlyName string) *UpdateConfigurationAddressParams {
   373  	params.FriendlyName = &FriendlyName
   374  	return params
   375  }
   376  func (params *UpdateConfigurationAddressParams) SetAutoCreationEnabled(AutoCreationEnabled bool) *UpdateConfigurationAddressParams {
   377  	params.AutoCreationEnabled = &AutoCreationEnabled
   378  	return params
   379  }
   380  func (params *UpdateConfigurationAddressParams) SetAutoCreationType(AutoCreationType string) *UpdateConfigurationAddressParams {
   381  	params.AutoCreationType = &AutoCreationType
   382  	return params
   383  }
   384  func (params *UpdateConfigurationAddressParams) SetAutoCreationConversationServiceSid(AutoCreationConversationServiceSid string) *UpdateConfigurationAddressParams {
   385  	params.AutoCreationConversationServiceSid = &AutoCreationConversationServiceSid
   386  	return params
   387  }
   388  func (params *UpdateConfigurationAddressParams) SetAutoCreationWebhookUrl(AutoCreationWebhookUrl string) *UpdateConfigurationAddressParams {
   389  	params.AutoCreationWebhookUrl = &AutoCreationWebhookUrl
   390  	return params
   391  }
   392  func (params *UpdateConfigurationAddressParams) SetAutoCreationWebhookMethod(AutoCreationWebhookMethod string) *UpdateConfigurationAddressParams {
   393  	params.AutoCreationWebhookMethod = &AutoCreationWebhookMethod
   394  	return params
   395  }
   396  func (params *UpdateConfigurationAddressParams) SetAutoCreationWebhookFilters(AutoCreationWebhookFilters []string) *UpdateConfigurationAddressParams {
   397  	params.AutoCreationWebhookFilters = &AutoCreationWebhookFilters
   398  	return params
   399  }
   400  func (params *UpdateConfigurationAddressParams) SetAutoCreationStudioFlowSid(AutoCreationStudioFlowSid string) *UpdateConfigurationAddressParams {
   401  	params.AutoCreationStudioFlowSid = &AutoCreationStudioFlowSid
   402  	return params
   403  }
   404  func (params *UpdateConfigurationAddressParams) SetAutoCreationStudioRetryCount(AutoCreationStudioRetryCount int) *UpdateConfigurationAddressParams {
   405  	params.AutoCreationStudioRetryCount = &AutoCreationStudioRetryCount
   406  	return params
   407  }
   408  
   409  // Update an existing address configuration
   410  func (c *ApiService) UpdateConfigurationAddress(Sid string, params *UpdateConfigurationAddressParams) (*ConversationsV1ConfigurationAddress, error) {
   411  	path := "/v1/Configuration/Addresses/{Sid}"
   412  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   413  
   414  	data := url.Values{}
   415  	headers := make(map[string]interface{})
   416  
   417  	if params != nil && params.FriendlyName != nil {
   418  		data.Set("FriendlyName", *params.FriendlyName)
   419  	}
   420  	if params != nil && params.AutoCreationEnabled != nil {
   421  		data.Set("AutoCreation.Enabled", fmt.Sprint(*params.AutoCreationEnabled))
   422  	}
   423  	if params != nil && params.AutoCreationType != nil {
   424  		data.Set("AutoCreation.Type", *params.AutoCreationType)
   425  	}
   426  	if params != nil && params.AutoCreationConversationServiceSid != nil {
   427  		data.Set("AutoCreation.ConversationServiceSid", *params.AutoCreationConversationServiceSid)
   428  	}
   429  	if params != nil && params.AutoCreationWebhookUrl != nil {
   430  		data.Set("AutoCreation.WebhookUrl", *params.AutoCreationWebhookUrl)
   431  	}
   432  	if params != nil && params.AutoCreationWebhookMethod != nil {
   433  		data.Set("AutoCreation.WebhookMethod", *params.AutoCreationWebhookMethod)
   434  	}
   435  	if params != nil && params.AutoCreationWebhookFilters != nil {
   436  		for _, item := range *params.AutoCreationWebhookFilters {
   437  			data.Add("AutoCreation.WebhookFilters", item)
   438  		}
   439  	}
   440  	if params != nil && params.AutoCreationStudioFlowSid != nil {
   441  		data.Set("AutoCreation.StudioFlowSid", *params.AutoCreationStudioFlowSid)
   442  	}
   443  	if params != nil && params.AutoCreationStudioRetryCount != nil {
   444  		data.Set("AutoCreation.StudioRetryCount", fmt.Sprint(*params.AutoCreationStudioRetryCount))
   445  	}
   446  
   447  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   448  	if err != nil {
   449  		return nil, err
   450  	}
   451  
   452  	defer resp.Body.Close()
   453  
   454  	ps := &ConversationsV1ConfigurationAddress{}
   455  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   456  		return nil, err
   457  	}
   458  
   459  	return ps, err
   460  }