/*
CoinAPI Market Data REST API

No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)

API version: v1
Contact: support@apibricks.io
*/

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

package openapi

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


// OrderBookL3APIService OrderBookL3API service
type OrderBookL3APIService service

type ApiV1Orderbooks3CurrentGetRequest struct {
	ctx context.Context
	ApiService *OrderBookL3APIService
	filterSymbolId *string
	limitLevels *int32
}

// Comma or semicolon delimited parts of symbol identifier used to filter the response.
func (r ApiV1Orderbooks3CurrentGetRequest) FilterSymbolId(filterSymbolId string) ApiV1Orderbooks3CurrentGetRequest {
	r.filterSymbolId = &filterSymbolId
	return r
}

// The maximum number of levels to include in the response.
func (r ApiV1Orderbooks3CurrentGetRequest) LimitLevels(limitLevels int32) ApiV1Orderbooks3CurrentGetRequest {
	r.limitLevels = &limitLevels
	return r
}

func (r ApiV1Orderbooks3CurrentGetRequest) Execute() ([]V1OrderBookBase, *http.Response, error) {
	return r.ApiService.V1Orderbooks3CurrentGetExecute(r)
}

/*
V1Orderbooks3CurrentGet Current order books

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

// Execute executes the request
//  @return []V1OrderBookBase
func (a *OrderBookL3APIService) V1Orderbooks3CurrentGetExecute(r ApiV1Orderbooks3CurrentGetRequest) ([]V1OrderBookBase, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  []V1OrderBookBase
	)

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

	localVarPath := localBasePath + "/v1/orderbooks3/current"

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

	if r.filterSymbolId != nil {
		parameterAddToHeaderOrQuery(localVarQueryParams, "filter_symbol_id", r.filterSymbolId, "form", "")
	}
	if r.limitLevels != nil {
		parameterAddToHeaderOrQuery(localVarQueryParams, "limit_levels", r.limitLevels, "form", "")
	}
	// 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{"text/plain", "application/json", "text/json", "application/x-msgpack"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	if r.ctx != nil {
		// API Key Authentication
		if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
			if apiKey, ok := auth["APIKey"]; ok {
				var key string
				if apiKey.Prefix != "" {
					key = apiKey.Prefix + " " + apiKey.Key
				} else {
					key = apiKey.Key
				}
				localVarHeaderParams["Authorization"] = key
			}
		}
	}
	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 ApiV1Orderbooks3SymbolIdCurrentGetRequest struct {
	ctx context.Context
	ApiService *OrderBookL3APIService
	symbolId string
	limitLevels *int32
}

// The maximum number of levels to include in the response.
func (r ApiV1Orderbooks3SymbolIdCurrentGetRequest) LimitLevels(limitLevels int32) ApiV1Orderbooks3SymbolIdCurrentGetRequest {
	r.limitLevels = &limitLevels
	return r
}

func (r ApiV1Orderbooks3SymbolIdCurrentGetRequest) Execute() (*V1OrderBookBase, *http.Response, error) {
	return r.ApiService.V1Orderbooks3SymbolIdCurrentGetExecute(r)
}

/*
V1Orderbooks3SymbolIdCurrentGet Current order book by symbol_id

Retrieves the current order book for the specified symbol.

 @param ctx context.Context - for authentication, logging, cancellation, deadlines, tracing, etc. Passed from http.Request or context.Background().
 @param symbolId The symbol ID (from the Metadata -> Symbols)
 @return ApiV1Orderbooks3SymbolIdCurrentGetRequest
*/
func (a *OrderBookL3APIService) V1Orderbooks3SymbolIdCurrentGet(ctx context.Context, symbolId string) ApiV1Orderbooks3SymbolIdCurrentGetRequest {
	return ApiV1Orderbooks3SymbolIdCurrentGetRequest{
		ApiService: a,
		ctx: ctx,
		symbolId: symbolId,
	}
}

// Execute executes the request
//  @return V1OrderBookBase
func (a *OrderBookL3APIService) V1Orderbooks3SymbolIdCurrentGetExecute(r ApiV1Orderbooks3SymbolIdCurrentGetRequest) (*V1OrderBookBase, *http.Response, error) {
	var (
		localVarHTTPMethod   = http.MethodGet
		localVarPostBody     interface{}
		formFiles            []formFile
		localVarReturnValue  *V1OrderBookBase
	)

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

	localVarPath := localBasePath + "/v1/orderbooks3/{symbol_id}/current"
	localVarPath = strings.Replace(localVarPath, "{"+"symbol_id"+"}", url.PathEscape(parameterValueToString(r.symbolId, "symbolId")), -1)

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

	if r.limitLevels != nil {
		parameterAddToHeaderOrQuery(localVarQueryParams, "limit_levels", r.limitLevels, "form", "")
	}
	// 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{"text/plain", "application/json", "text/json", "application/x-msgpack"}

	// set Accept header
	localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
	if localVarHTTPHeaderAccept != "" {
		localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
	}
	if r.ctx != nil {
		// API Key Authentication
		if auth, ok := r.ctx.Value(ContextAPIKeys).(map[string]APIKey); ok {
			if apiKey, ok := auth["APIKey"]; ok {
				var key string
				if apiKey.Prefix != "" {
					key = apiKey.Prefix + " " + apiKey.Key
				} else {
					key = apiKey.Key
				}
				localVarHeaderParams["Authorization"] = key
			}
		}
	}
	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
}
