"""
Binance Spot REST API

OpenAPI Specifications for the Binance Spot REST API

API documents:
  - [Github rest-api documentation file](https://github.com/binance/binance-spot-api-docs/blob/master/rest-api.md)
  - [General API information for rest-api on website](https://developers.binance.com/docs/binance-spot-api-docs/rest-api/general-api-information)

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""

from typing import List, Optional, Union
from requests import Session
from binance_common.configuration import ConfigurationRestAPI
from binance_common.errors import RequiredError
from binance_common.models import ApiResponse
from binance_common.signature import Signers
from binance_common.utils import send_request

from ..models import AggTradesResponse
from ..models import AvgPriceResponse
from ..models import DepthResponse
from ..models import GetTradesResponse
from ..models import HistoricalTradesResponse
from ..models import KlinesResponse
from ..models import TickerResponse
from ..models import Ticker24hrResponse
from ..models import TickerBookTickerResponse
from ..models import TickerPriceResponse
from ..models import TickerTradingDayResponse
from ..models import UiKlinesResponse


from ..models import DepthSymbolStatusEnum
from ..models import KlinesIntervalEnum
from ..models import TickerWindowSizeEnum
from ..models import TickerTypeEnum
from ..models import TickerSymbolStatusEnum
from ..models import Ticker24hrTypeEnum
from ..models import Ticker24hrSymbolStatusEnum
from ..models import TickerBookTickerSymbolStatusEnum
from ..models import TickerPriceSymbolStatusEnum
from ..models import TickerTradingDayTypeEnum
from ..models import TickerTradingDaySymbolStatusEnum
from ..models import UiKlinesIntervalEnum


class MarketApi:
    """API Client for MarketApi endpoints."""

    def __init__(
        self,
        configuration: ConfigurationRestAPI = None,
        session: Session = None,
        signer: Signers = None,
    ) -> None:
        self._configuration = configuration
        self._session = session
        self._signer = signer

    def agg_trades(
        self,
        symbol: Union[str, None],
        from_id: Optional[int] = None,
        start_time: Optional[int] = None,
        end_time: Optional[int] = None,
        limit: Optional[int] = None,
    ) -> ApiResponse[AggTradesResponse]:
        """
                Compressed/Aggregate trades list
                GET /api/v3/aggTrades
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#compressedaggregate-trades-list

                Get compressed, aggregate trades. Trades that fill at the time, from the same taker order, with the same price will have the quantity aggregated.
        Weight: 4

                Args:
                    symbol (Union[str, None]):
                    from_id (Optional[int] = None): ID to get aggregate trades from INCLUSIVE.
                    start_time (Optional[int] = None): Timestamp in ms to get aggregate trades from INCLUSIVE.
                    end_time (Optional[int] = None): Timestamp in ms to get aggregate trades until INCLUSIVE.
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.

                Returns:
                    ApiResponse[AggTradesResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )

        body = {}
        payload = {
            "symbol": symbol,
            "from_id": from_id,
            "start_time": start_time,
            "end_time": end_time,
            "limit": limit,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/aggTrades",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=AggTradesResponse,
        )

    def avg_price(
        self,
        symbol: Union[str, None],
    ) -> ApiResponse[AvgPriceResponse]:
        """
                Current average price
                GET /api/v3/avgPrice
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#current-average-price

                Current average price for a symbol.
        Weight: 2

                Args:
                    symbol (Union[str, None]):

                Returns:
                    ApiResponse[AvgPriceResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )

        body = {}
        payload = {"symbol": symbol}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/avgPrice",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=AvgPriceResponse,
        )

    def depth(
        self,
        symbol: Union[str, None],
        limit: Optional[int] = None,
        symbol_status: Optional[DepthSymbolStatusEnum] = None,
    ) -> ApiResponse[DepthResponse]:
        """
                Order book
                GET /api/v3/depth
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#order-book


        Weight: Adjusted based on the limit:

        |Limit|Request Weight
        ------|-------
        1-100|  5
        101-500| 25
        501-1000| 50
        1001-5000| 250

                Args:
                    symbol (Union[str, None]):
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.
                    symbol_status (Optional[DepthSymbolStatusEnum] = None):

                Returns:
                    ApiResponse[DepthResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )

        body = {}
        payload = {"symbol": symbol, "limit": limit, "symbol_status": symbol_status}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/depth",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=DepthResponse,
        )

    def get_trades(
        self,
        symbol: Union[str, None],
        limit: Optional[int] = None,
    ) -> ApiResponse[GetTradesResponse]:
        """
                Recent trades list
                GET /api/v3/trades
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#recent-trades-list

                Get recent trades.
        Weight: 25

                Args:
                    symbol (Union[str, None]):
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.

                Returns:
                    ApiResponse[GetTradesResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )

        body = {}
        payload = {"symbol": symbol, "limit": limit}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/trades",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=GetTradesResponse,
        )

    def historical_trades(
        self,
        symbol: Union[str, None],
        limit: Optional[int] = None,
        from_id: Optional[int] = None,
    ) -> ApiResponse[HistoricalTradesResponse]:
        """
                Old trade lookup
                GET /api/v3/historicalTrades
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#old-trade-lookup

                Get older trades.
        Weight: 25

                Args:
                    symbol (Union[str, None]):
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.
                    from_id (Optional[int] = None): ID to get aggregate trades from INCLUSIVE.

                Returns:
                    ApiResponse[HistoricalTradesResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )

        body = {}
        payload = {"symbol": symbol, "limit": limit, "from_id": from_id}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/historicalTrades",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=HistoricalTradesResponse,
        )

    def klines(
        self,
        symbol: Union[str, None],
        interval: Union[KlinesIntervalEnum, None],
        start_time: Optional[int] = None,
        end_time: Optional[int] = None,
        time_zone: Optional[str] = None,
        limit: Optional[int] = None,
    ) -> ApiResponse[KlinesResponse]:
        """
                Kline/Candlestick data
                GET /api/v3/klines
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#klinecandlestick-data

                Kline/candlestick bars for a symbol.
        Klines are uniquely identified by their open time.
        Weight: 2

                Args:
                    symbol (Union[str, None]):
                    interval (Union[KlinesIntervalEnum, None]):
                    start_time (Optional[int] = None): Timestamp in ms to get aggregate trades from INCLUSIVE.
                    end_time (Optional[int] = None): Timestamp in ms to get aggregate trades until INCLUSIVE.
                    time_zone (Optional[str] = None): Default: 0 (UTC)
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.

                Returns:
                    ApiResponse[KlinesResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )
        if interval is None:
            raise RequiredError(
                field="interval", error_message="Missing required parameter 'interval'"
            )

        body = {}
        payload = {
            "symbol": symbol,
            "interval": interval,
            "start_time": start_time,
            "end_time": end_time,
            "time_zone": time_zone,
            "limit": limit,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/klines",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=KlinesResponse,
        )

    def ticker(
        self,
        symbol: Optional[str] = None,
        symbols: Optional[List[str]] = None,
        window_size: Optional[TickerWindowSizeEnum] = None,
        type: Optional[TickerTypeEnum] = None,
        symbol_status: Optional[TickerSymbolStatusEnum] = None,
    ) -> ApiResponse[TickerResponse]:
        """
                Rolling window price change statistics
                GET /api/v3/ticker
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#rolling-window-price-change-statistics


        Weight: 4 for each requested <tt>symbol</tt> regardless of <tt>windowSize</tt>. <br/><br/> The weight for this request will cap at 200 once the number of `symbols` in the request is more than 50.

                Args:
                    symbol (Optional[str] = None): Symbol to query
                    symbols (Optional[List[str]] = None): List of symbols to query
                    window_size (Optional[TickerWindowSizeEnum] = None):
                    type (Optional[TickerTypeEnum] = None):
                    symbol_status (Optional[TickerSymbolStatusEnum] = None):

                Returns:
                    ApiResponse[TickerResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        body = {}
        payload = {
            "symbol": symbol,
            "symbols": symbols,
            "window_size": window_size,
            "type": type,
            "symbol_status": symbol_status,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/ticker",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=TickerResponse,
        )

    def ticker24hr(
        self,
        symbol: Optional[str] = None,
        symbols: Optional[List[str]] = None,
        type: Optional[Ticker24hrTypeEnum] = None,
        symbol_status: Optional[Ticker24hrSymbolStatusEnum] = None,
    ) -> ApiResponse[Ticker24hrResponse]:
        """
                24hr ticker price change statistics
                GET /api/v3/ticker/24hr
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#24hr-ticker-price-change-statistics

                24 hour rolling window price change statistics. **Careful** when accessing this with no symbol.
        Weight: <table>
        <thead>
            <tr>
                <th>Parameter</th>
                <th>Symbols Provided</th>
                <th>Weight</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td rowspan="2">symbol</td>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>symbol parameter is omitted</td>
                <td>80</td>
            </tr>
            <tr>
                <td rowspan="4">symbols</td>
                <td>1-20</td>
                <td>2</td>
            </tr>
            <tr>
                <td>21-100</td>
                <td>40</td>
            </tr>
            <tr>
                <td>101 or more</td>
                <td>80</td>
            </tr>
            <tr>
                <td>symbols parameter is omitted</td>
                <td>80</td>
            </tr>
        </tbody>
        </table>

                Args:
                    symbol (Optional[str] = None): Symbol to query
                    symbols (Optional[List[str]] = None): List of symbols to query
                    type (Optional[Ticker24hrTypeEnum] = None):
                    symbol_status (Optional[Ticker24hrSymbolStatusEnum] = None):

                Returns:
                    ApiResponse[Ticker24hrResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        body = {}
        payload = {
            "symbol": symbol,
            "symbols": symbols,
            "type": type,
            "symbol_status": symbol_status,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/ticker/24hr",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=Ticker24hrResponse,
        )

    def ticker_book_ticker(
        self,
        symbol: Optional[str] = None,
        symbols: Optional[List[str]] = None,
        symbol_status: Optional[TickerBookTickerSymbolStatusEnum] = None,
    ) -> ApiResponse[TickerBookTickerResponse]:
        """
                Symbol order book ticker
                GET /api/v3/ticker/bookTicker
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#symbol-order-book-ticker

                Best price/qty on the order book for a symbol or symbols.
        Weight: <table>
        <thead>
            <tr>
                <th>Parameter</th>
                <th>Symbols Provided</th>
                <th>Weight</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td rowspan="2">symbol</td>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>symbol parameter is omitted</td>
                <td>4</td>
            </tr>
            <tr>
                <td>symbols</td>
                <td>Any</td>
                <td>4</td>
            </tr>
        </tbody>
        </table>

                Args:
                    symbol (Optional[str] = None): Symbol to query
                    symbols (Optional[List[str]] = None): List of symbols to query
                    symbol_status (Optional[TickerBookTickerSymbolStatusEnum] = None):

                Returns:
                    ApiResponse[TickerBookTickerResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        body = {}
        payload = {"symbol": symbol, "symbols": symbols, "symbol_status": symbol_status}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/ticker/bookTicker",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=TickerBookTickerResponse,
        )

    def ticker_price(
        self,
        symbol: Optional[str] = None,
        symbols: Optional[List[str]] = None,
        symbol_status: Optional[TickerPriceSymbolStatusEnum] = None,
    ) -> ApiResponse[TickerPriceResponse]:
        """
                Symbol price ticker
                GET /api/v3/ticker/price
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#symbol-price-ticker

                Latest price for a symbol or symbols.
        Weight: <table>
        <thead>
            <tr>
                <th>Parameter</th>
                <th>Symbols Provided</th>
                <th>Weight</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td rowspan="2">symbol</td>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>symbol parameter is omitted</td>
                <td>4</td>
            </tr>
            <tr>
                <td>symbols</td>
                <td>Any</td>
                <td>4</td>
            </tr>
        </tbody>
        </table>

                Args:
                    symbol (Optional[str] = None): Symbol to query
                    symbols (Optional[List[str]] = None): List of symbols to query
                    symbol_status (Optional[TickerPriceSymbolStatusEnum] = None):

                Returns:
                    ApiResponse[TickerPriceResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        body = {}
        payload = {"symbol": symbol, "symbols": symbols, "symbol_status": symbol_status}

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/ticker/price",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=TickerPriceResponse,
        )

    def ticker_trading_day(
        self,
        symbol: Optional[str] = None,
        symbols: Optional[List[str]] = None,
        time_zone: Optional[str] = None,
        type: Optional[TickerTradingDayTypeEnum] = None,
        symbol_status: Optional[TickerTradingDaySymbolStatusEnum] = None,
    ) -> ApiResponse[TickerTradingDayResponse]:
        """
                Trading Day Ticker
                GET /api/v3/ticker/tradingDay
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#trading-day-ticker

                Price change statistics for a trading day.
        Weight: 4 for each requested <tt>symbol</tt>. <br/><br/> The weight for this request will cap at 200 once the number of `symbols` in the request is more than 50.

                Args:
                    symbol (Optional[str] = None): Symbol to query
                    symbols (Optional[List[str]] = None): List of symbols to query
                    time_zone (Optional[str] = None): Default: 0 (UTC)
                    type (Optional[TickerTradingDayTypeEnum] = None):
                    symbol_status (Optional[TickerTradingDaySymbolStatusEnum] = None):

                Returns:
                    ApiResponse[TickerTradingDayResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        body = {}
        payload = {
            "symbol": symbol,
            "symbols": symbols,
            "time_zone": time_zone,
            "type": type,
            "symbol_status": symbol_status,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/ticker/tradingDay",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=TickerTradingDayResponse,
        )

    def ui_klines(
        self,
        symbol: Union[str, None],
        interval: Union[UiKlinesIntervalEnum, None],
        start_time: Optional[int] = None,
        end_time: Optional[int] = None,
        time_zone: Optional[str] = None,
        limit: Optional[int] = None,
    ) -> ApiResponse[UiKlinesResponse]:
        """
                UIKlines
                GET /api/v3/uiKlines
                https://developers.binance.com/docs/binance-spot-api-docs/rest-api/market-data-endpoints#uiklines

                The request is similar to klines having the same parameters and response.

        `uiKlines` return modified kline data, optimized for presentation of candlestick charts.
        Weight: 2

                Args:
                    symbol (Union[str, None]):
                    interval (Union[UiKlinesIntervalEnum, None]):
                    start_time (Optional[int] = None): Timestamp in ms to get aggregate trades from INCLUSIVE.
                    end_time (Optional[int] = None): Timestamp in ms to get aggregate trades until INCLUSIVE.
                    time_zone (Optional[str] = None): Default: 0 (UTC)
                    limit (Optional[int] = None): Default: 500; Maximum: 1000.

                Returns:
                    ApiResponse[UiKlinesResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        if symbol is None:
            raise RequiredError(
                field="symbol", error_message="Missing required parameter 'symbol'"
            )
        if interval is None:
            raise RequiredError(
                field="interval", error_message="Missing required parameter 'interval'"
            )

        body = {}
        payload = {
            "symbol": symbol,
            "interval": interval,
            "start_time": start_time,
            "end_time": end_time,
            "time_zone": time_zone,
            "limit": limit,
        }

        return send_request(
            self._session,
            self._configuration,
            method="GET",
            path="/api/v3/uiKlines",
            payload=payload,
            body=body,
            time_unit=self._configuration.time_unit,
            response_model=UiKlinesResponse,
        )
