/*
Daytona Toolbox API

Daytona Toolbox API

API version: v0.0.0-dev
*/

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

package toolbox

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


type PortAPI interface {

	/*
	GetPorts Get active ports

	Get a list of all currently active ports

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

	// GetPortsExecute executes the request
	//  @return PortList
	GetPortsExecute(r PortAPIGetPortsRequest) (*PortList, *http.Response, error)

	/*
	IsPortInUse Check if port is in use

	Check if a specific port is currently in use

	@param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
	@param port Port number (3000-9999)
	@return PortAPIIsPortInUseRequest
	*/
	IsPortInUse(ctx context.Context, port int32) PortAPIIsPortInUseRequest

	// IsPortInUseExecute executes the request
	//  @return IsPortInUseResponse
	IsPortInUseExecute(r PortAPIIsPortInUseRequest) (*IsPortInUseResponse, *http.Response, error)
}

// PortAPIService PortAPI service
type PortAPIService service

type PortAPIGetPortsRequest struct {
	ctx context.Context
	ApiService PortAPI
}

func (r PortAPIGetPortsRequest) Execute() (*PortList, *http.Response, error) {
	return r.ApiService.GetPortsExecute(r)
}

/*
GetPorts Get active ports

Get a list of all currently active ports

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

// Execute executes the request
//  @return PortList
func (a *PortAPIService) GetPortsExecute(r PortAPIGetPortsRequest) (*PortList, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *PortList
	)

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

	localVarPath := localBasePath + "/port"

	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,
		}
		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 PortAPIIsPortInUseRequest struct {
	ctx context.Context
	ApiService PortAPI
	port int32
}

func (r PortAPIIsPortInUseRequest) Execute() (*IsPortInUseResponse, *http.Response, error) {
	return r.ApiService.IsPortInUseExecute(r)
}

/*
IsPortInUse Check if port is in use

Check if a specific port is currently in use

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param port Port number (3000-9999)
 @return PortAPIIsPortInUseRequest
*/
func (a *PortAPIService) IsPortInUse(ctx context.Context, port int32) PortAPIIsPortInUseRequest {
	return PortAPIIsPortInUseRequest{
		ApiService: a,
		ctx: ctx,
		port: port,
	}
}

// Execute executes the request
//  @return IsPortInUseResponse
func (a *PortAPIService) IsPortInUseExecute(r PortAPIIsPortInUseRequest) (*IsPortInUseResponse, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *IsPortInUseResponse
	)

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

	localVarPath := localBasePath + "/port/{port}/in-use"
	localVarPath = strings.Replace(localVarPath, "{"+"port"+"}", url.PathEscape(parameterValueToString(r.port, "port")), -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,
		}
		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
}
