github.com/twilio/twilio-go@v1.20.1/rest/supersim/v1/ip_commands.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 'CreateIpCommand'
    27  type CreateIpCommandParams struct {
    28  	// The `sid` or `unique_name` of the [Super SIM](https://www.twilio.com/docs/iot/supersim/api/sim-resource) to send the IP Command to.
    29  	Sim *string `json:"Sim,omitempty"`
    30  	// The data that will be sent to the device. The payload cannot exceed 1300 bytes. If the PayloadType is set to text, the payload is encoded in UTF-8. If PayloadType is set to binary, the payload is encoded in Base64.
    31  	Payload *string `json:"Payload,omitempty"`
    32  	// The device port to which the IP Command will be sent.
    33  	DevicePort *int `json:"DevicePort,omitempty"`
    34  	//
    35  	PayloadType *string `json:"PayloadType,omitempty"`
    36  	// The URL we should call using the `callback_method` after we have sent the IP Command.
    37  	CallbackUrl *string `json:"CallbackUrl,omitempty"`
    38  	// The HTTP method we should use to call `callback_url`. Can be `GET` or `POST`, and the default is `POST`.
    39  	CallbackMethod *string `json:"CallbackMethod,omitempty"`
    40  }
    41  
    42  func (params *CreateIpCommandParams) SetSim(Sim string) *CreateIpCommandParams {
    43  	params.Sim = &Sim
    44  	return params
    45  }
    46  func (params *CreateIpCommandParams) SetPayload(Payload string) *CreateIpCommandParams {
    47  	params.Payload = &Payload
    48  	return params
    49  }
    50  func (params *CreateIpCommandParams) SetDevicePort(DevicePort int) *CreateIpCommandParams {
    51  	params.DevicePort = &DevicePort
    52  	return params
    53  }
    54  func (params *CreateIpCommandParams) SetPayloadType(PayloadType string) *CreateIpCommandParams {
    55  	params.PayloadType = &PayloadType
    56  	return params
    57  }
    58  func (params *CreateIpCommandParams) SetCallbackUrl(CallbackUrl string) *CreateIpCommandParams {
    59  	params.CallbackUrl = &CallbackUrl
    60  	return params
    61  }
    62  func (params *CreateIpCommandParams) SetCallbackMethod(CallbackMethod string) *CreateIpCommandParams {
    63  	params.CallbackMethod = &CallbackMethod
    64  	return params
    65  }
    66  
    67  // Send an IP Command to a Super SIM.
    68  func (c *ApiService) CreateIpCommand(params *CreateIpCommandParams) (*SupersimV1IpCommand, error) {
    69  	path := "/v1/IpCommands"
    70  
    71  	data := url.Values{}
    72  	headers := make(map[string]interface{})
    73  
    74  	if params != nil && params.Sim != nil {
    75  		data.Set("Sim", *params.Sim)
    76  	}
    77  	if params != nil && params.Payload != nil {
    78  		data.Set("Payload", *params.Payload)
    79  	}
    80  	if params != nil && params.DevicePort != nil {
    81  		data.Set("DevicePort", fmt.Sprint(*params.DevicePort))
    82  	}
    83  	if params != nil && params.PayloadType != nil {
    84  		data.Set("PayloadType", *params.PayloadType)
    85  	}
    86  	if params != nil && params.CallbackUrl != nil {
    87  		data.Set("CallbackUrl", *params.CallbackUrl)
    88  	}
    89  	if params != nil && params.CallbackMethod != nil {
    90  		data.Set("CallbackMethod", *params.CallbackMethod)
    91  	}
    92  
    93  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	defer resp.Body.Close()
    99  
   100  	ps := &SupersimV1IpCommand{}
   101  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	return ps, err
   106  }
   107  
   108  // Fetch IP Command instance from your account.
   109  func (c *ApiService) FetchIpCommand(Sid string) (*SupersimV1IpCommand, error) {
   110  	path := "/v1/IpCommands/{Sid}"
   111  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   112  
   113  	data := url.Values{}
   114  	headers := make(map[string]interface{})
   115  
   116  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   117  	if err != nil {
   118  		return nil, err
   119  	}
   120  
   121  	defer resp.Body.Close()
   122  
   123  	ps := &SupersimV1IpCommand{}
   124  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	return ps, err
   129  }
   130  
   131  // Optional parameters for the method 'ListIpCommand'
   132  type ListIpCommandParams struct {
   133  	// The SID or unique name of the Sim resource that IP Command was sent to or from.
   134  	Sim *string `json:"Sim,omitempty"`
   135  	// The ICCID of the Sim resource that IP Command was sent to or from.
   136  	SimIccid *string `json:"SimIccid,omitempty"`
   137  	// The status of the IP Command. Can be: `queued`, `sent`, `received` or `failed`. See the [IP Command Status Values](https://www.twilio.com/docs/iot/supersim/api/ipcommand-resource#status-values) for a description of each.
   138  	Status *string `json:"Status,omitempty"`
   139  	// The direction of the IP Command. Can be `to_sim` or `from_sim`. The value of `to_sim` is synonymous with the term `mobile terminated`, and `from_sim` is synonymous with the term `mobile originated`.
   140  	Direction *string `json:"Direction,omitempty"`
   141  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   142  	PageSize *int `json:"PageSize,omitempty"`
   143  	// Max number of records to return.
   144  	Limit *int `json:"limit,omitempty"`
   145  }
   146  
   147  func (params *ListIpCommandParams) SetSim(Sim string) *ListIpCommandParams {
   148  	params.Sim = &Sim
   149  	return params
   150  }
   151  func (params *ListIpCommandParams) SetSimIccid(SimIccid string) *ListIpCommandParams {
   152  	params.SimIccid = &SimIccid
   153  	return params
   154  }
   155  func (params *ListIpCommandParams) SetStatus(Status string) *ListIpCommandParams {
   156  	params.Status = &Status
   157  	return params
   158  }
   159  func (params *ListIpCommandParams) SetDirection(Direction string) *ListIpCommandParams {
   160  	params.Direction = &Direction
   161  	return params
   162  }
   163  func (params *ListIpCommandParams) SetPageSize(PageSize int) *ListIpCommandParams {
   164  	params.PageSize = &PageSize
   165  	return params
   166  }
   167  func (params *ListIpCommandParams) SetLimit(Limit int) *ListIpCommandParams {
   168  	params.Limit = &Limit
   169  	return params
   170  }
   171  
   172  // Retrieve a single page of IpCommand records from the API. Request is executed immediately.
   173  func (c *ApiService) PageIpCommand(params *ListIpCommandParams, pageToken, pageNumber string) (*ListIpCommandResponse, error) {
   174  	path := "/v1/IpCommands"
   175  
   176  	data := url.Values{}
   177  	headers := make(map[string]interface{})
   178  
   179  	if params != nil && params.Sim != nil {
   180  		data.Set("Sim", *params.Sim)
   181  	}
   182  	if params != nil && params.SimIccid != nil {
   183  		data.Set("SimIccid", *params.SimIccid)
   184  	}
   185  	if params != nil && params.Status != nil {
   186  		data.Set("Status", *params.Status)
   187  	}
   188  	if params != nil && params.Direction != nil {
   189  		data.Set("Direction", *params.Direction)
   190  	}
   191  	if params != nil && params.PageSize != nil {
   192  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   193  	}
   194  
   195  	if pageToken != "" {
   196  		data.Set("PageToken", pageToken)
   197  	}
   198  	if pageNumber != "" {
   199  		data.Set("Page", pageNumber)
   200  	}
   201  
   202  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   203  	if err != nil {
   204  		return nil, err
   205  	}
   206  
   207  	defer resp.Body.Close()
   208  
   209  	ps := &ListIpCommandResponse{}
   210  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   211  		return nil, err
   212  	}
   213  
   214  	return ps, err
   215  }
   216  
   217  // Lists IpCommand records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   218  func (c *ApiService) ListIpCommand(params *ListIpCommandParams) ([]SupersimV1IpCommand, error) {
   219  	response, errors := c.StreamIpCommand(params)
   220  
   221  	records := make([]SupersimV1IpCommand, 0)
   222  	for record := range response {
   223  		records = append(records, record)
   224  	}
   225  
   226  	if err := <-errors; err != nil {
   227  		return nil, err
   228  	}
   229  
   230  	return records, nil
   231  }
   232  
   233  // Streams IpCommand records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   234  func (c *ApiService) StreamIpCommand(params *ListIpCommandParams) (chan SupersimV1IpCommand, chan error) {
   235  	if params == nil {
   236  		params = &ListIpCommandParams{}
   237  	}
   238  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   239  
   240  	recordChannel := make(chan SupersimV1IpCommand, 1)
   241  	errorChannel := make(chan error, 1)
   242  
   243  	response, err := c.PageIpCommand(params, "", "")
   244  	if err != nil {
   245  		errorChannel <- err
   246  		close(recordChannel)
   247  		close(errorChannel)
   248  	} else {
   249  		go c.streamIpCommand(response, params, recordChannel, errorChannel)
   250  	}
   251  
   252  	return recordChannel, errorChannel
   253  }
   254  
   255  func (c *ApiService) streamIpCommand(response *ListIpCommandResponse, params *ListIpCommandParams, recordChannel chan SupersimV1IpCommand, errorChannel chan error) {
   256  	curRecord := 1
   257  
   258  	for response != nil {
   259  		responseRecords := response.IpCommands
   260  		for item := range responseRecords {
   261  			recordChannel <- responseRecords[item]
   262  			curRecord += 1
   263  			if params.Limit != nil && *params.Limit < curRecord {
   264  				close(recordChannel)
   265  				close(errorChannel)
   266  				return
   267  			}
   268  		}
   269  
   270  		record, err := client.GetNext(c.baseURL, response, c.getNextListIpCommandResponse)
   271  		if err != nil {
   272  			errorChannel <- err
   273  			break
   274  		} else if record == nil {
   275  			break
   276  		}
   277  
   278  		response = record.(*ListIpCommandResponse)
   279  	}
   280  
   281  	close(recordChannel)
   282  	close(errorChannel)
   283  }
   284  
   285  func (c *ApiService) getNextListIpCommandResponse(nextPageUrl string) (interface{}, error) {
   286  	if nextPageUrl == "" {
   287  		return nil, nil
   288  	}
   289  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   290  	if err != nil {
   291  		return nil, err
   292  	}
   293  
   294  	defer resp.Body.Close()
   295  
   296  	ps := &ListIpCommandResponse{}
   297  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   298  		return nil, err
   299  	}
   300  	return ps, nil
   301  }