github.com/twilio/twilio-go@v1.20.1/rest/api/v2010/accounts_sip_credential_lists_credentials.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Api
     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 'CreateSipCredential'
    27  type CreateSipCredentialParams struct {
    28  	// The unique id of the Account that is responsible for this resource.
    29  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    30  	// The username that will be passed when authenticating SIP requests. The username should be sent in response to Twilio's challenge of the initial INVITE. It can be up to 32 characters long.
    31  	Username *string `json:"Username,omitempty"`
    32  	// The password that the username will use when authenticating SIP requests. The password must be a minimum of 12 characters, contain at least 1 digit, and have mixed case. (eg `IWasAtSignal2018`)
    33  	Password *string `json:"Password,omitempty"`
    34  }
    35  
    36  func (params *CreateSipCredentialParams) SetPathAccountSid(PathAccountSid string) *CreateSipCredentialParams {
    37  	params.PathAccountSid = &PathAccountSid
    38  	return params
    39  }
    40  func (params *CreateSipCredentialParams) SetUsername(Username string) *CreateSipCredentialParams {
    41  	params.Username = &Username
    42  	return params
    43  }
    44  func (params *CreateSipCredentialParams) SetPassword(Password string) *CreateSipCredentialParams {
    45  	params.Password = &Password
    46  	return params
    47  }
    48  
    49  // Create a new credential resource.
    50  func (c *ApiService) CreateSipCredential(CredentialListSid string, params *CreateSipCredentialParams) (*ApiV2010SipCredential, error) {
    51  	path := "/2010-04-01/Accounts/{AccountSid}/SIP/CredentialLists/{CredentialListSid}/Credentials.json"
    52  	if params != nil && params.PathAccountSid != nil {
    53  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
    54  	} else {
    55  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
    56  	}
    57  	path = strings.Replace(path, "{"+"CredentialListSid"+"}", CredentialListSid, -1)
    58  
    59  	data := url.Values{}
    60  	headers := make(map[string]interface{})
    61  
    62  	if params != nil && params.Username != nil {
    63  		data.Set("Username", *params.Username)
    64  	}
    65  	if params != nil && params.Password != nil {
    66  		data.Set("Password", *params.Password)
    67  	}
    68  
    69  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	defer resp.Body.Close()
    75  
    76  	ps := &ApiV2010SipCredential{}
    77  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	return ps, err
    82  }
    83  
    84  // Optional parameters for the method 'DeleteSipCredential'
    85  type DeleteSipCredentialParams struct {
    86  	// The unique id of the Account that is responsible for this resource.
    87  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
    88  }
    89  
    90  func (params *DeleteSipCredentialParams) SetPathAccountSid(PathAccountSid string) *DeleteSipCredentialParams {
    91  	params.PathAccountSid = &PathAccountSid
    92  	return params
    93  }
    94  
    95  // Delete a credential resource.
    96  func (c *ApiService) DeleteSipCredential(CredentialListSid string, Sid string, params *DeleteSipCredentialParams) error {
    97  	path := "/2010-04-01/Accounts/{AccountSid}/SIP/CredentialLists/{CredentialListSid}/Credentials/{Sid}.json"
    98  	if params != nil && params.PathAccountSid != nil {
    99  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   100  	} else {
   101  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   102  	}
   103  	path = strings.Replace(path, "{"+"CredentialListSid"+"}", CredentialListSid, -1)
   104  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   105  
   106  	data := url.Values{}
   107  	headers := make(map[string]interface{})
   108  
   109  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   110  	if err != nil {
   111  		return err
   112  	}
   113  
   114  	defer resp.Body.Close()
   115  
   116  	return nil
   117  }
   118  
   119  // Optional parameters for the method 'FetchSipCredential'
   120  type FetchSipCredentialParams struct {
   121  	// The unique id of the Account that is responsible for this resource.
   122  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   123  }
   124  
   125  func (params *FetchSipCredentialParams) SetPathAccountSid(PathAccountSid string) *FetchSipCredentialParams {
   126  	params.PathAccountSid = &PathAccountSid
   127  	return params
   128  }
   129  
   130  // Fetch a single credential.
   131  func (c *ApiService) FetchSipCredential(CredentialListSid string, Sid string, params *FetchSipCredentialParams) (*ApiV2010SipCredential, error) {
   132  	path := "/2010-04-01/Accounts/{AccountSid}/SIP/CredentialLists/{CredentialListSid}/Credentials/{Sid}.json"
   133  	if params != nil && params.PathAccountSid != nil {
   134  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   135  	} else {
   136  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   137  	}
   138  	path = strings.Replace(path, "{"+"CredentialListSid"+"}", CredentialListSid, -1)
   139  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   140  
   141  	data := url.Values{}
   142  	headers := make(map[string]interface{})
   143  
   144  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	defer resp.Body.Close()
   150  
   151  	ps := &ApiV2010SipCredential{}
   152  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	return ps, err
   157  }
   158  
   159  // Optional parameters for the method 'ListSipCredential'
   160  type ListSipCredentialParams struct {
   161  	// The unique id of the Account that is responsible for this resource.
   162  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   163  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   164  	PageSize *int `json:"PageSize,omitempty"`
   165  	// Max number of records to return.
   166  	Limit *int `json:"limit,omitempty"`
   167  }
   168  
   169  func (params *ListSipCredentialParams) SetPathAccountSid(PathAccountSid string) *ListSipCredentialParams {
   170  	params.PathAccountSid = &PathAccountSid
   171  	return params
   172  }
   173  func (params *ListSipCredentialParams) SetPageSize(PageSize int) *ListSipCredentialParams {
   174  	params.PageSize = &PageSize
   175  	return params
   176  }
   177  func (params *ListSipCredentialParams) SetLimit(Limit int) *ListSipCredentialParams {
   178  	params.Limit = &Limit
   179  	return params
   180  }
   181  
   182  // Retrieve a single page of SipCredential records from the API. Request is executed immediately.
   183  func (c *ApiService) PageSipCredential(CredentialListSid string, params *ListSipCredentialParams, pageToken, pageNumber string) (*ListSipCredentialResponse, error) {
   184  	path := "/2010-04-01/Accounts/{AccountSid}/SIP/CredentialLists/{CredentialListSid}/Credentials.json"
   185  
   186  	if params != nil && params.PathAccountSid != nil {
   187  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   188  	} else {
   189  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   190  	}
   191  	path = strings.Replace(path, "{"+"CredentialListSid"+"}", CredentialListSid, -1)
   192  
   193  	data := url.Values{}
   194  	headers := make(map[string]interface{})
   195  
   196  	if params != nil && params.PageSize != nil {
   197  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   198  	}
   199  
   200  	if pageToken != "" {
   201  		data.Set("PageToken", pageToken)
   202  	}
   203  	if pageNumber != "" {
   204  		data.Set("Page", pageNumber)
   205  	}
   206  
   207  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   208  	if err != nil {
   209  		return nil, err
   210  	}
   211  
   212  	defer resp.Body.Close()
   213  
   214  	ps := &ListSipCredentialResponse{}
   215  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   216  		return nil, err
   217  	}
   218  
   219  	return ps, err
   220  }
   221  
   222  // Lists SipCredential records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   223  func (c *ApiService) ListSipCredential(CredentialListSid string, params *ListSipCredentialParams) ([]ApiV2010SipCredential, error) {
   224  	response, errors := c.StreamSipCredential(CredentialListSid, params)
   225  
   226  	records := make([]ApiV2010SipCredential, 0)
   227  	for record := range response {
   228  		records = append(records, record)
   229  	}
   230  
   231  	if err := <-errors; err != nil {
   232  		return nil, err
   233  	}
   234  
   235  	return records, nil
   236  }
   237  
   238  // Streams SipCredential records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   239  func (c *ApiService) StreamSipCredential(CredentialListSid string, params *ListSipCredentialParams) (chan ApiV2010SipCredential, chan error) {
   240  	if params == nil {
   241  		params = &ListSipCredentialParams{}
   242  	}
   243  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   244  
   245  	recordChannel := make(chan ApiV2010SipCredential, 1)
   246  	errorChannel := make(chan error, 1)
   247  
   248  	response, err := c.PageSipCredential(CredentialListSid, params, "", "")
   249  	if err != nil {
   250  		errorChannel <- err
   251  		close(recordChannel)
   252  		close(errorChannel)
   253  	} else {
   254  		go c.streamSipCredential(response, params, recordChannel, errorChannel)
   255  	}
   256  
   257  	return recordChannel, errorChannel
   258  }
   259  
   260  func (c *ApiService) streamSipCredential(response *ListSipCredentialResponse, params *ListSipCredentialParams, recordChannel chan ApiV2010SipCredential, errorChannel chan error) {
   261  	curRecord := 1
   262  
   263  	for response != nil {
   264  		responseRecords := response.Credentials
   265  		for item := range responseRecords {
   266  			recordChannel <- responseRecords[item]
   267  			curRecord += 1
   268  			if params.Limit != nil && *params.Limit < curRecord {
   269  				close(recordChannel)
   270  				close(errorChannel)
   271  				return
   272  			}
   273  		}
   274  
   275  		record, err := client.GetNext(c.baseURL, response, c.getNextListSipCredentialResponse)
   276  		if err != nil {
   277  			errorChannel <- err
   278  			break
   279  		} else if record == nil {
   280  			break
   281  		}
   282  
   283  		response = record.(*ListSipCredentialResponse)
   284  	}
   285  
   286  	close(recordChannel)
   287  	close(errorChannel)
   288  }
   289  
   290  func (c *ApiService) getNextListSipCredentialResponse(nextPageUrl string) (interface{}, error) {
   291  	if nextPageUrl == "" {
   292  		return nil, nil
   293  	}
   294  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   295  	if err != nil {
   296  		return nil, err
   297  	}
   298  
   299  	defer resp.Body.Close()
   300  
   301  	ps := &ListSipCredentialResponse{}
   302  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   303  		return nil, err
   304  	}
   305  	return ps, nil
   306  }
   307  
   308  // Optional parameters for the method 'UpdateSipCredential'
   309  type UpdateSipCredentialParams struct {
   310  	// The unique id of the Account that is responsible for this resource.
   311  	PathAccountSid *string `json:"PathAccountSid,omitempty"`
   312  	// The password that the username will use when authenticating SIP requests. The password must be a minimum of 12 characters, contain at least 1 digit, and have mixed case. (eg `IWasAtSignal2018`)
   313  	Password *string `json:"Password,omitempty"`
   314  }
   315  
   316  func (params *UpdateSipCredentialParams) SetPathAccountSid(PathAccountSid string) *UpdateSipCredentialParams {
   317  	params.PathAccountSid = &PathAccountSid
   318  	return params
   319  }
   320  func (params *UpdateSipCredentialParams) SetPassword(Password string) *UpdateSipCredentialParams {
   321  	params.Password = &Password
   322  	return params
   323  }
   324  
   325  // Update a credential resource.
   326  func (c *ApiService) UpdateSipCredential(CredentialListSid string, Sid string, params *UpdateSipCredentialParams) (*ApiV2010SipCredential, error) {
   327  	path := "/2010-04-01/Accounts/{AccountSid}/SIP/CredentialLists/{CredentialListSid}/Credentials/{Sid}.json"
   328  	if params != nil && params.PathAccountSid != nil {
   329  		path = strings.Replace(path, "{"+"AccountSid"+"}", *params.PathAccountSid, -1)
   330  	} else {
   331  		path = strings.Replace(path, "{"+"AccountSid"+"}", c.requestHandler.Client.AccountSid(), -1)
   332  	}
   333  	path = strings.Replace(path, "{"+"CredentialListSid"+"}", CredentialListSid, -1)
   334  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   335  
   336  	data := url.Values{}
   337  	headers := make(map[string]interface{})
   338  
   339  	if params != nil && params.Password != nil {
   340  		data.Set("Password", *params.Password)
   341  	}
   342  
   343  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   344  	if err != nil {
   345  		return nil, err
   346  	}
   347  
   348  	defer resp.Body.Close()
   349  
   350  	ps := &ApiV2010SipCredential{}
   351  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   352  		return nil, err
   353  	}
   354  
   355  	return ps, err
   356  }