github.com/twilio/twilio-go@v1.20.1/rest/microvisor/v1/devices.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Microvisor
     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  // Fetch a specific Device.
    27  func (c *ApiService) FetchDevice(Sid string) (*MicrovisorV1Device, error) {
    28  	path := "/v1/Devices/{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.Get(c.baseURL+path, data, headers)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	defer resp.Body.Close()
    40  
    41  	ps := &MicrovisorV1Device{}
    42  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	return ps, err
    47  }
    48  
    49  // Optional parameters for the method 'ListDevice'
    50  type ListDeviceParams struct {
    51  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
    52  	PageSize *int `json:"PageSize,omitempty"`
    53  	// Max number of records to return.
    54  	Limit *int `json:"limit,omitempty"`
    55  }
    56  
    57  func (params *ListDeviceParams) SetPageSize(PageSize int) *ListDeviceParams {
    58  	params.PageSize = &PageSize
    59  	return params
    60  }
    61  func (params *ListDeviceParams) SetLimit(Limit int) *ListDeviceParams {
    62  	params.Limit = &Limit
    63  	return params
    64  }
    65  
    66  // Retrieve a single page of Device records from the API. Request is executed immediately.
    67  func (c *ApiService) PageDevice(params *ListDeviceParams, pageToken, pageNumber string) (*ListDeviceResponse, error) {
    68  	path := "/v1/Devices"
    69  
    70  	data := url.Values{}
    71  	headers := make(map[string]interface{})
    72  
    73  	if params != nil && params.PageSize != nil {
    74  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
    75  	}
    76  
    77  	if pageToken != "" {
    78  		data.Set("PageToken", pageToken)
    79  	}
    80  	if pageNumber != "" {
    81  		data.Set("Page", pageNumber)
    82  	}
    83  
    84  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	defer resp.Body.Close()
    90  
    91  	ps := &ListDeviceResponse{}
    92  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
    93  		return nil, err
    94  	}
    95  
    96  	return ps, err
    97  }
    98  
    99  // Lists Device records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   100  func (c *ApiService) ListDevice(params *ListDeviceParams) ([]MicrovisorV1Device, error) {
   101  	response, errors := c.StreamDevice(params)
   102  
   103  	records := make([]MicrovisorV1Device, 0)
   104  	for record := range response {
   105  		records = append(records, record)
   106  	}
   107  
   108  	if err := <-errors; err != nil {
   109  		return nil, err
   110  	}
   111  
   112  	return records, nil
   113  }
   114  
   115  // Streams Device records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   116  func (c *ApiService) StreamDevice(params *ListDeviceParams) (chan MicrovisorV1Device, chan error) {
   117  	if params == nil {
   118  		params = &ListDeviceParams{}
   119  	}
   120  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   121  
   122  	recordChannel := make(chan MicrovisorV1Device, 1)
   123  	errorChannel := make(chan error, 1)
   124  
   125  	response, err := c.PageDevice(params, "", "")
   126  	if err != nil {
   127  		errorChannel <- err
   128  		close(recordChannel)
   129  		close(errorChannel)
   130  	} else {
   131  		go c.streamDevice(response, params, recordChannel, errorChannel)
   132  	}
   133  
   134  	return recordChannel, errorChannel
   135  }
   136  
   137  func (c *ApiService) streamDevice(response *ListDeviceResponse, params *ListDeviceParams, recordChannel chan MicrovisorV1Device, errorChannel chan error) {
   138  	curRecord := 1
   139  
   140  	for response != nil {
   141  		responseRecords := response.Devices
   142  		for item := range responseRecords {
   143  			recordChannel <- responseRecords[item]
   144  			curRecord += 1
   145  			if params.Limit != nil && *params.Limit < curRecord {
   146  				close(recordChannel)
   147  				close(errorChannel)
   148  				return
   149  			}
   150  		}
   151  
   152  		record, err := client.GetNext(c.baseURL, response, c.getNextListDeviceResponse)
   153  		if err != nil {
   154  			errorChannel <- err
   155  			break
   156  		} else if record == nil {
   157  			break
   158  		}
   159  
   160  		response = record.(*ListDeviceResponse)
   161  	}
   162  
   163  	close(recordChannel)
   164  	close(errorChannel)
   165  }
   166  
   167  func (c *ApiService) getNextListDeviceResponse(nextPageUrl string) (interface{}, error) {
   168  	if nextPageUrl == "" {
   169  		return nil, nil
   170  	}
   171  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  
   176  	defer resp.Body.Close()
   177  
   178  	ps := &ListDeviceResponse{}
   179  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   180  		return nil, err
   181  	}
   182  	return ps, nil
   183  }
   184  
   185  // Optional parameters for the method 'UpdateDevice'
   186  type UpdateDeviceParams struct {
   187  	// A unique and addressable name to be assigned to this Device by the developer. It may be used in place of the Device SID.
   188  	UniqueName *string `json:"UniqueName,omitempty"`
   189  	// The SID or unique name of the App to be targeted to the Device.
   190  	TargetApp *string `json:"TargetApp,omitempty"`
   191  	// A Boolean flag specifying whether to enable application logging. Logs will be enabled or extended for 24 hours.
   192  	LoggingEnabled *bool `json:"LoggingEnabled,omitempty"`
   193  	// Set to true to restart the App running on the Device.
   194  	RestartApp *bool `json:"RestartApp,omitempty"`
   195  }
   196  
   197  func (params *UpdateDeviceParams) SetUniqueName(UniqueName string) *UpdateDeviceParams {
   198  	params.UniqueName = &UniqueName
   199  	return params
   200  }
   201  func (params *UpdateDeviceParams) SetTargetApp(TargetApp string) *UpdateDeviceParams {
   202  	params.TargetApp = &TargetApp
   203  	return params
   204  }
   205  func (params *UpdateDeviceParams) SetLoggingEnabled(LoggingEnabled bool) *UpdateDeviceParams {
   206  	params.LoggingEnabled = &LoggingEnabled
   207  	return params
   208  }
   209  func (params *UpdateDeviceParams) SetRestartApp(RestartApp bool) *UpdateDeviceParams {
   210  	params.RestartApp = &RestartApp
   211  	return params
   212  }
   213  
   214  // Update a specific Device.
   215  func (c *ApiService) UpdateDevice(Sid string, params *UpdateDeviceParams) (*MicrovisorV1Device, error) {
   216  	path := "/v1/Devices/{Sid}"
   217  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   218  
   219  	data := url.Values{}
   220  	headers := make(map[string]interface{})
   221  
   222  	if params != nil && params.UniqueName != nil {
   223  		data.Set("UniqueName", *params.UniqueName)
   224  	}
   225  	if params != nil && params.TargetApp != nil {
   226  		data.Set("TargetApp", *params.TargetApp)
   227  	}
   228  	if params != nil && params.LoggingEnabled != nil {
   229  		data.Set("LoggingEnabled", fmt.Sprint(*params.LoggingEnabled))
   230  	}
   231  	if params != nil && params.RestartApp != nil {
   232  		data.Set("RestartApp", fmt.Sprint(*params.RestartApp))
   233  	}
   234  
   235  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	defer resp.Body.Close()
   241  
   242  	ps := &MicrovisorV1Device{}
   243  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   244  		return nil, err
   245  	}
   246  
   247  	return ps, err
   248  }