github.com/twilio/twilio-go@v1.20.1/rest/wireless/v1/sims.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Wireless
     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  // Delete a Sim resource on your Account.
    27  func (c *ApiService) DeleteSim(Sid string) error {
    28  	path := "/v1/Sims/{Sid}"
    29  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    30  
    31  	data := url.Values{}
    32  	headers := make(map[string]interface{})
    33  
    34  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
    35  	if err != nil {
    36  		return err
    37  	}
    38  
    39  	defer resp.Body.Close()
    40  
    41  	return nil
    42  }
    43  
    44  // Fetch a Sim resource on your Account.
    45  func (c *ApiService) FetchSim(Sid string) (*WirelessV1Sim, error) {
    46  	path := "/v1/Sims/{Sid}"
    47  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    48  
    49  	data := url.Values{}
    50  	headers := make(map[string]interface{})
    51  
    52  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	defer resp.Body.Close()
    58  
    59  	ps := &WirelessV1Sim{}
    60  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return ps, err
    65  }
    66  
    67  // Optional parameters for the method 'ListSim'
    68  type ListSimParams struct {
    69  	// Only return Sim resources with this status.
    70  	Status *string `json:"Status,omitempty"`
    71  	// Only return Sim resources with this ICCID. This will return a list with a maximum size of 1.
    72  	Iccid *string `json:"Iccid,omitempty"`
    73  	// The SID or unique name of a [RatePlan resource](https://www.twilio.com/docs/iot/wireless/api/rateplan-resource). Only return Sim resources assigned to this RatePlan resource.
    74  	RatePlan *string `json:"RatePlan,omitempty"`
    75  	// Deprecated.
    76  	EId *string `json:"EId,omitempty"`
    77  	// Only return Sim resources with this registration code. This will return a list with a maximum size of 1.
    78  	SimRegistrationCode *string `json:"SimRegistrationCode,omitempty"`
    79  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    80  	PageSize *int `json:"PageSize,omitempty"`
    81  	// Max number of records to return.
    82  	Limit *int `json:"limit,omitempty"`
    83  }
    84  
    85  func (params *ListSimParams) SetStatus(Status string) *ListSimParams {
    86  	params.Status = &Status
    87  	return params
    88  }
    89  func (params *ListSimParams) SetIccid(Iccid string) *ListSimParams {
    90  	params.Iccid = &Iccid
    91  	return params
    92  }
    93  func (params *ListSimParams) SetRatePlan(RatePlan string) *ListSimParams {
    94  	params.RatePlan = &RatePlan
    95  	return params
    96  }
    97  func (params *ListSimParams) SetEId(EId string) *ListSimParams {
    98  	params.EId = &EId
    99  	return params
   100  }
   101  func (params *ListSimParams) SetSimRegistrationCode(SimRegistrationCode string) *ListSimParams {
   102  	params.SimRegistrationCode = &SimRegistrationCode
   103  	return params
   104  }
   105  func (params *ListSimParams) SetPageSize(PageSize int) *ListSimParams {
   106  	params.PageSize = &PageSize
   107  	return params
   108  }
   109  func (params *ListSimParams) SetLimit(Limit int) *ListSimParams {
   110  	params.Limit = &Limit
   111  	return params
   112  }
   113  
   114  // Retrieve a single page of Sim records from the API. Request is executed immediately.
   115  func (c *ApiService) PageSim(params *ListSimParams, pageToken, pageNumber string) (*ListSimResponse, error) {
   116  	path := "/v1/Sims"
   117  
   118  	data := url.Values{}
   119  	headers := make(map[string]interface{})
   120  
   121  	if params != nil && params.Status != nil {
   122  		data.Set("Status", *params.Status)
   123  	}
   124  	if params != nil && params.Iccid != nil {
   125  		data.Set("Iccid", *params.Iccid)
   126  	}
   127  	if params != nil && params.RatePlan != nil {
   128  		data.Set("RatePlan", *params.RatePlan)
   129  	}
   130  	if params != nil && params.EId != nil {
   131  		data.Set("EId", *params.EId)
   132  	}
   133  	if params != nil && params.SimRegistrationCode != nil {
   134  		data.Set("SimRegistrationCode", *params.SimRegistrationCode)
   135  	}
   136  	if params != nil && params.PageSize != nil {
   137  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   138  	}
   139  
   140  	if pageToken != "" {
   141  		data.Set("PageToken", pageToken)
   142  	}
   143  	if pageNumber != "" {
   144  		data.Set("Page", pageNumber)
   145  	}
   146  
   147  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  
   152  	defer resp.Body.Close()
   153  
   154  	ps := &ListSimResponse{}
   155  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   156  		return nil, err
   157  	}
   158  
   159  	return ps, err
   160  }
   161  
   162  // Lists Sim records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   163  func (c *ApiService) ListSim(params *ListSimParams) ([]WirelessV1Sim, error) {
   164  	response, errors := c.StreamSim(params)
   165  
   166  	records := make([]WirelessV1Sim, 0)
   167  	for record := range response {
   168  		records = append(records, record)
   169  	}
   170  
   171  	if err := <-errors; err != nil {
   172  		return nil, err
   173  	}
   174  
   175  	return records, nil
   176  }
   177  
   178  // Streams Sim records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   179  func (c *ApiService) StreamSim(params *ListSimParams) (chan WirelessV1Sim, chan error) {
   180  	if params == nil {
   181  		params = &ListSimParams{}
   182  	}
   183  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   184  
   185  	recordChannel := make(chan WirelessV1Sim, 1)
   186  	errorChannel := make(chan error, 1)
   187  
   188  	response, err := c.PageSim(params, "", "")
   189  	if err != nil {
   190  		errorChannel <- err
   191  		close(recordChannel)
   192  		close(errorChannel)
   193  	} else {
   194  		go c.streamSim(response, params, recordChannel, errorChannel)
   195  	}
   196  
   197  	return recordChannel, errorChannel
   198  }
   199  
   200  func (c *ApiService) streamSim(response *ListSimResponse, params *ListSimParams, recordChannel chan WirelessV1Sim, errorChannel chan error) {
   201  	curRecord := 1
   202  
   203  	for response != nil {
   204  		responseRecords := response.Sims
   205  		for item := range responseRecords {
   206  			recordChannel <- responseRecords[item]
   207  			curRecord += 1
   208  			if params.Limit != nil && *params.Limit < curRecord {
   209  				close(recordChannel)
   210  				close(errorChannel)
   211  				return
   212  			}
   213  		}
   214  
   215  		record, err := client.GetNext(c.baseURL, response, c.getNextListSimResponse)
   216  		if err != nil {
   217  			errorChannel <- err
   218  			break
   219  		} else if record == nil {
   220  			break
   221  		}
   222  
   223  		response = record.(*ListSimResponse)
   224  	}
   225  
   226  	close(recordChannel)
   227  	close(errorChannel)
   228  }
   229  
   230  func (c *ApiService) getNextListSimResponse(nextPageUrl string) (interface{}, error) {
   231  	if nextPageUrl == "" {
   232  		return nil, nil
   233  	}
   234  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   235  	if err != nil {
   236  		return nil, err
   237  	}
   238  
   239  	defer resp.Body.Close()
   240  
   241  	ps := &ListSimResponse{}
   242  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   243  		return nil, err
   244  	}
   245  	return ps, nil
   246  }
   247  
   248  // Optional parameters for the method 'UpdateSim'
   249  type UpdateSimParams struct {
   250  	// An application-defined string that uniquely identifies the resource. It can be used in place of the `sid` in the URL path to address the resource.
   251  	UniqueName *string `json:"UniqueName,omitempty"`
   252  	// The HTTP method we should use to call `callback_url`. Can be: `POST` or `GET`. The default is `POST`.
   253  	CallbackMethod *string `json:"CallbackMethod,omitempty"`
   254  	// The URL we should call using the `callback_url` when the SIM has finished updating. When the SIM transitions from `new` to `ready` or from any status to `deactivated`, we call this URL when the status changes to an intermediate status (`ready` or `deactivated`) and again when the status changes to its final status (`active` or `canceled`).
   255  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
   256  	// A descriptive string that you create to describe the Sim resource. It does not need to be unique.
   257  	FriendlyName *string `json:"FriendlyName,omitempty"`
   258  	// The SID or unique name of the [RatePlan resource](https://www.twilio.com/docs/iot/wireless/api/rateplan-resource) to which the Sim resource should be assigned.
   259  	RatePlan *string `json:"RatePlan,omitempty"`
   260  	//
   261  	Status *string `json:"Status,omitempty"`
   262  	// The HTTP method we should use to call `commands_callback_url`. Can be: `POST` or `GET`. The default is `POST`.
   263  	CommandsCallbackMethod *string `json:"CommandsCallbackMethod,omitempty"`
   264  	// The URL we should call using the `commands_callback_method` when the SIM sends a [Command](https://www.twilio.com/docs/iot/wireless/api/command-resource). Your server should respond with an HTTP status code in the 200 range; any response body is ignored.
   265  	CommandsCallbackUrl *string `json:"CommandsCallbackUrl,omitempty"`
   266  	// The HTTP method we should use to call `sms_fallback_url`. Can be: `GET` or `POST`. Default is `POST`.
   267  	SmsFallbackMethod *string `json:"SmsFallbackMethod,omitempty"`
   268  	// The URL we should call using the `sms_fallback_method` when an error occurs while retrieving or executing the TwiML requested from `sms_url`.
   269  	SmsFallbackUrl *string `json:"SmsFallbackUrl,omitempty"`
   270  	// The HTTP method we should use to call `sms_url`. Can be: `GET` or `POST`. Default is `POST`.
   271  	SmsMethod *string `json:"SmsMethod,omitempty"`
   272  	// The URL we should call using the `sms_method` when the SIM-connected device sends an SMS message that is not a [Command](https://www.twilio.com/docs/iot/wireless/api/command-resource).
   273  	SmsUrl *string `json:"SmsUrl,omitempty"`
   274  	// Deprecated.
   275  	VoiceFallbackMethod *string `json:"VoiceFallbackMethod,omitempty"`
   276  	// Deprecated.
   277  	VoiceFallbackUrl *string `json:"VoiceFallbackUrl,omitempty"`
   278  	// Deprecated.
   279  	VoiceMethod *string `json:"VoiceMethod,omitempty"`
   280  	// Deprecated.
   281  	VoiceUrl *string `json:"VoiceUrl,omitempty"`
   282  	//
   283  	ResetStatus *string `json:"ResetStatus,omitempty"`
   284  	// The SID of the [Account](https://www.twilio.com/docs/iam/api/account) to which the Sim resource should belong. The Account SID can only be that of the requesting Account or that of a [Subaccount](https://www.twilio.com/docs/iam/api/subaccounts) of the requesting Account. Only valid when the Sim resource's status is `new`. For more information, see the [Move SIMs between Subaccounts documentation](https://www.twilio.com/docs/iot/wireless/api/sim-resource#move-sims-between-subaccounts).
   285  	AccountSid *string `json:"AccountSid,omitempty"`
   286  }
   287  
   288  func (params *UpdateSimParams) SetUniqueName(UniqueName string) *UpdateSimParams {
   289  	params.UniqueName = &UniqueName
   290  	return params
   291  }
   292  func (params *UpdateSimParams) SetCallbackMethod(CallbackMethod string) *UpdateSimParams {
   293  	params.CallbackMethod = &CallbackMethod
   294  	return params
   295  }
   296  func (params *UpdateSimParams) SetCallbackUrl(CallbackUrl string) *UpdateSimParams {
   297  	params.CallbackUrl = &CallbackUrl
   298  	return params
   299  }
   300  func (params *UpdateSimParams) SetFriendlyName(FriendlyName string) *UpdateSimParams {
   301  	params.FriendlyName = &FriendlyName
   302  	return params
   303  }
   304  func (params *UpdateSimParams) SetRatePlan(RatePlan string) *UpdateSimParams {
   305  	params.RatePlan = &RatePlan
   306  	return params
   307  }
   308  func (params *UpdateSimParams) SetStatus(Status string) *UpdateSimParams {
   309  	params.Status = &Status
   310  	return params
   311  }
   312  func (params *UpdateSimParams) SetCommandsCallbackMethod(CommandsCallbackMethod string) *UpdateSimParams {
   313  	params.CommandsCallbackMethod = &CommandsCallbackMethod
   314  	return params
   315  }
   316  func (params *UpdateSimParams) SetCommandsCallbackUrl(CommandsCallbackUrl string) *UpdateSimParams {
   317  	params.CommandsCallbackUrl = &CommandsCallbackUrl
   318  	return params
   319  }
   320  func (params *UpdateSimParams) SetSmsFallbackMethod(SmsFallbackMethod string) *UpdateSimParams {
   321  	params.SmsFallbackMethod = &SmsFallbackMethod
   322  	return params
   323  }
   324  func (params *UpdateSimParams) SetSmsFallbackUrl(SmsFallbackUrl string) *UpdateSimParams {
   325  	params.SmsFallbackUrl = &SmsFallbackUrl
   326  	return params
   327  }
   328  func (params *UpdateSimParams) SetSmsMethod(SmsMethod string) *UpdateSimParams {
   329  	params.SmsMethod = &SmsMethod
   330  	return params
   331  }
   332  func (params *UpdateSimParams) SetSmsUrl(SmsUrl string) *UpdateSimParams {
   333  	params.SmsUrl = &SmsUrl
   334  	return params
   335  }
   336  func (params *UpdateSimParams) SetVoiceFallbackMethod(VoiceFallbackMethod string) *UpdateSimParams {
   337  	params.VoiceFallbackMethod = &VoiceFallbackMethod
   338  	return params
   339  }
   340  func (params *UpdateSimParams) SetVoiceFallbackUrl(VoiceFallbackUrl string) *UpdateSimParams {
   341  	params.VoiceFallbackUrl = &VoiceFallbackUrl
   342  	return params
   343  }
   344  func (params *UpdateSimParams) SetVoiceMethod(VoiceMethod string) *UpdateSimParams {
   345  	params.VoiceMethod = &VoiceMethod
   346  	return params
   347  }
   348  func (params *UpdateSimParams) SetVoiceUrl(VoiceUrl string) *UpdateSimParams {
   349  	params.VoiceUrl = &VoiceUrl
   350  	return params
   351  }
   352  func (params *UpdateSimParams) SetResetStatus(ResetStatus string) *UpdateSimParams {
   353  	params.ResetStatus = &ResetStatus
   354  	return params
   355  }
   356  func (params *UpdateSimParams) SetAccountSid(AccountSid string) *UpdateSimParams {
   357  	params.AccountSid = &AccountSid
   358  	return params
   359  }
   360  
   361  // Updates the given properties of a Sim resource on your Account.
   362  func (c *ApiService) UpdateSim(Sid string, params *UpdateSimParams) (*WirelessV1Sim, error) {
   363  	path := "/v1/Sims/{Sid}"
   364  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   365  
   366  	data := url.Values{}
   367  	headers := make(map[string]interface{})
   368  
   369  	if params != nil && params.UniqueName != nil {
   370  		data.Set("UniqueName", *params.UniqueName)
   371  	}
   372  	if params != nil && params.CallbackMethod != nil {
   373  		data.Set("CallbackMethod", *params.CallbackMethod)
   374  	}
   375  	if params != nil && params.CallbackUrl != nil {
   376  		data.Set("CallbackUrl", *params.CallbackUrl)
   377  	}
   378  	if params != nil && params.FriendlyName != nil {
   379  		data.Set("FriendlyName", *params.FriendlyName)
   380  	}
   381  	if params != nil && params.RatePlan != nil {
   382  		data.Set("RatePlan", *params.RatePlan)
   383  	}
   384  	if params != nil && params.Status != nil {
   385  		data.Set("Status", *params.Status)
   386  	}
   387  	if params != nil && params.CommandsCallbackMethod != nil {
   388  		data.Set("CommandsCallbackMethod", *params.CommandsCallbackMethod)
   389  	}
   390  	if params != nil && params.CommandsCallbackUrl != nil {
   391  		data.Set("CommandsCallbackUrl", *params.CommandsCallbackUrl)
   392  	}
   393  	if params != nil && params.SmsFallbackMethod != nil {
   394  		data.Set("SmsFallbackMethod", *params.SmsFallbackMethod)
   395  	}
   396  	if params != nil && params.SmsFallbackUrl != nil {
   397  		data.Set("SmsFallbackUrl", *params.SmsFallbackUrl)
   398  	}
   399  	if params != nil && params.SmsMethod != nil {
   400  		data.Set("SmsMethod", *params.SmsMethod)
   401  	}
   402  	if params != nil && params.SmsUrl != nil {
   403  		data.Set("SmsUrl", *params.SmsUrl)
   404  	}
   405  	if params != nil && params.VoiceFallbackMethod != nil {
   406  		data.Set("VoiceFallbackMethod", *params.VoiceFallbackMethod)
   407  	}
   408  	if params != nil && params.VoiceFallbackUrl != nil {
   409  		data.Set("VoiceFallbackUrl", *params.VoiceFallbackUrl)
   410  	}
   411  	if params != nil && params.VoiceMethod != nil {
   412  		data.Set("VoiceMethod", *params.VoiceMethod)
   413  	}
   414  	if params != nil && params.VoiceUrl != nil {
   415  		data.Set("VoiceUrl", *params.VoiceUrl)
   416  	}
   417  	if params != nil && params.ResetStatus != nil {
   418  		data.Set("ResetStatus", *params.ResetStatus)
   419  	}
   420  	if params != nil && params.AccountSid != nil {
   421  		data.Set("AccountSid", *params.AccountSid)
   422  	}
   423  
   424  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   425  	if err != nil {
   426  		return nil, err
   427  	}
   428  
   429  	defer resp.Body.Close()
   430  
   431  	ps := &WirelessV1Sim{}
   432  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   433  		return nil, err
   434  	}
   435  
   436  	return ps, err
   437  }