<?php

/**
 * FlexibleRateApi
 * PHP version 8.1.
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 */

/**
 * Binance Crypto Loan REST API.
 *
 * OpenAPI Specification for the Binance Crypto Loan REST API
 *
 * The version of the OpenAPI document: 1.0.0
 * Generated by: https://openapi-generator.tech
 * Generator version: 7.12.0
 */

/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */

namespace Binance\Client\CryptoLoan\Api;

use Binance\Client\CryptoLoan\Model\CheckCollateralRepayRateResponse;
use Binance\Client\CryptoLoan\Model\FlexibleLoanAdjustLtvRequest;
use Binance\Client\CryptoLoan\Model\FlexibleLoanAdjustLtvResponse;
use Binance\Client\CryptoLoan\Model\FlexibleLoanBorrowRequest;
use Binance\Client\CryptoLoan\Model\FlexibleLoanBorrowResponse;
use Binance\Client\CryptoLoan\Model\FlexibleLoanRepayRequest;
use Binance\Client\CryptoLoan\Model\FlexibleLoanRepayResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanAssetsDataResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanBorrowHistoryResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanCollateralAssetsDataResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanLiquidationHistoryResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanLtvAdjustmentHistoryResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanOngoingOrdersResponse;
use Binance\Client\CryptoLoan\Model\GetFlexibleLoanRepaymentHistoryResponse;
use Binance\Common\ApiException;
use Binance\Common\Auth\SignerFactory;
use Binance\Common\Auth\SignerInterface;
use Binance\Common\CommonUtils;
use Binance\Common\Configuration\ClientConfiguration;
use Binance\Common\Dtos\ApiResponse;
use Binance\Common\HeaderSelector;
use Binance\Common\HttpClient;
use Binance\Common\ObjectSerializer;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Utils;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * FlexibleRateApi Class Doc Comment.
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 */
class FlexibleRateApi
{
    /** @var string[] */
    public const contentTypes = [
        'checkCollateralRepayRate' => ['application/x-www-form-urlencoded'],
        'flexibleLoanAdjustLtv' => ['application/x-www-form-urlencoded'],
        'flexibleLoanBorrow' => ['application/x-www-form-urlencoded'],
        'flexibleLoanRepay' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanAssetsData' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanBorrowHistory' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanCollateralAssetsData' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanLiquidationHistory' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanLtvAdjustmentHistory' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanOngoingOrders' => ['application/x-www-form-urlencoded'],
        'getFlexibleLoanRepaymentHistory' => ['application/x-www-form-urlencoded'],
    ];
    private const HAS_TIME_UNIT = false;

    /**
     * @var ClientInterface
     */
    protected $client;

    /**
     * @var HeaderSelector
     */
    protected $headerSelector;

    /**
     * Summary of clientConfig.
     *
     * @var ClientConfiguration
     */
    protected $clientConfig;

    /**
     * Summary of signer.
     *
     * @var SignerInterface
     */
    protected $signer;

    /**
     * userAgent for HTTP requests.
     *
     * @var string
     */
    private $userAgent;

    public function __construct(
        ?ClientConfiguration $clientConfig = new ClientConfiguration(),
        ?ClientInterface $client = null,
        ?HeaderSelector $selector = null,
    ) {
        $this->clientConfig = $clientConfig;
        $this->client = $client ?: new HttpClient($clientConfig);
        $this->headerSelector = $selector ?: new HeaderSelector();
        if (!empty($clientConfig->getSignatureConfiguration())) {
            $this->signer = SignerFactory::getSigner($clientConfig->getSignatureConfiguration());
        }
        $this->userAgent = CommonUtils::getUserAgent('crypto-loan');
    }

    /**
     * Operation checkCollateralRepayRate.
     *
     * Check Collateral Repay Rate (USER_DATA)
     *
     * @param string   $loanCoin       loanCoin (required)
     * @param string   $collateralCoin collateralCoin (required)
     * @param null|int $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<CheckCollateralRepayRateResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function checkCollateralRepayRate($loanCoin, $collateralCoin, $recvWindow = null): ApiResponse
    {
        return $this->checkCollateralRepayRateWithHttpInfo($loanCoin, $collateralCoin, $recvWindow);
    }

    /**
     * Operation checkCollateralRepayRateWithHttpInfo.
     *
     * Check Collateral Repay Rate (USER_DATA)
     *
     * @param string   $loanCoin       (required)
     * @param string   $collateralCoin (required)
     * @param null|int $recvWindow     (optional)
     *
     * @return ApiResponse<CheckCollateralRepayRateResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function checkCollateralRepayRateWithHttpInfo($loanCoin, $collateralCoin, $recvWindow = null): ApiResponse
    {
        $request = $this->checkCollateralRepayRateRequest($loanCoin, $collateralCoin, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\CheckCollateralRepayRateResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\CheckCollateralRepayRateResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\CheckCollateralRepayRateResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'checkCollateralRepayRate'.
     *
     * @param string   $loanCoin       (required)
     * @param string   $collateralCoin (required)
     * @param null|int $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function checkCollateralRepayRateRequest($loanCoin, $collateralCoin, $recvWindow = null)
    {
        $contentType = self::contentTypes['checkCollateralRepayRate'][0];

        // verify the required parameter 'loanCoin' is set
        if (null === $loanCoin || (is_array($loanCoin) && 0 === count($loanCoin))) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $loanCoin when calling checkCollateralRepayRate'
            );
        }

        // verify the required parameter 'collateralCoin' is set
        if (null === $collateralCoin || (is_array($collateralCoin) && 0 === count($collateralCoin))) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $collateralCoin when calling checkCollateralRepayRate'
            );
        }

        $resourcePath = '/sapi/v2/loan/flexible/repay/rate';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            true // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            true // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation flexibleLoanAdjustLtv.
     *
     * Flexible Loan Adjust LTV(TRADE)
     *
     * @param FlexibleLoanAdjustLtvRequest $flexibleLoanAdjustLtvRequest flexibleLoanAdjustLtvRequest (required)
     *
     * @return ApiResponse<FlexibleLoanAdjustLtvResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanAdjustLtv($flexibleLoanAdjustLtvRequest): ApiResponse
    {
        return $this->flexibleLoanAdjustLtvWithHttpInfo($flexibleLoanAdjustLtvRequest);
    }

    /**
     * Operation flexibleLoanAdjustLtvWithHttpInfo.
     *
     * Flexible Loan Adjust LTV(TRADE)
     *
     * @param FlexibleLoanAdjustLtvRequest $flexibleLoanAdjustLtvRequest (required)
     *
     * @return ApiResponse<FlexibleLoanAdjustLtvResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanAdjustLtvWithHttpInfo($flexibleLoanAdjustLtvRequest): ApiResponse
    {
        $request = $this->flexibleLoanAdjustLtvRequest($flexibleLoanAdjustLtvRequest);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanAdjustLtvResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\FlexibleLoanAdjustLtvResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanAdjustLtvResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'flexibleLoanAdjustLtv'.
     *
     * @param FlexibleLoanAdjustLtvRequest $flexibleLoanAdjustLtvRequest (required)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanAdjustLtvRequest($flexibleLoanAdjustLtvRequest)
    {
        $contentType = self::contentTypes['flexibleLoanAdjustLtv'][0];

        // verify the required parameter 'flexibleLoanAdjustLtvRequest' is set
        if (null === $flexibleLoanAdjustLtvRequest || (is_array($flexibleLoanAdjustLtvRequest) && 0 === count($flexibleLoanAdjustLtvRequest))) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $flexibleLoanAdjustLtvRequest when calling flexibleLoanAdjustLtv'
            );
        }

        $resourcePath = '/sapi/v2/loan/flexible/adjust/ltv';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $flexibleLoanAdjustLtvRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $flexibleLoanAdjustLtvRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $flexibleLoanAdjustLtvRequest->{$getter}();
            }
        }

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        // for model (json/xml)
        if (count($formParams) > 0) {
            if ($multipart) {
                $multipartContents = [];
                foreach ($formParams as $formParamName => $formParamValue) {
                    $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
                    foreach ($formParamValueItems as $formParamValueItem) {
                        $multipartContents[] = [
                            'name' => $formParamName,
                            'contents' => $formParamValueItem,
                        ];
                    }
                }
                // for HTTP post (form)
                $httpBody = new MultipartStream($multipartContents);
            } elseif (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the form parameters
                $httpBody = Utils::jsonEncode($formParams);
            } else {
                // for HTTP post (form)
                $httpBody = ObjectSerializer::buildQuery($formParams);
            }
        } elseif (isset($flexibleLoanAdjustLtvRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($flexibleLoanAdjustLtvRequest));
            } else {
                $httpBody = $flexibleLoanAdjustLtvRequest;
            }
        }

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'POST',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation flexibleLoanBorrow.
     *
     * Flexible Loan Borrow(TRADE)
     *
     * @param FlexibleLoanBorrowRequest $flexibleLoanBorrowRequest flexibleLoanBorrowRequest (required)
     *
     * @return ApiResponse<FlexibleLoanBorrowResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanBorrow($flexibleLoanBorrowRequest): ApiResponse
    {
        return $this->flexibleLoanBorrowWithHttpInfo($flexibleLoanBorrowRequest);
    }

    /**
     * Operation flexibleLoanBorrowWithHttpInfo.
     *
     * Flexible Loan Borrow(TRADE)
     *
     * @param FlexibleLoanBorrowRequest $flexibleLoanBorrowRequest (required)
     *
     * @return ApiResponse<FlexibleLoanBorrowResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanBorrowWithHttpInfo($flexibleLoanBorrowRequest): ApiResponse
    {
        $request = $this->flexibleLoanBorrowRequest($flexibleLoanBorrowRequest);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanBorrowResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\FlexibleLoanBorrowResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanBorrowResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'flexibleLoanBorrow'.
     *
     * @param FlexibleLoanBorrowRequest $flexibleLoanBorrowRequest (required)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanBorrowRequest($flexibleLoanBorrowRequest)
    {
        $contentType = self::contentTypes['flexibleLoanBorrow'][0];

        // verify the required parameter 'flexibleLoanBorrowRequest' is set
        if (null === $flexibleLoanBorrowRequest || (is_array($flexibleLoanBorrowRequest) && 0 === count($flexibleLoanBorrowRequest))) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $flexibleLoanBorrowRequest when calling flexibleLoanBorrow'
            );
        }

        $resourcePath = '/sapi/v2/loan/flexible/borrow';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $flexibleLoanBorrowRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $flexibleLoanBorrowRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $flexibleLoanBorrowRequest->{$getter}();
            }
        }

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        // for model (json/xml)
        if (count($formParams) > 0) {
            if ($multipart) {
                $multipartContents = [];
                foreach ($formParams as $formParamName => $formParamValue) {
                    $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
                    foreach ($formParamValueItems as $formParamValueItem) {
                        $multipartContents[] = [
                            'name' => $formParamName,
                            'contents' => $formParamValueItem,
                        ];
                    }
                }
                // for HTTP post (form)
                $httpBody = new MultipartStream($multipartContents);
            } elseif (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the form parameters
                $httpBody = Utils::jsonEncode($formParams);
            } else {
                // for HTTP post (form)
                $httpBody = ObjectSerializer::buildQuery($formParams);
            }
        } elseif (isset($flexibleLoanBorrowRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($flexibleLoanBorrowRequest));
            } else {
                $httpBody = $flexibleLoanBorrowRequest;
            }
        }

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'POST',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation flexibleLoanRepay.
     *
     * Flexible Loan Repay(TRADE)
     *
     * @param FlexibleLoanRepayRequest $flexibleLoanRepayRequest flexibleLoanRepayRequest (required)
     *
     * @return ApiResponse<FlexibleLoanRepayResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanRepay($flexibleLoanRepayRequest): ApiResponse
    {
        return $this->flexibleLoanRepayWithHttpInfo($flexibleLoanRepayRequest);
    }

    /**
     * Operation flexibleLoanRepayWithHttpInfo.
     *
     * Flexible Loan Repay(TRADE)
     *
     * @param FlexibleLoanRepayRequest $flexibleLoanRepayRequest (required)
     *
     * @return ApiResponse<FlexibleLoanRepayResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanRepayWithHttpInfo($flexibleLoanRepayRequest): ApiResponse
    {
        $request = $this->flexibleLoanRepayRequest($flexibleLoanRepayRequest);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanRepayResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\FlexibleLoanRepayResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\FlexibleLoanRepayResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'flexibleLoanRepay'.
     *
     * @param FlexibleLoanRepayRequest $flexibleLoanRepayRequest (required)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function flexibleLoanRepayRequest($flexibleLoanRepayRequest)
    {
        $contentType = self::contentTypes['flexibleLoanRepay'][0];

        // verify the required parameter 'flexibleLoanRepayRequest' is set
        if (null === $flexibleLoanRepayRequest || (is_array($flexibleLoanRepayRequest) && 0 === count($flexibleLoanRepayRequest))) {
            throw new \InvalidArgumentException(
                'Missing the required parameter $flexibleLoanRepayRequest when calling flexibleLoanRepay'
            );
        }

        $resourcePath = '/sapi/v2/loan/flexible/repay';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $flexibleLoanRepayRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $flexibleLoanRepayRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $flexibleLoanRepayRequest->{$getter}();
            }
        }

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        // for model (json/xml)
        if (count($formParams) > 0) {
            if ($multipart) {
                $multipartContents = [];
                foreach ($formParams as $formParamName => $formParamValue) {
                    $formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
                    foreach ($formParamValueItems as $formParamValueItem) {
                        $multipartContents[] = [
                            'name' => $formParamName,
                            'contents' => $formParamValueItem,
                        ];
                    }
                }
                // for HTTP post (form)
                $httpBody = new MultipartStream($multipartContents);
            } elseif (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the form parameters
                $httpBody = Utils::jsonEncode($formParams);
            } else {
                // for HTTP post (form)
                $httpBody = ObjectSerializer::buildQuery($formParams);
            }
        } elseif (isset($flexibleLoanRepayRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($flexibleLoanRepayRequest));
            } else {
                $httpBody = $flexibleLoanRepayRequest;
            }
        }

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'POST',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanAssetsData.
     *
     * Get Flexible Loan Assets Data(USER_DATA)
     *
     * @param null|string $loanCoin   loanCoin (optional)
     * @param null|int    $recvWindow recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanAssetsDataResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanAssetsData($loanCoin = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanAssetsDataWithHttpInfo($loanCoin, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanAssetsDataWithHttpInfo.
     *
     * Get Flexible Loan Assets Data(USER_DATA)
     *
     * @param null|string $loanCoin   (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanAssetsDataResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanAssetsDataWithHttpInfo($loanCoin = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanAssetsDataRequest($loanCoin, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanAssetsDataResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanAssetsDataResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanAssetsDataResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanAssetsData'.
     *
     * @param null|string $loanCoin   (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanAssetsDataRequest($loanCoin = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanAssetsData'][0];

        $resourcePath = '/sapi/v2/loan/flexible/loanable/data';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanBorrowHistory.
     *
     * Get Flexible Loan Borrow History(USER_DATA)
     *
     * @param null|string $loanCoin       loanCoin (optional)
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $startTime      startTime (optional)
     * @param null|int    $endTime        endTime (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanBorrowHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanBorrowHistory($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanBorrowHistoryWithHttpInfo($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanBorrowHistoryWithHttpInfo.
     *
     * Get Flexible Loan Borrow History(USER_DATA)
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanBorrowHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanBorrowHistoryWithHttpInfo($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanBorrowHistoryRequest($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanBorrowHistoryResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanBorrowHistoryResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanBorrowHistoryResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanBorrowHistory'.
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanBorrowHistoryRequest($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanBorrowHistory'][0];

        $resourcePath = '/sapi/v2/loan/flexible/borrow/history';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $startTime,
            'startTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $endTime,
            'endTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $current,
            'current', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $limit,
            'limit', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanCollateralAssetsData.
     *
     * Get Flexible Loan Collateral Assets Data(USER_DATA)
     *
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanCollateralAssetsDataResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanCollateralAssetsData($collateralCoin = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanCollateralAssetsDataWithHttpInfo($collateralCoin, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanCollateralAssetsDataWithHttpInfo.
     *
     * Get Flexible Loan Collateral Assets Data(USER_DATA)
     *
     * @param null|string $collateralCoin (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanCollateralAssetsDataResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanCollateralAssetsDataWithHttpInfo($collateralCoin = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanCollateralAssetsDataRequest($collateralCoin, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanCollateralAssetsDataResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanCollateralAssetsDataResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanCollateralAssetsDataResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanCollateralAssetsData'.
     *
     * @param null|string $collateralCoin (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanCollateralAssetsDataRequest($collateralCoin = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanCollateralAssetsData'][0];

        $resourcePath = '/sapi/v2/loan/flexible/collateral/data';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanLiquidationHistory.
     *
     * Get Flexible Loan Liquidation History (USER_DATA)
     *
     * @param null|string $loanCoin       loanCoin (optional)
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $startTime      startTime (optional)
     * @param null|int    $endTime        endTime (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanLiquidationHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLiquidationHistory($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanLiquidationHistoryWithHttpInfo($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanLiquidationHistoryWithHttpInfo.
     *
     * Get Flexible Loan Liquidation History (USER_DATA)
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanLiquidationHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLiquidationHistoryWithHttpInfo($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanLiquidationHistoryRequest($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLiquidationHistoryResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLiquidationHistoryResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLiquidationHistoryResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanLiquidationHistory'.
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLiquidationHistoryRequest($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanLiquidationHistory'][0];

        $resourcePath = '/sapi/v2/loan/flexible/liquidation/history';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $startTime,
            'startTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $endTime,
            'endTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $current,
            'current', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $limit,
            'limit', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanLtvAdjustmentHistory.
     *
     * Get Flexible Loan LTV Adjustment History(USER_DATA)
     *
     * @param null|string $loanCoin       loanCoin (optional)
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $startTime      startTime (optional)
     * @param null|int    $endTime        endTime (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanLtvAdjustmentHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLtvAdjustmentHistory($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanLtvAdjustmentHistoryWithHttpInfo($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanLtvAdjustmentHistoryWithHttpInfo.
     *
     * Get Flexible Loan LTV Adjustment History(USER_DATA)
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanLtvAdjustmentHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLtvAdjustmentHistoryWithHttpInfo($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanLtvAdjustmentHistoryRequest($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLtvAdjustmentHistoryResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLtvAdjustmentHistoryResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanLtvAdjustmentHistoryResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanLtvAdjustmentHistory'.
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanLtvAdjustmentHistoryRequest($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanLtvAdjustmentHistory'][0];

        $resourcePath = '/sapi/v2/loan/flexible/ltv/adjustment/history';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $startTime,
            'startTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $endTime,
            'endTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $current,
            'current', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $limit,
            'limit', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanOngoingOrders.
     *
     * Get Flexible Loan Ongoing Orders(USER_DATA)
     *
     * @param null|string $loanCoin       loanCoin (optional)
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanOngoingOrdersResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanOngoingOrders($loanCoin = null, $collateralCoin = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanOngoingOrdersWithHttpInfo($loanCoin, $collateralCoin, $current, $limit, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanOngoingOrdersWithHttpInfo.
     *
     * Get Flexible Loan Ongoing Orders(USER_DATA)
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanOngoingOrdersResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanOngoingOrdersWithHttpInfo($loanCoin = null, $collateralCoin = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanOngoingOrdersRequest($loanCoin, $collateralCoin, $current, $limit, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanOngoingOrdersResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanOngoingOrdersResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanOngoingOrdersResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanOngoingOrders'.
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanOngoingOrdersRequest($loanCoin = null, $collateralCoin = null, $current = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanOngoingOrders'][0];

        $resourcePath = '/sapi/v2/loan/flexible/ongoing/orders';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $current,
            'current', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $limit,
            'limit', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Operation getFlexibleLoanRepaymentHistory.
     *
     * Get Flexible Loan Repayment History(USER_DATA)
     *
     * @param null|string $loanCoin       loanCoin (optional)
     * @param null|string $collateralCoin collateralCoin (optional)
     * @param null|int    $startTime      startTime (optional)
     * @param null|int    $endTime        endTime (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     recvWindow (optional)
     *
     * @return ApiResponse<GetFlexibleLoanRepaymentHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanRepaymentHistory($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->getFlexibleLoanRepaymentHistoryWithHttpInfo($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);
    }

    /**
     * Operation getFlexibleLoanRepaymentHistoryWithHttpInfo.
     *
     * Get Flexible Loan Repayment History(USER_DATA)
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return ApiResponse<GetFlexibleLoanRepaymentHistoryResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanRepaymentHistoryWithHttpInfo($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getFlexibleLoanRepaymentHistoryRequest($loanCoin, $collateralCoin, $startTime, $endTime, $current, $limit, $recvWindow);

        try {
            try {
                $response = $this->client->send($request, []);
            } catch (RequestException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    $e->getResponse() ? $e->getResponse()->getHeaders() : null,
                    $e->getResponse() ? (string) $e->getResponse()->getBody() : null
                );
            } catch (ConnectException $e) {
                throw new ApiException(
                    "[{$e->getCode()}] {$e->getMessage()}",
                    (int) $e->getCode(),
                    null,
                    null
                );
            }

            $statusCode = $response->getStatusCode();

            switch ($statusCode) {
                case 200:
                    return $this->handleResponseWithDataType(
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanRepaymentHistoryResponse',
                        $request,
                        $response,
                    );
            }

            if ($statusCode < 200 || $statusCode > 299) {
                throw new ApiException(
                    sprintf(
                        '[%d] Error connecting to the API (%s)',
                        $statusCode,
                        (string) $request->getUri()
                    ),
                    $statusCode,
                    $response->getHeaders(),
                    (string) $response->getBody()
                );
            }

            return $this->handleResponseWithDataType(
                '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanRepaymentHistoryResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\CryptoLoan\Model\GetFlexibleLoanRepaymentHistoryResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFlexibleLoanRepaymentHistory'.
     *
     * @param null|string $loanCoin       (optional)
     * @param null|string $collateralCoin (optional)
     * @param null|int    $startTime      (optional)
     * @param null|int    $endTime        (optional)
     * @param null|int    $current        Current querying page. Start from 1; default: 1; max: 1000 (optional)
     * @param null|int    $limit          Default: 10; max: 100 (optional)
     * @param null|int    $recvWindow     (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFlexibleLoanRepaymentHistoryRequest($loanCoin = null, $collateralCoin = null, $startTime = null, $endTime = null, $current = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFlexibleLoanRepaymentHistory'][0];

        $resourcePath = '/sapi/v2/loan/flexible/repay/history';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $loanCoin,
            'loanCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $collateralCoin,
            'collateralCoin', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $startTime,
            'startTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $endTime,
            'endTime', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $current,
            'current', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $limit,
            'limit', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $recvWindow,
            'recvWindow', // param base name
            'integer', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);

        $headers = $this->headerSelector->selectHeaders(
            ['application/json'],
            $contentType,
            $multipart
        );

        $defaultHeaders = [];
        $defaultHeaders['User-Agent'] = $this->userAgent;

        if (self::HAS_TIME_UNIT && !empty($this->clientConfig->getTimeUnit())) {
            $defaultHeaders['X-MBX-TIME-UNIT'] = $this->clientConfig->getTimeUnit();
        }

        $headers = array_merge(
            $defaultHeaders,
            $headerParams,
            $headers
        );

        $operationHost = $this->clientConfig->getUrl();

        $queryParams['timestamp'] = $this->getTimestamp();
        $query = ObjectSerializer::buildQuery($queryParams);
        $queryParams['signature'] = $this->signer->sign($query.$httpBody);
        $headers['X-MBX-APIKEY'] = $this->clientConfig->getSignatureConfiguration()->getApiKey();
        $query = ObjectSerializer::buildQuery($queryParams);

        return new Request(
            'GET',
            $operationHost.$resourcePath.($query ? "?{$query}" : ''),
            $headers,
            $httpBody
        );
    }

    /**
     * Summary of getTimestamp.
     */
    public function getTimestamp(): string
    {
        return date_create()->format('Uv');
    }

    private function handleResponseWithDataType(
        string $dataType,
        RequestInterface $request,
        ResponseInterface $response
    ): ApiResponse {
        if ('\SplFileObject' === $dataType) {
            $content = $response->getBody(); // stream goes to serializer
        } else {
            $content = (string) $response->getBody();
            if ('string' !== $dataType) {
                try {
                    $content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
                } catch (\JsonException $exception) {
                    throw new ApiException(
                        sprintf(
                            'Error JSON decoding server response (%s)',
                            $request->getUri()
                        ),
                        $response->getStatusCode(),
                        $response->getHeaders(),
                        $content
                    );
                }
            }
        }

        $rateLimits = CommonUtils::getRateLimits($response->getStatusCode(), $response->getHeaders());

        return new ApiResponse(
            $response->getStatusCode(),
            $response->getHeaders(),
            ObjectSerializer::deserialize($content, $dataType, []),
            $rateLimits
        );
    }

    private function responseWithinRangeCode(
        string $rangeCode,
        int $statusCode
    ): bool {
        $left = (int) ($rangeCode[0].'00');
        $right = (int) ($rangeCode[0].'99');

        return $statusCode >= $left && $statusCode <= $right;
    }
}
