/*
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"
	"strings"
)


type DeviceConfigureManagementAPIAPI interface {

	/*
	DeviceConfigManagementDeployDeviceConfiguration Deploy the device's configuration

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param deviceId ID of the device.
	@return ApiDeviceConfigManagementDeployDeviceConfigurationRequest
	*/
	DeviceConfigManagementDeployDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementDeployDeviceConfigurationRequest

	// DeviceConfigManagementDeployDeviceConfigurationExecute executes the request
	//  @return NewConfigurationDeploymentResponse
	DeviceConfigManagementDeployDeviceConfigurationExecute(r ApiDeviceConfigManagementDeployDeviceConfigurationRequest) (*NewConfigurationDeploymentResponse, *http.Response, error)

	/*
	DeviceConfigManagementGetDeviceConfiguration Get the device's configuration

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param deviceId ID of the device to query.
	@return ApiDeviceConfigManagementGetDeviceConfigurationRequest
	*/
	DeviceConfigManagementGetDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementGetDeviceConfigurationRequest

	// DeviceConfigManagementGetDeviceConfigurationExecute executes the request
	//  @return DeviceConfiguration
	DeviceConfigManagementGetDeviceConfigurationExecute(r ApiDeviceConfigManagementGetDeviceConfigurationRequest) (*DeviceConfiguration, *http.Response, error)

	/*
	DeviceConfigManagementSetDeviceConfiguration Set the device's configuration

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param deviceId ID of the device to query.
	@return ApiDeviceConfigManagementSetDeviceConfigurationRequest
	*/
	DeviceConfigManagementSetDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementSetDeviceConfigurationRequest

	// DeviceConfigManagementSetDeviceConfigurationExecute executes the request
	DeviceConfigManagementSetDeviceConfigurationExecute(r ApiDeviceConfigManagementSetDeviceConfigurationRequest) (*http.Response, error)
}

// DeviceConfigureManagementAPIAPIService DeviceConfigureManagementAPIAPI service
type DeviceConfigureManagementAPIAPIService service

type ApiDeviceConfigManagementDeployDeviceConfigurationRequest struct {
	ctx context.Context
	ApiService DeviceConfigureManagementAPIAPI
	deviceId string
	newConfigurationDeployment *NewConfigurationDeployment
}

func (r ApiDeviceConfigManagementDeployDeviceConfigurationRequest) NewConfigurationDeployment(newConfigurationDeployment NewConfigurationDeployment) ApiDeviceConfigManagementDeployDeviceConfigurationRequest {
	r.newConfigurationDeployment = &newConfigurationDeployment
	return r
}

func (r ApiDeviceConfigManagementDeployDeviceConfigurationRequest) Execute() (*NewConfigurationDeploymentResponse, *http.Response, error) {
	return r.ApiService.DeviceConfigManagementDeployDeviceConfigurationExecute(r)
}

/*
DeviceConfigManagementDeployDeviceConfiguration Deploy the device's configuration

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param deviceId ID of the device.
 @return ApiDeviceConfigManagementDeployDeviceConfigurationRequest
*/
func (a *DeviceConfigureManagementAPIAPIService) DeviceConfigManagementDeployDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementDeployDeviceConfigurationRequest {
	return ApiDeviceConfigManagementDeployDeviceConfigurationRequest{
		ApiService: a,
		ctx: ctx,
		deviceId: deviceId,
	}
}

// Execute executes the request
//  @return NewConfigurationDeploymentResponse
func (a *DeviceConfigureManagementAPIAPIService) DeviceConfigManagementDeployDeviceConfigurationExecute(r ApiDeviceConfigManagementDeployDeviceConfigurationRequest) (*NewConfigurationDeploymentResponse, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodPost
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *NewConfigurationDeploymentResponse
	)

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

	localVarPath := localBasePath + "/api/management/v1/deviceconfig/configurations/device/{deviceId}/deploy"
	localVarPath = strings.Replace(localVarPath, "{"+"deviceId"+"}", url.PathEscape(parameterValueToString(r.deviceId, "deviceId")), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}
	if r.newConfigurationDeployment == nil {
		return localVarReturnValue, nil, reportError("newConfigurationDeployment 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.newConfigurationDeployment
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return localVarReturnValue, nil, err
	}

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

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarReturnValue, 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 localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarReturnValue, localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 404 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarReturnValue, 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 localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
	if err != nil {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: err.Error(),
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	return localVarReturnValue, localVarHTTPResponse, nil
}

type ApiDeviceConfigManagementGetDeviceConfigurationRequest struct {
	ctx context.Context
	ApiService DeviceConfigureManagementAPIAPI
	deviceId string
}

func (r ApiDeviceConfigManagementGetDeviceConfigurationRequest) Execute() (*DeviceConfiguration, *http.Response, error) {
	return r.ApiService.DeviceConfigManagementGetDeviceConfigurationExecute(r)
}

/*
DeviceConfigManagementGetDeviceConfiguration Get the device's configuration

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param deviceId ID of the device to query.
 @return ApiDeviceConfigManagementGetDeviceConfigurationRequest
*/
func (a *DeviceConfigureManagementAPIAPIService) DeviceConfigManagementGetDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementGetDeviceConfigurationRequest {
	return ApiDeviceConfigManagementGetDeviceConfigurationRequest{
		ApiService: a,
		ctx: ctx,
		deviceId: deviceId,
	}
}

// Execute executes the request
//  @return DeviceConfiguration
func (a *DeviceConfigureManagementAPIAPIService) DeviceConfigManagementGetDeviceConfigurationExecute(r ApiDeviceConfigManagementGetDeviceConfigurationRequest) (*DeviceConfiguration, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *DeviceConfiguration
	)

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

	localVarPath := localBasePath + "/api/management/v1/deviceconfig/configurations/device/{deviceId}"
	localVarPath = strings.Replace(localVarPath, "{"+"deviceId"+"}", url.PathEscape(parameterValueToString(r.deviceId, "deviceId")), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

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

	// 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
	}
	req, err := a.client.prepareRequest(r.ctx, localVarPath, localVarHTTPMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, formFiles)
	if err != nil {
		return localVarReturnValue, nil, err
	}

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

	localVarBody, err := io.ReadAll(localVarHTTPResponse.Body)
	localVarHTTPResponse.Body.Close()
	localVarHTTPResponse.Body = io.NopCloser(bytes.NewBuffer(localVarBody))
	if err != nil {
		return localVarReturnValue, 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 localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarReturnValue, localVarHTTPResponse, newErr
		}
		if localVarHTTPResponse.StatusCode == 404 {
			var v Error
			err = a.client.decode(&v, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
			if err != nil {
				newErr.error = err.Error()
				return localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
			return localVarReturnValue, 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 localVarReturnValue, localVarHTTPResponse, newErr
			}
					newErr.error = formatErrorMessage(localVarHTTPResponse.Status, &v)
					newErr.model = v
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	err = a.client.decode(&localVarReturnValue, localVarBody, localVarHTTPResponse.Header.Get("Content-Type"))
	if err != nil {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: err.Error(),
		}
		return localVarReturnValue, localVarHTTPResponse, newErr
	}

	return localVarReturnValue, localVarHTTPResponse, nil
}

type ApiDeviceConfigManagementSetDeviceConfigurationRequest struct {
	ctx context.Context
	ApiService DeviceConfigureManagementAPIAPI
	deviceId string
	requestBody *map[string]string
}

func (r ApiDeviceConfigManagementSetDeviceConfigurationRequest) RequestBody(requestBody map[string]string) ApiDeviceConfigManagementSetDeviceConfigurationRequest {
	r.requestBody = &requestBody
	return r
}

func (r ApiDeviceConfigManagementSetDeviceConfigurationRequest) Execute() (*http.Response, error) {
	return r.ApiService.DeviceConfigManagementSetDeviceConfigurationExecute(r)
}

/*
DeviceConfigManagementSetDeviceConfiguration Set the device's configuration

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param deviceId ID of the device to query.
 @return ApiDeviceConfigManagementSetDeviceConfigurationRequest
*/
func (a *DeviceConfigureManagementAPIAPIService) DeviceConfigManagementSetDeviceConfiguration(ctx context.Context, deviceId string) ApiDeviceConfigManagementSetDeviceConfigurationRequest {
	return ApiDeviceConfigManagementSetDeviceConfigurationRequest{
		ApiService: a,
		ctx: ctx,
		deviceId: deviceId,
	}
}

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

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

	localVarPath := localBasePath + "/api/management/v1/deviceconfig/configurations/device/{deviceId}"
	localVarPath = strings.Replace(localVarPath, "{"+"deviceId"+"}", url.PathEscape(parameterValueToString(r.deviceId, "deviceId")), -1)

	localVarHeaderParams := make(map[string]string)
	localVarQueryParams := url.Values{}
	localVarFormParams := url.Values{}

	// 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.requestBody
	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 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 == 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
}
