/*
Mender API

Combined API specification for the features of the different Mender backend services, suitable for code generation applications 

API version: 1
Contact: support@mender.io
*/

// Code generated by OpenAPI Generator (https://openapi-generator.tech); DO NOT EDIT.

package client

import (
	"bytes"
	"context"
	"io"
	"net/http"
	"net/url"
)


type DeviceInventoryDeviceAPIAPI interface {

	/*
	AssignAttributes Assign a set of attributes for a device

	Saves the provided attribute set for the authenticated device.
The device ID is retrieved from the authorization header.

This method has upsert semantics:

* the values of existing attributes are overwritten

* attributes assigned for the first time are automatically created


	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@return ApiAssignAttributesRequest
	*/
	AssignAttributes(ctx context.Context) ApiAssignAttributesRequest

	// AssignAttributesExecute executes the request
	AssignAttributesExecute(r ApiAssignAttributesRequest) (*http.Response, error)

	/*
	ReplaceAttributes Replace the set of attributes for a device

	Replaces the attribute set for the authenticated device with the provided one.
The device ID is retrieved from the authorization header.

This method replaces all the attributes with the new set:

* attributes not provided in the set are removed from the db

* the values of existing attributes are overwritten

* attributes assigned for the first time are automatically created


	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@return ApiReplaceAttributesRequest
	*/
	ReplaceAttributes(ctx context.Context) ApiReplaceAttributesRequest

	// ReplaceAttributesExecute executes the request
	ReplaceAttributesExecute(r ApiReplaceAttributesRequest) (*http.Response, error)
}

// DeviceInventoryDeviceAPIAPIService DeviceInventoryDeviceAPIAPI service
type DeviceInventoryDeviceAPIAPIService service

type ApiAssignAttributesRequest struct {
	ctx context.Context
	ApiService DeviceInventoryDeviceAPIAPI
	deviceAttributeRequest *[]DeviceAttributeRequest
}

// A list of attribute descriptors.
func (r ApiAssignAttributesRequest) DeviceAttributeRequest(deviceAttributeRequest []DeviceAttributeRequest) ApiAssignAttributesRequest {
	r.deviceAttributeRequest = &deviceAttributeRequest
	return r
}

func (r ApiAssignAttributesRequest) Execute() (*http.Response, error) {
	return r.ApiService.AssignAttributesExecute(r)
}

/*
AssignAttributes Assign a set of attributes for a device

Saves the provided attribute set for the authenticated device.
The device ID is retrieved from the authorization header.

This method has upsert semantics:

* the values of existing attributes are overwritten

* attributes assigned for the first time are automatically created


 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @return ApiAssignAttributesRequest
*/
func (a *DeviceInventoryDeviceAPIAPIService) AssignAttributes(ctx context.Context) ApiAssignAttributesRequest {
	return ApiAssignAttributesRequest{
		ApiService: a,
		ctx: ctx,
	}
}

// Execute executes the request
func (a *DeviceInventoryDeviceAPIAPIService) AssignAttributesExecute(r ApiAssignAttributesRequest) (*http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodPatch
		localVarPostBody     interface{}
		formFiles            []formFile
	)

	localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DeviceInventoryDeviceAPIAPIService.AssignAttributes")
	if err != nil {
		return nil, &GenericOpenAPIError{error: err.Error()}
	}

	localVarPath := localBasePath + "/api/devices/v1/inventory/device/attributes"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}
	if r.deviceAttributeRequest == nil {
		return nil, reportError("deviceAttributeRequest is required and must be specified")
	}

	// to determine the Content-Type header
	localVarHTTPContentTypes := []string{"application/json"}

	// set Content-Type header
	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
	if localVarHTTPContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
	}

	// to determine the Accept header
	localVarHTTPHeaderAccepts := []string{"application/json"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	// body params
	localVarPostBody = r.deviceAttributeRequest
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return nil, err
	}

	localVarHTTPResponse, err := a.client.callAPI(req)
	if err != nil || localVarHTTPResponse == nil {
		return localVarHTTPResponse, err
	}

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarHTTPResponse, err
	}

	if a.client.cfg.ResponseMiddleware != nil {
		err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, localVarBody)
		if err != nil {
			return localVarHTTPResponse, err
		}
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		if localVarHTTPResponse.StatusCode == 400 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 401 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 500 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
		}
		return localVarHTTPResponse, newErr
	}

	return localVarHTTPResponse, nil
}

type ApiReplaceAttributesRequest struct {
	ctx context.Context
	ApiService DeviceInventoryDeviceAPIAPI
	deviceAttributeRequest *[]DeviceAttributeRequest
}

// A list of attribute descriptors.
func (r ApiReplaceAttributesRequest) DeviceAttributeRequest(deviceAttributeRequest []DeviceAttributeRequest) ApiReplaceAttributesRequest {
	r.deviceAttributeRequest = &deviceAttributeRequest
	return r
}

func (r ApiReplaceAttributesRequest) Execute() (*http.Response, error) {
	return r.ApiService.ReplaceAttributesExecute(r)
}

/*
ReplaceAttributes Replace the set of attributes for a device

Replaces the attribute set for the authenticated device with the provided one.
The device ID is retrieved from the authorization header.

This method replaces all the attributes with the new set:

* attributes not provided in the set are removed from the db

* the values of existing attributes are overwritten

* attributes assigned for the first time are automatically created


 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @return ApiReplaceAttributesRequest
*/
func (a *DeviceInventoryDeviceAPIAPIService) ReplaceAttributes(ctx context.Context) ApiReplaceAttributesRequest {
	return ApiReplaceAttributesRequest{
		ApiService: a,
		ctx: ctx,
	}
}

// Execute executes the request
func (a *DeviceInventoryDeviceAPIAPIService) ReplaceAttributesExecute(r ApiReplaceAttributesRequest) (*http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodPut
		localVarPostBody     interface{}
		formFiles            []formFile
	)

	localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DeviceInventoryDeviceAPIAPIService.ReplaceAttributes")
	if err != nil {
		return nil, &GenericOpenAPIError{error: err.Error()}
	}

	localVarPath := localBasePath + "/api/devices/v1/inventory/device/attributes"

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}
	if r.deviceAttributeRequest == nil {
		return nil, reportError("deviceAttributeRequest is required and must be specified")
	}

	// to determine the Content-Type header
	localVarHTTPContentTypes := []string{"application/json"}

	// set Content-Type header
	localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
	if localVarHTTPContentType != "" {
		localVarHeaderParams["Content-Type"] = localVarHTTPContentType
	}

	// to determine the Accept header
	localVarHTTPHeaderAccepts := []string{"application/json"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	// body params
	localVarPostBody = r.deviceAttributeRequest
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return nil, err
	}

	localVarHTTPResponse, err := a.client.callAPI(req)
	if err != nil || localVarHTTPResponse == nil {
		return localVarHTTPResponse, err
	}

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarHTTPResponse, err
	}

	if a.client.cfg.ResponseMiddleware != nil {
		err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, localVarBody)
		if err != nil {
			return localVarHTTPResponse, err
		}
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		if localVarHTTPResponse.StatusCode == 400 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 401 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 500 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
		}
		return localVarHTTPResponse, newErr
	}

	return localVarHTTPResponse, nil
}
