github.com/twilio/twilio-go@v1.20.1/rest/chat/v2/credentials.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Chat
     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 'CreateCredential'
    27  type CreateCredentialParams struct {
    28  	//
    29  	Type *string `json:"Type,omitempty"`
    30  	// A descriptive string that you create to describe the new resource. It can be up to 64 characters long.
    31  	FriendlyName *string `json:"FriendlyName,omitempty"`
    32  	// [APN only] The URL encoded representation of the certificate. For example,  `-----BEGIN CERTIFICATE----- MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END CERTIFICATE-----`
    33  	Certificate *string `json:"Certificate,omitempty"`
    34  	// [APN only] The URL encoded representation of the private key. For example, `-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG... -----END RSA PRIVATE KEY-----`
    35  	PrivateKey *string `json:"PrivateKey,omitempty"`
    36  	// [APN only] Whether to send the credential to sandbox APNs. Can be `true` to send to sandbox APNs or `false` to send to production.
    37  	Sandbox *bool `json:"Sandbox,omitempty"`
    38  	// [GCM only] The API key for the project that was obtained from the Google Developer console for your GCM Service application credential.
    39  	ApiKey *string `json:"ApiKey,omitempty"`
    40  	// [FCM only] The **Server key** of your project from the Firebase console, found under Settings / Cloud messaging.
    41  	Secret *string `json:"Secret,omitempty"`
    42  }
    43  
    44  func (params *CreateCredentialParams) SetType(Type string) *CreateCredentialParams {
    45  	params.Type = &Type
    46  	return params
    47  }
    48  func (params *CreateCredentialParams) SetFriendlyName(FriendlyName string) *CreateCredentialParams {
    49  	params.FriendlyName = &FriendlyName
    50  	return params
    51  }
    52  func (params *CreateCredentialParams) SetCertificate(Certificate string) *CreateCredentialParams {
    53  	params.Certificate = &Certificate
    54  	return params
    55  }
    56  func (params *CreateCredentialParams) SetPrivateKey(PrivateKey string) *CreateCredentialParams {
    57  	params.PrivateKey = &PrivateKey
    58  	return params
    59  }
    60  func (params *CreateCredentialParams) SetSandbox(Sandbox bool) *CreateCredentialParams {
    61  	params.Sandbox = &Sandbox
    62  	return params
    63  }
    64  func (params *CreateCredentialParams) SetApiKey(ApiKey string) *CreateCredentialParams {
    65  	params.ApiKey = &ApiKey
    66  	return params
    67  }
    68  func (params *CreateCredentialParams) SetSecret(Secret string) *CreateCredentialParams {
    69  	params.Secret = &Secret
    70  	return params
    71  }
    72  
    73  //
    74  func (c *ApiService) CreateCredential(params *CreateCredentialParams) (*ChatV2Credential, error) {
    75  	path := "/v2/Credentials"
    76  
    77  	data := url.Values{}
    78  	headers := make(map[string]interface{})
    79  
    80  	if params != nil && params.Type != nil {
    81  		data.Set("Type", *params.Type)
    82  	}
    83  	if params != nil && params.FriendlyName != nil {
    84  		data.Set("FriendlyName", *params.FriendlyName)
    85  	}
    86  	if params != nil && params.Certificate != nil {
    87  		data.Set("Certificate", *params.Certificate)
    88  	}
    89  	if params != nil && params.PrivateKey != nil {
    90  		data.Set("PrivateKey", *params.PrivateKey)
    91  	}
    92  	if params != nil && params.Sandbox != nil {
    93  		data.Set("Sandbox", fmt.Sprint(*params.Sandbox))
    94  	}
    95  	if params != nil && params.ApiKey != nil {
    96  		data.Set("ApiKey", *params.ApiKey)
    97  	}
    98  	if params != nil && params.Secret != nil {
    99  		data.Set("Secret", *params.Secret)
   100  	}
   101  
   102  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	defer resp.Body.Close()
   108  
   109  	ps := &ChatV2Credential{}
   110  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   111  		return nil, err
   112  	}
   113  
   114  	return ps, err
   115  }
   116  
   117  //
   118  func (c *ApiService) DeleteCredential(Sid string) error {
   119  	path := "/v2/Credentials/{Sid}"
   120  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   121  
   122  	data := url.Values{}
   123  	headers := make(map[string]interface{})
   124  
   125  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   126  	if err != nil {
   127  		return err
   128  	}
   129  
   130  	defer resp.Body.Close()
   131  
   132  	return nil
   133  }
   134  
   135  //
   136  func (c *ApiService) FetchCredential(Sid string) (*ChatV2Credential, error) {
   137  	path := "/v2/Credentials/{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.Get(c.baseURL+path, data, headers)
   144  	if err != nil {
   145  		return nil, err
   146  	}
   147  
   148  	defer resp.Body.Close()
   149  
   150  	ps := &ChatV2Credential{}
   151  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   152  		return nil, err
   153  	}
   154  
   155  	return ps, err
   156  }
   157  
   158  // Optional parameters for the method 'ListCredential'
   159  type ListCredentialParams struct {
   160  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   161  	PageSize *int `json:"PageSize,omitempty"`
   162  	// Max number of records to return.
   163  	Limit *int `json:"limit,omitempty"`
   164  }
   165  
   166  func (params *ListCredentialParams) SetPageSize(PageSize int) *ListCredentialParams {
   167  	params.PageSize = &PageSize
   168  	return params
   169  }
   170  func (params *ListCredentialParams) SetLimit(Limit int) *ListCredentialParams {
   171  	params.Limit = &Limit
   172  	return params
   173  }
   174  
   175  // Retrieve a single page of Credential records from the API. Request is executed immediately.
   176  func (c *ApiService) PageCredential(params *ListCredentialParams, pageToken, pageNumber string) (*ListCredentialResponse, error) {
   177  	path := "/v2/Credentials"
   178  
   179  	data := url.Values{}
   180  	headers := make(map[string]interface{})
   181  
   182  	if params != nil && params.PageSize != nil {
   183  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   184  	}
   185  
   186  	if pageToken != "" {
   187  		data.Set("PageToken", pageToken)
   188  	}
   189  	if pageNumber != "" {
   190  		data.Set("Page", pageNumber)
   191  	}
   192  
   193  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   194  	if err != nil {
   195  		return nil, err
   196  	}
   197  
   198  	defer resp.Body.Close()
   199  
   200  	ps := &ListCredentialResponse{}
   201  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   202  		return nil, err
   203  	}
   204  
   205  	return ps, err
   206  }
   207  
   208  // Lists Credential records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   209  func (c *ApiService) ListCredential(params *ListCredentialParams) ([]ChatV2Credential, error) {
   210  	response, errors := c.StreamCredential(params)
   211  
   212  	records := make([]ChatV2Credential, 0)
   213  	for record := range response {
   214  		records = append(records, record)
   215  	}
   216  
   217  	if err := <-errors; err != nil {
   218  		return nil, err
   219  	}
   220  
   221  	return records, nil
   222  }
   223  
   224  // Streams Credential records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   225  func (c *ApiService) StreamCredential(params *ListCredentialParams) (chan ChatV2Credential, chan error) {
   226  	if params == nil {
   227  		params = &ListCredentialParams{}
   228  	}
   229  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   230  
   231  	recordChannel := make(chan ChatV2Credential, 1)
   232  	errorChannel := make(chan error, 1)
   233  
   234  	response, err := c.PageCredential(params, "", "")
   235  	if err != nil {
   236  		errorChannel <- err
   237  		close(recordChannel)
   238  		close(errorChannel)
   239  	} else {
   240  		go c.streamCredential(response, params, recordChannel, errorChannel)
   241  	}
   242  
   243  	return recordChannel, errorChannel
   244  }
   245  
   246  func (c *ApiService) streamCredential(response *ListCredentialResponse, params *ListCredentialParams, recordChannel chan ChatV2Credential, errorChannel chan error) {
   247  	curRecord := 1
   248  
   249  	for response != nil {
   250  		responseRecords := response.Credentials
   251  		for item := range responseRecords {
   252  			recordChannel <- responseRecords[item]
   253  			curRecord += 1
   254  			if params.Limit != nil && *params.Limit < curRecord {
   255  				close(recordChannel)
   256  				close(errorChannel)
   257  				return
   258  			}
   259  		}
   260  
   261  		record, err := client.GetNext(c.baseURL, response, c.getNextListCredentialResponse)
   262  		if err != nil {
   263  			errorChannel <- err
   264  			break
   265  		} else if record == nil {
   266  			break
   267  		}
   268  
   269  		response = record.(*ListCredentialResponse)
   270  	}
   271  
   272  	close(recordChannel)
   273  	close(errorChannel)
   274  }
   275  
   276  func (c *ApiService) getNextListCredentialResponse(nextPageUrl string) (interface{}, error) {
   277  	if nextPageUrl == "" {
   278  		return nil, nil
   279  	}
   280  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   281  	if err != nil {
   282  		return nil, err
   283  	}
   284  
   285  	defer resp.Body.Close()
   286  
   287  	ps := &ListCredentialResponse{}
   288  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   289  		return nil, err
   290  	}
   291  	return ps, nil
   292  }
   293  
   294  // Optional parameters for the method 'UpdateCredential'
   295  type UpdateCredentialParams struct {
   296  	// A descriptive string that you create to describe the resource. It can be up to 64 characters long.
   297  	FriendlyName *string `json:"FriendlyName,omitempty"`
   298  	// [APN only] The URL encoded representation of the certificate. For example,  `-----BEGIN CERTIFICATE----- MIIFnTCCBIWgAwIBAgIIAjy9H849+E8wDQYJKoZIhvcNAQEF.....A== -----END CERTIFICATE-----`
   299  	Certificate *string `json:"Certificate,omitempty"`
   300  	// [APN only] The URL encoded representation of the private key. For example, `-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAuyf/lNrH9ck8DmNyo3fG... -----END RSA PRIVATE KEY-----`
   301  	PrivateKey *string `json:"PrivateKey,omitempty"`
   302  	// [APN only] Whether to send the credential to sandbox APNs. Can be `true` to send to sandbox APNs or `false` to send to production.
   303  	Sandbox *bool `json:"Sandbox,omitempty"`
   304  	// [GCM only] The API key for the project that was obtained from the Google Developer console for your GCM Service application credential.
   305  	ApiKey *string `json:"ApiKey,omitempty"`
   306  	// [FCM only] The **Server key** of your project from the Firebase console, found under Settings / Cloud messaging.
   307  	Secret *string `json:"Secret,omitempty"`
   308  }
   309  
   310  func (params *UpdateCredentialParams) SetFriendlyName(FriendlyName string) *UpdateCredentialParams {
   311  	params.FriendlyName = &FriendlyName
   312  	return params
   313  }
   314  func (params *UpdateCredentialParams) SetCertificate(Certificate string) *UpdateCredentialParams {
   315  	params.Certificate = &Certificate
   316  	return params
   317  }
   318  func (params *UpdateCredentialParams) SetPrivateKey(PrivateKey string) *UpdateCredentialParams {
   319  	params.PrivateKey = &PrivateKey
   320  	return params
   321  }
   322  func (params *UpdateCredentialParams) SetSandbox(Sandbox bool) *UpdateCredentialParams {
   323  	params.Sandbox = &Sandbox
   324  	return params
   325  }
   326  func (params *UpdateCredentialParams) SetApiKey(ApiKey string) *UpdateCredentialParams {
   327  	params.ApiKey = &ApiKey
   328  	return params
   329  }
   330  func (params *UpdateCredentialParams) SetSecret(Secret string) *UpdateCredentialParams {
   331  	params.Secret = &Secret
   332  	return params
   333  }
   334  
   335  //
   336  func (c *ApiService) UpdateCredential(Sid string, params *UpdateCredentialParams) (*ChatV2Credential, error) {
   337  	path := "/v2/Credentials/{Sid}"
   338  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   339  
   340  	data := url.Values{}
   341  	headers := make(map[string]interface{})
   342  
   343  	if params != nil && params.FriendlyName != nil {
   344  		data.Set("FriendlyName", *params.FriendlyName)
   345  	}
   346  	if params != nil && params.Certificate != nil {
   347  		data.Set("Certificate", *params.Certificate)
   348  	}
   349  	if params != nil && params.PrivateKey != nil {
   350  		data.Set("PrivateKey", *params.PrivateKey)
   351  	}
   352  	if params != nil && params.Sandbox != nil {
   353  		data.Set("Sandbox", fmt.Sprint(*params.Sandbox))
   354  	}
   355  	if params != nil && params.ApiKey != nil {
   356  		data.Set("ApiKey", *params.ApiKey)
   357  	}
   358  	if params != nil && params.Secret != nil {
   359  		data.Set("Secret", *params.Secret)
   360  	}
   361  
   362  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   363  	if err != nil {
   364  		return nil, err
   365  	}
   366  
   367  	defer resp.Body.Close()
   368  
   369  	ps := &ChatV2Credential{}
   370  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   371  		return nil, err
   372  	}
   373  
   374  	return ps, err
   375  }