sammchardy / python-binance

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

Binance API Exception: APIError(code=0): Invalid JSON error message from Binance #1346

Open ArieArepunk opened 11 months ago

ArieArepunk commented 11 months ago

When i run my code it's fine, but when about 1-2 hour i got the error. Can someone help me please?

gnemoug commented 11 months ago

you can custom the Client, use CustomClient to replace Client

class CustomClient(Client):
    def _create_futures_api_uri(self, path: str, version: int = 1) -> str:
        url = self.FUTURES_URL
        if self.testnet:
            url = self.FUTURES_TESTNET_URL

        v_path = self.FUTURES_API_VERSION
        if version == 2:
            v_path = self.FUTURES_API_VERSION2
        return url + '/' + v_path + '/' + path

    def _request_futures_api(
        self, method, path, signed=False, version=1, **kwargs
    ) -> Dict:
        uri = self._create_futures_api_uri(path, version=version)

        return self._request(method, uri, signed, True, **kwargs)

    def futures_account_balance(self, **params):
        """Get futures account balance
        https://binance-docs.github.io/apidocs/futures/en/#future-account-balance-user_data
        """
        return self._request_futures_api('get', 'balance', True, version=2, data=params)

    def futures_account(self, **params):
        """Get current account information.
        https://binance-docs.github.io/apidocs/futures/en/#account-information-user_data
        """
        return self._request_futures_api('get', 'account', True, version=2, data=params)

    def futures_position_information(self, **params):
        """Get position information
        https://binance-docs.github.io/apidocs/futures/en/#position-information-user_data
        """
        return self._request_futures_api(
            'get', 'positionRisk', True, version=2, data=params
        )
Tenzdenek commented 11 months ago

if i use async client . i must do this with async client too becouse i have the same problem but i use async and client too i my code

ArieArepunk commented 11 months ago

you can custom the Client, use CustomClient to replace Client

class CustomClient(Client):
    def _create_futures_api_uri(self, path: str, version: int = 1) -> str:
        url = self.FUTURES_URL
        if self.testnet:
            url = self.FUTURES_TESTNET_URL

        v_path = self.FUTURES_API_VERSION
        if version == 2:
            v_path = self.FUTURES_API_VERSION2
        return url + '/' + v_path + '/' + path

    def _request_futures_api(
        self, method, path, signed=False, version=1, **kwargs
    ) -> Dict:
        uri = self._create_futures_api_uri(path, version=version)

        return self._request(method, uri, signed, True, **kwargs)

    def futures_account_balance(self, **params):
        """Get futures account balance
        https://binance-docs.github.io/apidocs/futures/en/#future-account-balance-user_data
        """
        return self._request_futures_api('get', 'balance', True, version=2, data=params)

    def futures_account(self, **params):
        """Get current account information.
        https://binance-docs.github.io/apidocs/futures/en/#account-information-user_data
        """
        return self._request_futures_api('get', 'account', True, version=2, data=params)

    def futures_position_information(self, **params):
        """Get position information
        https://binance-docs.github.io/apidocs/futures/en/#position-information-user_data
        """
        return self._request_futures_api(
            'get', 'positionRisk', True, version=2, data=params
        )

Thank you