Kucoin / kucoin-python-sdk

MIT License
40 stars 11 forks source link

If "data" of API response is empty list, methods return *dict* instead of *list* #78

Closed funduck closed 1 year ago

funduck commented 2 years ago

For example, if one calls data = market.get_kline(...) and there are no klines found the data will be {code:200000,data:[]} That is at least not expected because of method description

"""
        https://docs.kucoin.com/#get-klines
        :param symbol: symbol (Mandatory)
        :type: str
        :param kline_type: Type of candlestick patterns (Mandatory)
        :type: str
        :param kwargs: [Optional] startAt, endAt, currentPage, pageSize
        :return:
        [
          [
              "1545904980",             //Start time of the candle cycle
              "0.058",                  //opening price
              "0.049",                  //closing price
              "0.058",                  //highest price
              "0.049",                  //lowest price
              "0.018",                  //Transaction amount
              "0.000945"                //Transaction volume
          ],
          [
              "1545904920",
              "0.058",
              "0.072",
              "0.072",
              "0.058",
              "0.103",
              "0.006986"
          ]
        ]
        """

Since a lot of people already use the library it is not possible to change behavior, but may be a good idea to point this case in description

codewc commented 1 year ago

For example, if one calls data = market.get_kline(...) and there are no klines found the data will be {code:200000,data:[]} That is at least not expected because of method description

"""
        https://docs.kucoin.com/#get-klines
        :param symbol: symbol (Mandatory)
        :type: str
        :param kline_type: Type of candlestick patterns (Mandatory)
        :type: str
        :param kwargs: [Optional] startAt, endAt, currentPage, pageSize
        :return:
        [
          [
              "1545904980",             //Start time of the candle cycle
              "0.058",                  //opening price
              "0.049",                  //closing price
              "0.058",                  //highest price
              "0.049",                  //lowest price
              "0.018",                  //Transaction amount
              "0.000945"                //Transaction volume
          ],
          [
              "1545904920",
              "0.058",
              "0.072",
              "0.072",
              "0.058",
              "0.103",
              "0.006986"
          ]
        ]
        """

Since a lot of people already use the library it is not possible to change behavior, but may be a good idea to point this case in description

    @staticmethod
    def check_response_data(response_data):
        if response_data.status_code == 200:
            try:
                data = response_data.json()
            except ValueError:
                raise Exception(response_data.content)
            else:
                if data and data.get('code'):
                    if data.get('code') == '200000':
                        if data.get('data'):
                            return data['data']
                        else:
                            return data
                    else:
                        raise Exception("{}-{}".format(response_data.status_code, response_data.text))
        else:
            raise Exception("{}-{}".format(response_data.status_code, response_data.text))

Because the data is empty, more information will be printed https://github.com/Kucoin/kucoin-python-sdk/blob/2f1d9a3851309fb3da92762edcbb900cc40d6381/kucoin/base_request/base_request.py#L111

funduck commented 1 year ago

Thanks, I get the idea.