sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.91k stars 2.19k forks source link

Select API base endpoint for spot/margin #1282

Open mkaanerkoc opened 1 year ago

mkaanerkoc commented 1 year ago

Hello everyone,

Recently, I have been experiencing frequent timeout errors while using the Binance API. Upon reviewing their documentation, I noticed that they offer five different base endpoints for the spot/margin endpoint which can be alternated when there is API overload. I believe that it would be a simple and effective improvement to the Client/AsyncClient classes if we were able to inject the base endpoint from the constructor. This would allow for more flexibility and convenience for the users. What do you think ?

A simple solution would be like this.

BASE_ENDPOINT_DEFAULT = ''
BASE_ENDPOINT_1 = '1'
BASE_ENDPOINT_2 = '2'
BASE_ENDPOINT_3 = '3'
BASE_ENDPOINT_4 = '4'

def __init__(
    self, api_key: Optional[str] = None, api_secret: Optional[str] = None,
    requests_params: Optional[Dict[str, str]] = None, tld: str = 'com',
    testnet: bool = False, base_endpoint:str = BASE_ENDPOINT_DEFAULT
):
    """Binance API Client constructor

    :param api_key: Api Key
    :type api_key: str.
    :param api_secret: Api Secret
    :type api_secret: str.
    :param requests_params: optional - Dictionary of requests params to use for all calls
    :type requests_params: dict.
    :param testnet: Use testnet environment - only available for vanilla options at the moment
    :type testnet: bool
    :param base_endpoint: Base endpoint for API
    :type base_endpoint: str

    """

    self.tld = tld
    self.base_endpoint = base_endpoint
    self.API_URL = self.API_URL.format(base_endpoint, tld)
    self.MARGIN_API_URL = self.MARGIN_API_URL.format(tld)
sammchardy commented 1 year ago

Hi @mkaanerkoc, sounds like a good idea.

I'll include it in the next update shortly.