"""
Binance Spot WebSocket API

OpenAPI Specifications for the Binance Spot WebSocket API

API documents:
  - [Github web-socket-api documentation file](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-api.md)
  - [General API information for web-socket-api on website](https://developers.binance.com/docs/binance-spot-api-docs/web-socket-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 binance_common.models import WebsocketApiResponse
from binance_common.signature import Signers
from binance_common.websocket import WebSocketAPIBase

from ..models import SessionLogonResponse
from ..models import SessionLogoutResponse
from ..models import SessionStatusResponse


from typing import Optional


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

    def __init__(
        self,
        websocket_api: WebSocketAPIBase,
        signer: Signers = None,
    ) -> None:
        self.websocket_api = websocket_api
        self.signer = signer

    async def session_logon(
        self,
        id: Optional[str] = None,
        recv_window: Optional[float] = None,
    ) -> WebsocketApiResponse[SessionLogonResponse]:
        """
            WebSocket Log in with API key
            /session.logon
            https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/authentication-requests#log-in-with-api-key-signed

            Authenticate WebSocket connection using the provided API key.

        After calling `session.logon`, you can omit `apiKey` and `signature` parameters for future requests that require them.

        Note that only one API key can be authenticated.
        Calling `session.logon` multiple times changes the current authenticated API key.
        Weight: 2

            Args:
                    id (Optional[str] = None): Unique WebSocket request ID.
                    recv_window (Optional[float] = None): The value cannot be greater than `60000`. <br> Supports up to three decimal places of precision (e.g., 6000.346) so that microseconds may be specified.

            Returns:
                WebsocketApiResponse[SessionLogonResponse]

            Raises:
                RequiredError: If a required parameter is missing.

        """

        params = {
            **({"id": id} if id is not None else {}),
            **({"recv_window": recv_window} if recv_window is not None else {}),
        }

        payload = {
            "method": "/session.logon".replace("/", "", 1),
            "params": params,
        }

        return await self.websocket_api.send_signed_message(
            payload=payload,
            response_model=SessionLogonResponse,
            signer=self.signer,
            session_logon=True,
        )

    async def session_logout(
        self,
        id: Optional[str] = None,
    ) -> WebsocketApiResponse[SessionLogoutResponse]:
        """
            WebSocket Log out of the session
            /session.logout
            https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/authentication-requests#log-out-of-the-session

            Forget the API key previously authenticated.
        If the connection is not authenticated, this request does nothing.

        Note that the WebSocket connection stays open after `session.logout` request.
        You can continue using the connection,
        but now you will have to explicitly provide the `apiKey` and `signature` parameters where needed.
        Weight: 2

            Args:
                    id (Optional[str] = None): Unique WebSocket request ID.

            Returns:
                WebsocketApiResponse[SessionLogoutResponse]

            Raises:
                RequiredError: If a required parameter is missing.

        """

        params = {
            **({"id": id} if id is not None else {}),
        }

        payload = {
            "method": "/session.logout".replace("/", "", 1),
            "params": params,
        }

        return await self.websocket_api.send_message(
            payload=payload, response_model=SessionLogoutResponse, session_logout=True
        )

    async def session_status(
        self,
        id: Optional[str] = None,
    ) -> WebsocketApiResponse[SessionStatusResponse]:
        """
            WebSocket Query session status
            /session.status
            https://developers.binance.com/docs/binance-spot-api-docs/websocket-api/authentication-requests#query-session-status

            Query the status of the WebSocket connection,
        inspecting which API key (if any) is used to authorize requests.
        Weight: 2

            Args:
                    id (Optional[str] = None): Unique WebSocket request ID.

            Returns:
                WebsocketApiResponse[SessionStatusResponse]

            Raises:
                RequiredError: If a required parameter is missing.

        """

        params = {
            **({"id": id} if id is not None else {}),
        }

        payload = {
            "method": "/session.status".replace("/", "", 1),
            "params": params,
        }

        return await self.websocket_api.send_message(
            payload=payload, response_model=SessionStatusResponse
        )
