"""
Binance Gift Card REST API

OpenAPI Specification for the Binance Gift Card REST API
The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""

import requests
from typing import Optional, TypeVar, Union
from binance_common.configuration import ConfigurationRestAPI
from binance_common.models import ApiResponse
from binance_common.signature import Signers
from binance_common.utils import send_request
from .api.market_data_api import MarketDataApi

from .models import CreateADualTokenGiftCardResponse
from .models import CreateASingleTokenGiftCardResponse
from .models import FetchRsaPublicKeyResponse
from .models import FetchTokenLimitResponse
from .models import RedeemABinanceGiftCardResponse
from .models import VerifyBinanceGiftCardByGiftCardNumberResponse


T = TypeVar("T")


class GiftCardRestAPI:
    def __init__(
        self,
        configuration: ConfigurationRestAPI,
    ) -> None:
        self.configuration = configuration
        self._session = requests.Session()
        self._signer = (
            Signers.get_signer(
                configuration.private_key, configuration.private_key_passphrase
            )
            if configuration.private_key is not None
            else None
        )

        self._marketDataApi = MarketDataApi(
            self.configuration, self._session, self._signer
        )

    def send_request(
        self,
        endpoint: str,
        method: str,
        query_params: Optional[dict] = None,
        body_params: Optional[dict] = None,
    ) -> ApiResponse[T]:
        """
        Sends an request to the Binance REST API.

        Args:
            endpoint (str): The API endpoint path to send the request to.
            method (str): The HTTP method to use for the request (e.g. "GET", "POST", "PUT", "DELETE").
            query_params (Optional[dict]): The request payload as a dictionary, or None if no payload is required.
            body_params (Optional[dict]): The request body as a dictionary, or None if no body is required.

        Returns:
            ApiResponse[T]: The API response, where T is the expected response type.
        """
        return send_request[T](
            self._session,
            self.configuration,
            method,
            endpoint,
            query_params,
            body_params,
        )

    def send_signed_request(
        self,
        endpoint: str,
        method: str,
        query_params: Optional[dict] = None,
        body_params: Optional[dict] = None,
    ) -> ApiResponse[T]:
        """
        Sends a signed request to the Binance REST API.

        Args:
            endpoint (str): The API endpoint path to send the request to.
            method (str): The HTTP method to use for the request (e.g. "GET", "POST", "PUT", "DELETE").
            query_params (Optional[dict]): The request payload as a dictionary, or None if no payload is required.
            body_params (Optional[dict]): The request body as a dictionary, or None if no body is required.

        Returns:
            ApiResponse[T]: The API response, where T is the expected response type.
        """
        return send_request[T](
            self._session,
            self.configuration,
            method,
            endpoint,
            query_params,
            body_params,
            is_signed=True,
            signer=self._signer,
        )

    def create_a_dual_token_gift_card(
        self,
        base_token: Union[str, None],
        face_token: Union[str, None],
        base_token_amount: Union[float, None],
        recv_window: Optional[int] = None,
    ) -> ApiResponse[CreateADualTokenGiftCardResponse]:
        """
                Create a dual-token gift card(fixed value, discount feature)(TRADE)

                * This API is for creating a dual-token ( stablecoin-denominated) Binance Gift Card. You may create a gift card using USDT as baseToken, that is redeemable to another designated token (faceToken). For example, you can create a fixed-value BTC gift card and pay with 100 USDT plus minting fee. This gift card can keep the value fixed at 100 USDT before redemption, and will be redeemable to BTC equivalent to 100 USDT upon redemption.
        * Once successfully created, the amount of baseToken (e.g. USDT) in the fixed-value gift card along with the fee would be deducted from your funding wallet.


        * To get started with, please make sure:
        * You have a Binance account
        * You have passed KYB
        * You have a sufﬁcient balance(Gift Card amount and fee amount) in your Binance funding wallet
        * You need Enable Withdrawals for the API Key which requests this endpoint.

        Weight: 1

                Args:
                    base_token (Union[str, None]): The token you want to pay, example: BUSD
                    face_token (Union[str, None]): The token you want to buy, example: BNB. If faceToken = baseToken, it's the same as createCode endpoint.
                    base_token_amount (Union[float, None]): The base token asset quantity, example : 1.002
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[CreateADualTokenGiftCardResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.create_a_dual_token_gift_card(
            base_token, face_token, base_token_amount, recv_window
        )

    def create_a_single_token_gift_card(
        self,
        token: Union[str, None],
        amount: Union[float, None],
        recv_window: Optional[int] = None,
    ) -> ApiResponse[CreateASingleTokenGiftCardResponse]:
        """
                Create a single-token gift card (USER_DATA)

                This API is for creating a Binance Gift Card.

        To get started with, please make sure:

        * You have a Binance account
        * You have passed KYB
        * You have a sufﬁcient balance(Gift Card amount and fee amount) in your Binance funding wallet
        * You need `Enable Withdrawals` for the API Key which requests this endpoint.

        Weight: 1

                Args:
                    token (Union[str, None]): The token type contained in the Binance Gift Card
                    amount (Union[float, None]): The amount of the token contained in the Binance Gift Card
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[CreateASingleTokenGiftCardResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.create_a_single_token_gift_card(
            token, amount, recv_window
        )

    def fetch_rsa_public_key(
        self,
        recv_window: Optional[int] = None,
    ) -> ApiResponse[FetchRsaPublicKeyResponse]:
        """
                Fetch RSA Public Key(USER_DATA)

                This API is for fetching the RSA Public Key.
        This RSA Public key will be used to encrypt the card code.

        **Please note that the RSA Public key fetched is valid only for the current day.**

        Weight: 1

                Args:
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[FetchRsaPublicKeyResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.fetch_rsa_public_key(recv_window)

    def fetch_token_limit(
        self,
        base_token: Union[str, None],
        recv_window: Optional[int] = None,
    ) -> ApiResponse[FetchTokenLimitResponse]:
        """
                Fetch Token Limit(USER_DATA)

                This API is to help you verify which tokens are available for you to create Stablecoin-Denominated gift cards as mentioned in section 2 and its’ limitation.

        Weight: 1

                Args:
                    base_token (Union[str, None]): The token you want to pay, example: BUSD
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[FetchTokenLimitResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.fetch_token_limit(base_token, recv_window)

    def redeem_a_binance_gift_card(
        self,
        code: Union[str, None],
        external_uid: Optional[str] = None,
        recv_window: Optional[int] = None,
    ) -> ApiResponse[RedeemABinanceGiftCardResponse]:
        """
                Redeem a Binance Gift Card(USER_DATA)

                This API is for redeeming a Binance Gift Card
        Once redeemed, the coins will be deposited in your funding wallet.

        * Parameter code can be sent in two formats:
        * Plaintext
        * Encrypted

        * Sending code in Encrypted format provides more security than sending it as a plaintext. To send card code in encrypted format the following steps must be followed:
        * Fetch RSA public key from api stated below.
        * Use the below algorithm to encrypt the card code using the RSA public key fetched above: `RSA/ECB/OAEPWithSHA-256AndMGF1Padding`
        **A sample code snippet (JAVA) is stated below for reference, the same approach can be used for different languages like C#, PERL, PYTHON, SHELL etc.:**

        Weight: 1

                Args:
                    code (Union[str, None]): Redemption code of Binance Gift Card to be redeemed, supports both Plaintext & Encrypted code.
                    external_uid (Optional[str] = None): Each external unique ID represents a unique user on the partner platform. The function helps you to identify the redemption behavior of different users, such as redemption frequency and amount. It also helps risk and limit control of a single account, such as daily limit on redemption volume, frequency, and incorrect number of entries. This will also prevent a single user account reach the partner's daily redemption limits. We strongly recommend you to use this feature and transfer us the User ID of your users if you have different users redeeming Binance Gift Cards on your platform. To protect user data privacy, you may choose to transfer the user id in any desired format (max. 400 characters).
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[RedeemABinanceGiftCardResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.redeem_a_binance_gift_card(
            code, external_uid, recv_window
        )

    def verify_binance_gift_card_by_gift_card_number(
        self,
        reference_no: Union[str, None],
        recv_window: Optional[int] = None,
    ) -> ApiResponse[VerifyBinanceGiftCardByGiftCardNumberResponse]:
        """
                Verify Binance Gift Card by Gift Card Number(USER_DATA)

                This API is for verifying whether the Binance Gift Card is valid or not by entering Gift Card Number.

        **Please note that if you enter the wrong Gift Card Number 5 times within an hour, you will no longer be able to verify any Gift Card Number for that hour.**

        Weight: 1

                Args:
                    reference_no (Union[str, None]): Enter the Gift Card Number
                    recv_window (Optional[int] = None):

                Returns:
                    ApiResponse[VerifyBinanceGiftCardByGiftCardNumberResponse]

                Raises:
                    RequiredError: If a required parameter is missing.

        """

        return self._marketDataApi.verify_binance_gift_card_by_gift_card_number(
            reference_no, recv_window
        )
