<?php

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

/**
 * Binance Sub Account REST API.
 *
 * OpenAPI Specification for the Binance Sub Account 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\SubAccount\Api;

use Binance\Client\SubAccount\Model\CreateAVirtualSubAccountRequest;
use Binance\Client\SubAccount\Model\CreateAVirtualSubAccountResponse;
use Binance\Client\SubAccount\Model\EnableFuturesForSubAccountRequest;
use Binance\Client\SubAccount\Model\EnableFuturesForSubAccountResponse;
use Binance\Client\SubAccount\Model\EnableOptionsForSubAccountRequest;
use Binance\Client\SubAccount\Model\EnableOptionsForSubAccountResponse;
use Binance\Client\SubAccount\Model\GetFuturesPositionRiskOfSubAccountResponse;
use Binance\Client\SubAccount\Model\GetFuturesPositionRiskOfSubAccountV2Response;
use Binance\Client\SubAccount\Model\GetSubAccountsStatusOnMarginOrFuturesResponse;
use Binance\Client\SubAccount\Model\QuerySubAccountListResponse;
use Binance\Client\SubAccount\Model\QuerySubAccountTransactionStatisticsResponse;
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;

/**
 * AccountManagementApi Class Doc Comment.
 *
 * @category Class
 *
 * @author   OpenAPI Generator team
 *
 * @see     https://openapi-generator.tech
 */
class AccountManagementApi
{
    /** @var string[] */
    public const contentTypes = [
        'createAVirtualSubAccount' => ['application/x-www-form-urlencoded'],
        'enableFuturesForSubAccount' => ['application/x-www-form-urlencoded'],
        'enableOptionsForSubAccount' => ['application/x-www-form-urlencoded'],
        'getFuturesPositionRiskOfSubAccount' => ['application/x-www-form-urlencoded'],
        'getFuturesPositionRiskOfSubAccountV2' => ['application/x-www-form-urlencoded'],
        'getSubAccountsStatusOnMarginOrFutures' => ['application/x-www-form-urlencoded'],
        'querySubAccountList' => ['application/x-www-form-urlencoded'],
        'querySubAccountTransactionStatistics' => ['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('sub-account');
    }

    /**
     * Operation createAVirtualSubAccount.
     *
     * Create a Virtual Sub-account (For Master Account) (USER_DATA)
     *
     * @param CreateAVirtualSubAccountRequest $createAVirtualSubAccountRequest createAVirtualSubAccountRequest (required)
     *
     * @return ApiResponse<CreateAVirtualSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function createAVirtualSubAccount($createAVirtualSubAccountRequest): ApiResponse
    {
        return $this->createAVirtualSubAccountWithHttpInfo($createAVirtualSubAccountRequest);
    }

    /**
     * Operation createAVirtualSubAccountWithHttpInfo.
     *
     * Create a Virtual Sub-account (For Master Account) (USER_DATA)
     *
     * @param CreateAVirtualSubAccountRequest $createAVirtualSubAccountRequest (required)
     *
     * @return ApiResponse<CreateAVirtualSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function createAVirtualSubAccountWithHttpInfo($createAVirtualSubAccountRequest): ApiResponse
    {
        $request = $this->createAVirtualSubAccountRequest($createAVirtualSubAccountRequest);

        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\SubAccount\Model\CreateAVirtualSubAccountResponse',
                        $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\SubAccount\Model\CreateAVirtualSubAccountResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\CreateAVirtualSubAccountResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

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

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

        $resourcePath = '/sapi/v1/sub-account/virtualSubAccount';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $createAVirtualSubAccountRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $createAVirtualSubAccountRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $createAVirtualSubAccountRequest->{$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($createAVirtualSubAccountRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($createAVirtualSubAccountRequest));
            } else {
                $httpBody = $createAVirtualSubAccountRequest;
            }
        }

        $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 enableFuturesForSubAccount.
     *
     * Enable Futures for Sub-account (For Master Account) (USER_DATA)
     *
     * @param EnableFuturesForSubAccountRequest $enableFuturesForSubAccountRequest enableFuturesForSubAccountRequest (required)
     *
     * @return ApiResponse<EnableFuturesForSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function enableFuturesForSubAccount($enableFuturesForSubAccountRequest): ApiResponse
    {
        return $this->enableFuturesForSubAccountWithHttpInfo($enableFuturesForSubAccountRequest);
    }

    /**
     * Operation enableFuturesForSubAccountWithHttpInfo.
     *
     * Enable Futures for Sub-account (For Master Account) (USER_DATA)
     *
     * @param EnableFuturesForSubAccountRequest $enableFuturesForSubAccountRequest (required)
     *
     * @return ApiResponse<EnableFuturesForSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function enableFuturesForSubAccountWithHttpInfo($enableFuturesForSubAccountRequest): ApiResponse
    {
        $request = $this->enableFuturesForSubAccountRequest($enableFuturesForSubAccountRequest);

        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\SubAccount\Model\EnableFuturesForSubAccountResponse',
                        $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\SubAccount\Model\EnableFuturesForSubAccountResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\EnableFuturesForSubAccountResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

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

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

        $resourcePath = '/sapi/v1/sub-account/futures/enable';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $enableFuturesForSubAccountRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $enableFuturesForSubAccountRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $enableFuturesForSubAccountRequest->{$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($enableFuturesForSubAccountRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($enableFuturesForSubAccountRequest));
            } else {
                $httpBody = $enableFuturesForSubAccountRequest;
            }
        }

        $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 enableOptionsForSubAccount.
     *
     * Enable Options for Sub-account (For Master Account) (USER_DATA)
     *
     * @param EnableOptionsForSubAccountRequest $enableOptionsForSubAccountRequest enableOptionsForSubAccountRequest (required)
     *
     * @return ApiResponse<EnableOptionsForSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function enableOptionsForSubAccount($enableOptionsForSubAccountRequest): ApiResponse
    {
        return $this->enableOptionsForSubAccountWithHttpInfo($enableOptionsForSubAccountRequest);
    }

    /**
     * Operation enableOptionsForSubAccountWithHttpInfo.
     *
     * Enable Options for Sub-account (For Master Account) (USER_DATA)
     *
     * @param EnableOptionsForSubAccountRequest $enableOptionsForSubAccountRequest (required)
     *
     * @return ApiResponse<EnableOptionsForSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function enableOptionsForSubAccountWithHttpInfo($enableOptionsForSubAccountRequest): ApiResponse
    {
        $request = $this->enableOptionsForSubAccountRequest($enableOptionsForSubAccountRequest);

        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\SubAccount\Model\EnableOptionsForSubAccountResponse',
                        $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\SubAccount\Model\EnableOptionsForSubAccountResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\EnableOptionsForSubAccountResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

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

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

        $resourcePath = '/sapi/v1/sub-account/eoptions/enable';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        $getters = $enableOptionsForSubAccountRequest::getters();
        $formParams = [];
        foreach ($getters as $property => $getter) {
            $value = $enableOptionsForSubAccountRequest->{$getter}();
            if (!empty($value)) {
                $formParams[$property] = $enableOptionsForSubAccountRequest->{$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($enableOptionsForSubAccountRequest)) {
            if (false !== stripos($headers['Content-Type'], 'application/json')) {
                // if Content-Type contains "application/json", json_encode the body
                $httpBody = Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($enableOptionsForSubAccountRequest));
            } else {
                $httpBody = $enableOptionsForSubAccountRequest;
            }
        }

        $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 getFuturesPositionRiskOfSubAccount.
     *
     * Get Futures Position-Risk of Sub-account (For Master Account) (USER_DATA)
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow recvWindow (optional)
     *
     * @return ApiResponse<GetFuturesPositionRiskOfSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccount($email, $recvWindow = null): ApiResponse
    {
        return $this->getFuturesPositionRiskOfSubAccountWithHttpInfo($email, $recvWindow);
    }

    /**
     * Operation getFuturesPositionRiskOfSubAccountWithHttpInfo.
     *
     * Get Futures Position-Risk of Sub-account (For Master Account) (USER_DATA)
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow (optional)
     *
     * @return ApiResponse<GetFuturesPositionRiskOfSubAccountResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccountWithHttpInfo($email, $recvWindow = null): ApiResponse
    {
        $request = $this->getFuturesPositionRiskOfSubAccountRequest($email, $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\SubAccount\Model\GetFuturesPositionRiskOfSubAccountResponse',
                        $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\SubAccount\Model\GetFuturesPositionRiskOfSubAccountResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\GetFuturesPositionRiskOfSubAccountResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFuturesPositionRiskOfSubAccount'.
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccountRequest($email, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFuturesPositionRiskOfSubAccount'][0];

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

        $resourcePath = '/sapi/v1/sub-account/futures/positionRisk';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $email,
            'email', // 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 getFuturesPositionRiskOfSubAccountV2.
     *
     * Get Futures Position-Risk of Sub-account V2 (For Master Account) (USER_DATA)
     *
     * @param string   $email       [Sub-account email](#email-address) (required)
     * @param int      $futuresType 1:USDT-margined Futures，2: Coin-margined Futures (required)
     * @param null|int $recvWindow  recvWindow (optional)
     *
     * @return ApiResponse<GetFuturesPositionRiskOfSubAccountV2Response>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccountV2($email, $futuresType, $recvWindow = null): ApiResponse
    {
        return $this->getFuturesPositionRiskOfSubAccountV2WithHttpInfo($email, $futuresType, $recvWindow);
    }

    /**
     * Operation getFuturesPositionRiskOfSubAccountV2WithHttpInfo.
     *
     * Get Futures Position-Risk of Sub-account V2 (For Master Account) (USER_DATA)
     *
     * @param string   $email       [Sub-account email](#email-address) (required)
     * @param int      $futuresType 1:USDT-margined Futures，2: Coin-margined Futures (required)
     * @param null|int $recvWindow  (optional)
     *
     * @return ApiResponse<GetFuturesPositionRiskOfSubAccountV2Response>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccountV2WithHttpInfo($email, $futuresType, $recvWindow = null): ApiResponse
    {
        $request = $this->getFuturesPositionRiskOfSubAccountV2Request($email, $futuresType, $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\SubAccount\Model\GetFuturesPositionRiskOfSubAccountV2Response',
                        $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\SubAccount\Model\GetFuturesPositionRiskOfSubAccountV2Response',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\GetFuturesPositionRiskOfSubAccountV2Response',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getFuturesPositionRiskOfSubAccountV2'.
     *
     * @param string   $email       [Sub-account email](#email-address) (required)
     * @param int      $futuresType 1:USDT-margined Futures，2: Coin-margined Futures (required)
     * @param null|int $recvWindow  (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getFuturesPositionRiskOfSubAccountV2Request($email, $futuresType, $recvWindow = null)
    {
        $contentType = self::contentTypes['getFuturesPositionRiskOfSubAccountV2'][0];

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

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

        $resourcePath = '/sapi/v2/sub-account/futures/positionRisk';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $email,
            'email', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            true // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $futuresType,
            'futuresType', // param base name
            'integer', // 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 getSubAccountsStatusOnMarginOrFutures.
     *
     * Get Sub-account&#39;s Status on Margin Or Futures (For Master Account) (USER_DATA)
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|int    $recvWindow recvWindow (optional)
     *
     * @return ApiResponse<GetSubAccountsStatusOnMarginOrFuturesResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getSubAccountsStatusOnMarginOrFutures($email = null, $recvWindow = null): ApiResponse
    {
        return $this->getSubAccountsStatusOnMarginOrFuturesWithHttpInfo($email, $recvWindow);
    }

    /**
     * Operation getSubAccountsStatusOnMarginOrFuturesWithHttpInfo.
     *
     * Get Sub-account&#39;s Status on Margin Or Futures (For Master Account) (USER_DATA)
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return ApiResponse<GetSubAccountsStatusOnMarginOrFuturesResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function getSubAccountsStatusOnMarginOrFuturesWithHttpInfo($email = null, $recvWindow = null): ApiResponse
    {
        $request = $this->getSubAccountsStatusOnMarginOrFuturesRequest($email, $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\SubAccount\Model\GetSubAccountsStatusOnMarginOrFuturesResponse',
                        $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\SubAccount\Model\GetSubAccountsStatusOnMarginOrFuturesResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\GetSubAccountsStatusOnMarginOrFuturesResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'getSubAccountsStatusOnMarginOrFutures'.
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function getSubAccountsStatusOnMarginOrFuturesRequest($email = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['getSubAccountsStatusOnMarginOrFutures'][0];

        $resourcePath = '/sapi/v1/sub-account/status';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $email,
            'email', // 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 querySubAccountList.
     *
     * Query Sub-account List (For Master Account) (USER_DATA)
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|string $isFreeze   true or false (optional)
     * @param null|int    $page       Default value: 1 (optional)
     * @param null|int    $limit      Default value: 1, Max value: 200 (optional)
     * @param null|int    $recvWindow recvWindow (optional)
     *
     * @return ApiResponse<QuerySubAccountListResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function querySubAccountList($email = null, $isFreeze = null, $page = null, $limit = null, $recvWindow = null): ApiResponse
    {
        return $this->querySubAccountListWithHttpInfo($email, $isFreeze, $page, $limit, $recvWindow);
    }

    /**
     * Operation querySubAccountListWithHttpInfo.
     *
     * Query Sub-account List (For Master Account) (USER_DATA)
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|string $isFreeze   true or false (optional)
     * @param null|int    $page       Default value: 1 (optional)
     * @param null|int    $limit      Default value: 1, Max value: 200 (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return ApiResponse<QuerySubAccountListResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function querySubAccountListWithHttpInfo($email = null, $isFreeze = null, $page = null, $limit = null, $recvWindow = null): ApiResponse
    {
        $request = $this->querySubAccountListRequest($email, $isFreeze, $page, $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\SubAccount\Model\QuerySubAccountListResponse',
                        $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\SubAccount\Model\QuerySubAccountListResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\QuerySubAccountListResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'querySubAccountList'.
     *
     * @param null|string $email      Managed sub-account email (optional)
     * @param null|string $isFreeze   true or false (optional)
     * @param null|int    $page       Default value: 1 (optional)
     * @param null|int    $limit      Default value: 1, Max value: 200 (optional)
     * @param null|int    $recvWindow (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function querySubAccountListRequest($email = null, $isFreeze = null, $page = null, $limit = null, $recvWindow = null)
    {
        $contentType = self::contentTypes['querySubAccountList'][0];

        $resourcePath = '/sapi/v1/sub-account/list';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $email,
            'email', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $isFreeze,
            'isFreeze', // param base name
            'string', // openApiType
            'form', // style
            true, // explode
            false // required
        ) ?? []);
        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $page,
            'page', // 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 querySubAccountTransactionStatistics.
     *
     * Query Sub-account Transaction Statistics (For Master Account) (USER_DATA)
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow recvWindow (optional)
     *
     * @return ApiResponse<QuerySubAccountTransactionStatisticsResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function querySubAccountTransactionStatistics($email, $recvWindow = null): ApiResponse
    {
        return $this->querySubAccountTransactionStatisticsWithHttpInfo($email, $recvWindow);
    }

    /**
     * Operation querySubAccountTransactionStatisticsWithHttpInfo.
     *
     * Query Sub-account Transaction Statistics (For Master Account) (USER_DATA)
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow (optional)
     *
     * @return ApiResponse<QuerySubAccountTransactionStatisticsResponse>
     *
     * @throws ApiException              on non-2xx response or if the response body is not in the expected format
     * @throws \InvalidArgumentException
     */
    public function querySubAccountTransactionStatisticsWithHttpInfo($email, $recvWindow = null): ApiResponse
    {
        $request = $this->querySubAccountTransactionStatisticsRequest($email, $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\SubAccount\Model\QuerySubAccountTransactionStatisticsResponse',
                        $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\SubAccount\Model\QuerySubAccountTransactionStatisticsResponse',
                $request,
                $response,
            );
        } catch (ApiException $e) {
            switch ($e->getCode()) {
                case 200:
                    $data = ObjectSerializer::deserialize(
                        $e->getResponseBody(),
                        '\Binance\Client\SubAccount\Model\QuerySubAccountTransactionStatisticsResponse',
                        $e->getResponseHeaders()
                    );
                    $e->setResponseObject($data);

                    throw $e;
            }

            throw $e;
        }
    }

    /**
     * Create request for operation 'querySubAccountTransactionStatistics'.
     *
     * @param string   $email      [Sub-account email](#email-address) (required)
     * @param null|int $recvWindow (optional)
     *
     * @return Request
     *
     * @throws \InvalidArgumentException
     */
    public function querySubAccountTransactionStatisticsRequest($email, $recvWindow = null)
    {
        $contentType = self::contentTypes['querySubAccountTransactionStatistics'][0];

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

        $resourcePath = '/sapi/v1/sub-account/transaction-statistics';
        $formParams = [];
        $queryParams = [];
        $headerParams = [];
        $httpBody = '';
        $multipart = false;

        // query params
        $queryParams = array_merge($queryParams, ObjectSerializer::toQueryValue(
            $email,
            'email', // 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
        );
    }

    /**
     * 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;
    }
}
