github.com/optim-corp/cios-golang-sdk@v0.5.1/sdk/service/device/management.go (about)

     1  package srvdevice
     2  
     3  import (
     4  	_nethttp "net/http"
     5  
     6  	ciosctx "github.com/optim-corp/cios-golang-sdk/ctx"
     7  
     8  	cnv "github.com/fcfcqloow/go-advance/convert"
     9  
    10  	"github.com/optim-corp/cios-golang-sdk/util"
    11  
    12  	xmath "github.com/fcfcqloow/go-advance/math"
    13  
    14  	"github.com/optim-corp/cios-golang-sdk/cios"
    15  )
    16  
    17  func MakeGetDevicesOpts() cios.ApiGetDevicesRequest {
    18  	return cios.ApiGetDevicesRequest{}
    19  }
    20  
    21  func (self *CiosDeviceManagement) GetDevices(ctx ciosctx.RequestCtx, params cios.ApiGetDevicesRequest) (response cios.MultipleDevice, httpResponse *_nethttp.Response, err error) {
    22  	if err := self.refresh(); err != nil {
    23  		return cios.MultipleDevice{}, nil, err
    24  	}
    25  	params.ApiService = self.ApiClient.DeviceApi
    26  	params.Ctx = self.withHost(ctx)
    27  	params.P_order = util.ToNil(params.P_order)
    28  	params.P_orderBy = util.ToNil(params.P_orderBy)
    29  	params.P_name = util.ToNil(params.P_name)
    30  	params.P_definitionId = util.ToNil(params.P_definitionId)
    31  	params.P_definitionTag = util.ToNil(params.P_definitionTag)
    32  	params.P_idNumber = util.ToNil(params.P_idNumber)
    33  	params.P_resourceOwnerId = util.ToNil(params.P_resourceOwnerId)
    34  	params.P_isPublic = util.ToNil(params.P_isPublic)
    35  	params.P_lang = util.ToNil(params.P_lang)
    36  	return params.Execute()
    37  }
    38  func (self *CiosDeviceManagement) GetDevicesAll(ctx ciosctx.RequestCtx, params cios.ApiGetDevicesRequest) ([]cios.Device, *_nethttp.Response, error) {
    39  	var (
    40  		result       []cios.Device
    41  		httpResponse *_nethttp.Response
    42  		err          error
    43  		_limit       = int64(1000)
    44  		offset       = int64(0)
    45  		getFunction  = func(offset int64) (cios.MultipleDevice, *_nethttp.Response, error) {
    46  			return self.GetDevices(ctx, params.Limit(xmath.MinInt64(_limit, 1000)).Offset(offset+cnv.MustInt64(params.P_offset)))
    47  		}
    48  	)
    49  	if params.P_limit != nil {
    50  		_limit = *params.P_limit
    51  		for {
    52  			res, httpRes, err := getFunction(offset)
    53  			if err != nil {
    54  				return nil, httpRes, err
    55  			}
    56  			result = append(result, res.Devices...)
    57  			offset += 1000
    58  			_limit -= 1000
    59  			if _limit <= 0 {
    60  				break
    61  			}
    62  		}
    63  	} else {
    64  		res, httpRes, err := getFunction(offset)
    65  		if err != nil {
    66  			return nil, httpRes, err
    67  		}
    68  		result = append(result, res.Devices...)
    69  		for offset = int64(1000); offset+cnv.MustInt64(params.P_offset) < res.Total; offset += 1000 {
    70  			res, httpRes, err = getFunction(offset)
    71  			if err != nil {
    72  				return nil, httpRes, err
    73  			}
    74  			result = append(result, res.Devices...)
    75  		}
    76  	}
    77  	return result, httpResponse, err
    78  }
    79  func (self *CiosDeviceManagement) GetDevicesUnlimited(ctx ciosctx.RequestCtx, params cios.ApiGetDevicesRequest) ([]cios.Device, *_nethttp.Response, error) {
    80  	params.P_limit = nil
    81  	return self.GetDevicesAll(ctx, params)
    82  }
    83  func (self *CiosDeviceManagement) GetDevice(ctx ciosctx.RequestCtx, deviceID string, lang *string, isDev *bool) (cios.Device, *_nethttp.Response, error) {
    84  	if err := self.refresh(); err != nil {
    85  		return cios.Device{}, nil, err
    86  	}
    87  	request := self.ApiClient.DeviceApi.GetDevice(self.withHost(ctx), deviceID)
    88  	if lang != nil {
    89  		request = request.Lang(*lang)
    90  	}
    91  	if isDev != nil {
    92  		request = request.IsDev(*isDev)
    93  	}
    94  	response, httpResponse, err := request.Execute()
    95  	if err != nil {
    96  		return cios.Device{}, httpResponse, err
    97  	}
    98  	return response.Device, httpResponse, err
    99  }
   100  func (self *CiosDeviceManagement) GetDeviceInventory(ctx ciosctx.RequestCtx, deviceID string) (map[string]interface{}, *_nethttp.Response, error) {
   101  	if err := self.refresh(); err != nil {
   102  		return nil, nil, err
   103  	}
   104  	return self.ApiClient.DeviceApi.GetDeviceInventoryLatest(ctx, deviceID).Execute()
   105  }
   106  func (self *CiosDeviceManagement) DeleteDevice(ctx ciosctx.RequestCtx, id string) (*_nethttp.Response, error) {
   107  	if err := self.refresh(); err != nil {
   108  		return nil, err
   109  	}
   110  	return self.ApiClient.DeviceApi.DeleteDevice(self.withHost(ctx), id).Execute()
   111  }
   112  func (self *CiosDeviceManagement) CreateDevice(ctx ciosctx.RequestCtx, body cios.DeviceInfo) (cios.Device, *_nethttp.Response, error) {
   113  	if err := self.refresh(); err != nil {
   114  		return cios.Device{}, nil, err
   115  	}
   116  	response, httpResponse, err := self.ApiClient.DeviceApi.CreateDevice(self.withHost(ctx)).DeviceInfo(body).Execute()
   117  	if err != nil {
   118  		return cios.Device{}, httpResponse, err
   119  	}
   120  	return response.Device, httpResponse, err
   121  }
   122  func (self *CiosDeviceManagement) UpdateDevice(ctx ciosctx.RequestCtx, deviceId string, body cios.DeviceUpdateRequest) (cios.Device, *_nethttp.Response, error) {
   123  	if err := self.refresh(); err != nil {
   124  		return cios.Device{}, nil, err
   125  	}
   126  	response, httpResponse, err := self.ApiClient.DeviceApi.UpdateDevice(self.withHost(ctx), deviceId).DeviceUpdateRequest(body).Execute()
   127  	if err != nil {
   128  		return cios.Device{}, httpResponse, err
   129  	}
   130  	return response.Device, httpResponse, err
   131  }