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

	/*
	GetStatisticsInternal Get inventory statistics

	The following statistics are currently supported:
* Number of accepted and pending devices in each device tier


	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param tenantId Tenant ID.
	@return ApiGetStatisticsInternalRequest
	*/
	GetStatisticsInternal(ctx context.Context, tenantId string) ApiGetStatisticsInternalRequest

	// GetStatisticsInternalExecute executes the request
	//  @return GetStatisticsInternal200Response
	GetStatisticsInternalExecute(r ApiGetStatisticsInternalRequest) (*GetStatisticsInternal200Response, *http.Response, error)

	/*
	InventoryInternalV2SearchDeviceInventories Search device inventories based on attributes

	Returns a paged collection of devices and their attributes.

It accepts optional filters and sort parameters as body parameters.


	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param tenantId Tenant ID.
	@return ApiInventoryInternalV2SearchDeviceInventoriesRequest
	*/
	InventoryInternalV2SearchDeviceInventories(ctx context.Context, tenantId string) ApiInventoryInternalV2SearchDeviceInventoriesRequest

	// InventoryInternalV2SearchDeviceInventoriesExecute executes the request
	//  @return []DeviceInventoryResponse
	InventoryInternalV2SearchDeviceInventoriesExecute(r ApiInventoryInternalV2SearchDeviceInventoriesRequest) ([]DeviceInventoryResponse, *http.Response, error)
}

// DeviceInventoryFiltersAndSearchInternalAPIAPIService DeviceInventoryFiltersAndSearchInternalAPIAPI service
type DeviceInventoryFiltersAndSearchInternalAPIAPIService service

type ApiGetStatisticsInternalRequest struct {
	ctx context.Context
	ApiService DeviceInventoryFiltersAndSearchInternalAPIAPI
	tenantId string
}

func (r ApiGetStatisticsInternalRequest) Execute() (*GetStatisticsInternal200Response, *http.Response, error) {
	return r.ApiService.GetStatisticsInternalExecute(r)
}

/*
GetStatisticsInternal Get inventory statistics

The following statistics are currently supported:
* Number of accepted and pending devices in each device tier


 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param tenantId Tenant ID.
 @return ApiGetStatisticsInternalRequest
*/
func (a *DeviceInventoryFiltersAndSearchInternalAPIAPIService) GetStatisticsInternal(ctx context.Context, tenantId string) ApiGetStatisticsInternalRequest {
	return ApiGetStatisticsInternalRequest{
		ApiService: a,
		ctx: ctx,
		tenantId: tenantId,
	}
}

// Execute executes the request
//  @return GetStatisticsInternal200Response
func (a *DeviceInventoryFiltersAndSearchInternalAPIAPIService) GetStatisticsInternalExecute(r ApiGetStatisticsInternalRequest) (*GetStatisticsInternal200Response, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *GetStatisticsInternal200Response
	)

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

	localVarPath := localBasePath + "/api/internal/v2/inventory/tenants/{tenant_id}/statistics"
	localVarPath = strings.Replace(localVarPath, "{"+"tenant_id"+"}", url.PathEscape(parameterValueToString(r.tenantId, "tenantId")), -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 a.client.cfg.ResponseMiddleware != nil {
		err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, localVarBody)
		if err != nil {
			return localVarReturnValue, localVarHTTPResponse, err
		}
	}

	if localVarHTTPResponse.StatusCode >= 300 {
		newErr := &GenericOpenAPIError{
			body:  localVarBody,
			error: localVarHTTPResponse.Status,
		}
		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 ApiInventoryInternalV2SearchDeviceInventoriesRequest struct {
	ctx context.Context
	ApiService DeviceInventoryFiltersAndSearchInternalAPIAPI
	tenantId string
	searchParams *SearchParams
}

// The search and sort parameters of the filter
func (r ApiInventoryInternalV2SearchDeviceInventoriesRequest) SearchParams(searchParams SearchParams) ApiInventoryInternalV2SearchDeviceInventoriesRequest {
	r.searchParams = &searchParams
	return r
}

func (r ApiInventoryInternalV2SearchDeviceInventoriesRequest) Execute() ([]DeviceInventoryResponse, *http.Response, error) {
	return r.ApiService.InventoryInternalV2SearchDeviceInventoriesExecute(r)
}

/*
InventoryInternalV2SearchDeviceInventories Search device inventories based on attributes

Returns a paged collection of devices and their attributes.

It accepts optional filters and sort parameters as body parameters.


 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param tenantId Tenant ID.
 @return ApiInventoryInternalV2SearchDeviceInventoriesRequest
*/
func (a *DeviceInventoryFiltersAndSearchInternalAPIAPIService) InventoryInternalV2SearchDeviceInventories(ctx context.Context, tenantId string) ApiInventoryInternalV2SearchDeviceInventoriesRequest {
	return ApiInventoryInternalV2SearchDeviceInventoriesRequest{
		ApiService: a,
		ctx: ctx,
		tenantId: tenantId,
	}
}

// Execute executes the request
//  @return []DeviceInventoryResponse
func (a *DeviceInventoryFiltersAndSearchInternalAPIAPIService) InventoryInternalV2SearchDeviceInventoriesExecute(r ApiInventoryInternalV2SearchDeviceInventoriesRequest) ([]DeviceInventoryResponse, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodPost
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  []DeviceInventoryResponse
	)

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

	localVarPath := localBasePath + "/api/internal/v2/inventory/tenants/{tenant_id}/filters/search"
	localVarPath = strings.Replace(localVarPath, "{"+"tenant_id"+"}", url.PathEscape(parameterValueToString(r.tenantId, "tenantId")), -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.searchParams
	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 a.client.cfg.ResponseMiddleware != nil {
		err = a.client.cfg.ResponseMiddleware(localVarHTTPResponse, 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 == 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
}
