/*
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 DeviceConnectDeviceAPIAPI interface {

	/*
	DeviceConnectConnect Connect the device and make it available to the server.

	Calling /connect will upgrade the connection to a persistent websocket connection and make the device available to the management API. The device must provide DeviceJWT identity either as Authorization (Bearer) header or as a cookie named 'JWT'.

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

	// DeviceConnectConnectExecute executes the request
	DeviceConnectConnectExecute(r ApiDeviceConnectConnectRequest) (*http.Response, error)
}

// DeviceConnectDeviceAPIAPIService DeviceConnectDeviceAPIAPI service
type DeviceConnectDeviceAPIAPIService service

type ApiDeviceConnectConnectRequest struct {
	ctx context.Context
	ApiService DeviceConnectDeviceAPIAPI
	connection *string
	upgrade *string
	secWebsocketKey *string
	secWebsocketVersion *int32
}

// Standard websocket request header.
func (r ApiDeviceConnectConnectRequest) Connection(connection string) ApiDeviceConnectConnectRequest {
	r.connection = &connection
	return r
}

// Standard websocket request header.
func (r ApiDeviceConnectConnectRequest) Upgrade(upgrade string) ApiDeviceConnectConnectRequest {
	r.upgrade = &upgrade
	return r
}

// Standard websocket request header.
func (r ApiDeviceConnectConnectRequest) SecWebsocketKey(secWebsocketKey string) ApiDeviceConnectConnectRequest {
	r.secWebsocketKey = &secWebsocketKey
	return r
}

// Standard websocket request header.
func (r ApiDeviceConnectConnectRequest) SecWebsocketVersion(secWebsocketVersion int32) ApiDeviceConnectConnectRequest {
	r.secWebsocketVersion = &secWebsocketVersion
	return r
}

func (r ApiDeviceConnectConnectRequest) Execute() (*http.Response, error) {
	return r.ApiService.DeviceConnectConnectExecute(r)
}

/*
DeviceConnectConnect Connect the device and make it available to the server.

Calling /connect will upgrade the connection to a persistent websocket connection and make the device available to the management API. The device must provide DeviceJWT identity either as Authorization (Bearer) header or as a cookie named 'JWT'.

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

// Execute executes the request
func (a *DeviceConnectDeviceAPIAPIService) DeviceConnectConnectExecute(r ApiDeviceConnectConnectRequest) (*http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
	)

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

	localVarPath := localBasePath + "/api/devices/v1/deviceconnect/connect"

	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
	}
	if r.connection != nil {
		parameterAddToHeaderOrQuery(localVarHeaderParams, "Connection", r.connection, "simple", "")
	}
	if r.upgrade != nil {
		parameterAddToHeaderOrQuery(localVarHeaderParams, "Upgrade", r.upgrade, "simple", "")
	}
	if r.secWebsocketKey != nil {
		parameterAddToHeaderOrQuery(localVarHeaderParams, "Sec-Websocket-Key", r.secWebsocketKey, "simple", "")
	}
	if r.secWebsocketVersion != nil {
		parameterAddToHeaderOrQuery(localVarHeaderParams, "Sec-Websocket-Version", r.secWebsocketVersion, "simple", "")
	}
	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 == 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
}
