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

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Supersim
     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 'CreateSim'
    27  type CreateSimParams struct {
    28  	// The [ICCID](https://en.wikipedia.org/wiki/Subscriber_identity_module#ICCID) of the Super SIM to be added to your Account.
    29  	Iccid *string `json:"Iccid,omitempty"`
    30  	// The 10-digit code required to claim the Super SIM for your Account.
    31  	RegistrationCode *string `json:"RegistrationCode,omitempty"`
    32  }
    33  
    34  func (params *CreateSimParams) SetIccid(Iccid string) *CreateSimParams {
    35  	params.Iccid = &Iccid
    36  	return params
    37  }
    38  func (params *CreateSimParams) SetRegistrationCode(RegistrationCode string) *CreateSimParams {
    39  	params.RegistrationCode = &RegistrationCode
    40  	return params
    41  }
    42  
    43  // Register a Super SIM to your Account
    44  func (c *ApiService) CreateSim(params *CreateSimParams) (*SupersimV1Sim, error) {
    45  	path := "/v1/Sims"
    46  
    47  	data := url.Values{}
    48  	headers := make(map[string]interface{})
    49  
    50  	if params != nil && params.Iccid != nil {
    51  		data.Set("Iccid", *params.Iccid)
    52  	}
    53  	if params != nil && params.RegistrationCode != nil {
    54  		data.Set("RegistrationCode", *params.RegistrationCode)
    55  	}
    56  
    57  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	defer resp.Body.Close()
    63  
    64  	ps := &SupersimV1Sim{}
    65  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    66  		return nil, err
    67  	}
    68  
    69  	return ps, err
    70  }
    71  
    72  // Fetch a Super SIM instance from your account.
    73  func (c *ApiService) FetchSim(Sid string) (*SupersimV1Sim, error) {
    74  	path := "/v1/Sims/{Sid}"
    75  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
    76  
    77  	data := url.Values{}
    78  	headers := make(map[string]interface{})
    79  
    80  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  
    85  	defer resp.Body.Close()
    86  
    87  	ps := &SupersimV1Sim{}
    88  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    89  		return nil, err
    90  	}
    91  
    92  	return ps, err
    93  }
    94  
    95  // Optional parameters for the method 'ListSim'
    96  type ListSimParams struct {
    97  	// The status of the Sim resources to read. Can be `new`, `ready`, `active`, `inactive`, or `scheduled`.
    98  	Status *string `json:"Status,omitempty"`
    99  	// The SID or unique name of the Fleet to which a list of Sims are assigned.
   100  	Fleet *string `json:"Fleet,omitempty"`
   101  	// The [ICCID](https://en.wikipedia.org/wiki/Subscriber_identity_module#ICCID) associated with a Super SIM to filter the list by. Passing this parameter will always return a list containing zero or one SIMs.
   102  	Iccid *string `json:"Iccid,omitempty"`
   103  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   104  	PageSize *int `json:"PageSize,omitempty"`
   105  	// Max number of records to return.
   106  	Limit *int `json:"limit,omitempty"`
   107  }
   108  
   109  func (params *ListSimParams) SetStatus(Status string) *ListSimParams {
   110  	params.Status = &Status
   111  	return params
   112  }
   113  func (params *ListSimParams) SetFleet(Fleet string) *ListSimParams {
   114  	params.Fleet = &Fleet
   115  	return params
   116  }
   117  func (params *ListSimParams) SetIccid(Iccid string) *ListSimParams {
   118  	params.Iccid = &Iccid
   119  	return params
   120  }
   121  func (params *ListSimParams) SetPageSize(PageSize int) *ListSimParams {
   122  	params.PageSize = &PageSize
   123  	return params
   124  }
   125  func (params *ListSimParams) SetLimit(Limit int) *ListSimParams {
   126  	params.Limit = &Limit
   127  	return params
   128  }
   129  
   130  // Retrieve a single page of Sim records from the API. Request is executed immediately.
   131  func (c *ApiService) PageSim(params *ListSimParams, pageToken, pageNumber string) (*ListSimResponse, error) {
   132  	path := "/v1/Sims"
   133  
   134  	data := url.Values{}
   135  	headers := make(map[string]interface{})
   136  
   137  	if params != nil && params.Status != nil {
   138  		data.Set("Status", *params.Status)
   139  	}
   140  	if params != nil && params.Fleet != nil {
   141  		data.Set("Fleet", *params.Fleet)
   142  	}
   143  	if params != nil && params.Iccid != nil {
   144  		data.Set("Iccid", *params.Iccid)
   145  	}
   146  	if params != nil && params.PageSize != nil {
   147  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   148  	}
   149  
   150  	if pageToken != "" {
   151  		data.Set("PageToken", pageToken)
   152  	}
   153  	if pageNumber != "" {
   154  		data.Set("Page", pageNumber)
   155  	}
   156  
   157  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   158  	if err != nil {
   159  		return nil, err
   160  	}
   161  
   162  	defer resp.Body.Close()
   163  
   164  	ps := &ListSimResponse{}
   165  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   166  		return nil, err
   167  	}
   168  
   169  	return ps, err
   170  }
   171  
   172  // Lists Sim records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   173  func (c *ApiService) ListSim(params *ListSimParams) ([]SupersimV1Sim, error) {
   174  	response, errors := c.StreamSim(params)
   175  
   176  	records := make([]SupersimV1Sim, 0)
   177  	for record := range response {
   178  		records = append(records, record)
   179  	}
   180  
   181  	if err := <-errors; err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return records, nil
   186  }
   187  
   188  // Streams Sim records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   189  func (c *ApiService) StreamSim(params *ListSimParams) (chan SupersimV1Sim, chan error) {
   190  	if params == nil {
   191  		params = &ListSimParams{}
   192  	}
   193  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   194  
   195  	recordChannel := make(chan SupersimV1Sim, 1)
   196  	errorChannel := make(chan error, 1)
   197  
   198  	response, err := c.PageSim(params, "", "")
   199  	if err != nil {
   200  		errorChannel <- err
   201  		close(recordChannel)
   202  		close(errorChannel)
   203  	} else {
   204  		go c.streamSim(response, params, recordChannel, errorChannel)
   205  	}
   206  
   207  	return recordChannel, errorChannel
   208  }
   209  
   210  func (c *ApiService) streamSim(response *ListSimResponse, params *ListSimParams, recordChannel chan SupersimV1Sim, errorChannel chan error) {
   211  	curRecord := 1
   212  
   213  	for response != nil {
   214  		responseRecords := response.Sims
   215  		for item := range responseRecords {
   216  			recordChannel <- responseRecords[item]
   217  			curRecord += 1
   218  			if params.Limit != nil && *params.Limit < curRecord {
   219  				close(recordChannel)
   220  				close(errorChannel)
   221  				return
   222  			}
   223  		}
   224  
   225  		record, err := client.GetNext(c.baseURL, response, c.getNextListSimResponse)
   226  		if err != nil {
   227  			errorChannel <- err
   228  			break
   229  		} else if record == nil {
   230  			break
   231  		}
   232  
   233  		response = record.(*ListSimResponse)
   234  	}
   235  
   236  	close(recordChannel)
   237  	close(errorChannel)
   238  }
   239  
   240  func (c *ApiService) getNextListSimResponse(nextPageUrl string) (interface{}, error) {
   241  	if nextPageUrl == "" {
   242  		return nil, nil
   243  	}
   244  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   245  	if err != nil {
   246  		return nil, err
   247  	}
   248  
   249  	defer resp.Body.Close()
   250  
   251  	ps := &ListSimResponse{}
   252  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   253  		return nil, err
   254  	}
   255  	return ps, nil
   256  }
   257  
   258  // Optional parameters for the method 'UpdateSim'
   259  type UpdateSimParams struct {
   260  	// An application-defined string that uniquely identifies the resource. It can be used in place of the resource's `sid` in the URL to address the resource.
   261  	UniqueName *string `json:"UniqueName,omitempty"`
   262  	//
   263  	Status *string `json:"Status,omitempty"`
   264  	// The SID or unique name of the Fleet to which the SIM resource should be assigned.
   265  	Fleet *string `json:"Fleet,omitempty"`
   266  	// The URL we should call using the `callback_method` after an asynchronous update has finished.
   267  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
   268  	// The HTTP method we should use to call `callback_url`. Can be: `GET` or `POST` and the default is POST.
   269  	CallbackMethod *string `json:"CallbackMethod,omitempty"`
   270  	// The SID of the Account to which the Sim resource should belong. The Account SID can only be that of the requesting Account or that of a Subaccount of the requesting Account. Only valid when the Sim resource's status is new.
   271  	AccountSid *string `json:"AccountSid,omitempty"`
   272  }
   273  
   274  func (params *UpdateSimParams) SetUniqueName(UniqueName string) *UpdateSimParams {
   275  	params.UniqueName = &UniqueName
   276  	return params
   277  }
   278  func (params *UpdateSimParams) SetStatus(Status string) *UpdateSimParams {
   279  	params.Status = &Status
   280  	return params
   281  }
   282  func (params *UpdateSimParams) SetFleet(Fleet string) *UpdateSimParams {
   283  	params.Fleet = &Fleet
   284  	return params
   285  }
   286  func (params *UpdateSimParams) SetCallbackUrl(CallbackUrl string) *UpdateSimParams {
   287  	params.CallbackUrl = &CallbackUrl
   288  	return params
   289  }
   290  func (params *UpdateSimParams) SetCallbackMethod(CallbackMethod string) *UpdateSimParams {
   291  	params.CallbackMethod = &CallbackMethod
   292  	return params
   293  }
   294  func (params *UpdateSimParams) SetAccountSid(AccountSid string) *UpdateSimParams {
   295  	params.AccountSid = &AccountSid
   296  	return params
   297  }
   298  
   299  // Updates the given properties of a Super SIM instance from your account.
   300  func (c *ApiService) UpdateSim(Sid string, params *UpdateSimParams) (*SupersimV1Sim, error) {
   301  	path := "/v1/Sims/{Sid}"
   302  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   303  
   304  	data := url.Values{}
   305  	headers := make(map[string]interface{})
   306  
   307  	if params != nil && params.UniqueName != nil {
   308  		data.Set("UniqueName", *params.UniqueName)
   309  	}
   310  	if params != nil && params.Status != nil {
   311  		data.Set("Status", *params.Status)
   312  	}
   313  	if params != nil && params.Fleet != nil {
   314  		data.Set("Fleet", *params.Fleet)
   315  	}
   316  	if params != nil && params.CallbackUrl != nil {
   317  		data.Set("CallbackUrl", *params.CallbackUrl)
   318  	}
   319  	if params != nil && params.CallbackMethod != nil {
   320  		data.Set("CallbackMethod", *params.CallbackMethod)
   321  	}
   322  	if params != nil && params.AccountSid != nil {
   323  		data.Set("AccountSid", *params.AccountSid)
   324  	}
   325  
   326  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   327  	if err != nil {
   328  		return nil, err
   329  	}
   330  
   331  	defer resp.Body.Close()
   332  
   333  	ps := &SupersimV1Sim{}
   334  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   335  		return nil, err
   336  	}
   337  
   338  	return ps, err
   339  }