Ejemplo n.º 1
0
    def response_handler(self, response, command, **kwargs):
        if ("error" in response) and (response["error"] is not None):
            raise PoloniexError(response["error"])

        for case in switch(command):
            if case("generateNewAddress"):
                if response["success"] == 0:
                    if "address" in response:
                        raise AddressAlreadyExist(
                            "Address [{}] already exist".format(
                                response["address"]))
                    elif "response" in response:
                        raise PoloniexError(response["response"])

        return response
Ejemplo n.º 2
0
    def api_call(self, *args, **kwargs):
        response = requests.get(self.url, *args, **kwargs)

        if response.status_code == 200:
            return response.json()
        else:
            raise PoloniexError('Got {} when calling {}.'.format(
                response.status_code, self.url))
Ejemplo n.º 3
0
    async def api_call(self, *args, **kwargs):
        async with self.session.get(self.url, *args, **kwargs) as response:
            logger.debug(response)
            response = await response.json()

            if ("error" in response) and (response["error"] is not None):
                raise PoloniexError(response["error"])

            return response
Ejemplo n.º 4
0
        def decorator(self, *args, **kwargs):
            method, params = self.get_params(func.__name__, **kwargs)

            if method == "post":
                response = self.api_call(data=params)
            elif method == "get":
                response = self.api_call(params=params)
            else:
                raise PoloniexError("Not available method '{}'".format(method))

            return self.response_handler(response)
Ejemplo n.º 5
0
        async def async_decorator(self, *args, **kwargs):
            kwargs = apply_defaults(func, *args, **kwargs)
            method, params = self.get_params(func.__name__, **kwargs)

            if method == "post":
                response = await self.api_call(data=params)
            elif method == "get":
                response = await self.api_call(params=params)
            else:
                raise PoloniexError("Not available method '{}'".format(method))

            return self.response_handler(response, command=func.__name__)
Ejemplo n.º 6
0
    def api_call(self, *args, **kwargs):
        data, headers = self.secure_request(kwargs.get('data', {}),
                                            kwargs.get('headers', {}))

        kwargs['data'] = data
        kwargs['headers'] = headers

        response = requests.post(self.url, *args, **kwargs)
        if response.status_code == 200:
            return response.json()
        else:
            raise PoloniexError('Got {} when calling {}.'.format(
                response.status_code, self.url))
Ejemplo n.º 7
0
    def get_params(self, command, **kwargs):
        currency_pair = kwargs.get("currency_pair")
        if currency_pair and currency_pair not in constants.CURRENCY_PAIRS + [
                "all"
        ]:
            raise PoloniexError(
                "Currency pair '{}' not available.".format(currency_pair))

        depth = kwargs.get("depth")

        start = kwargs.get("start")
        if start:
            start = start.timestamp()

        end = kwargs.get("end")
        if end:
            end = end.timestamp()

        period = kwargs.get("period")
        if period and period not in constants.CHART_DATA_PERIODS:
            raise PoloniexError("Period '{}' not available.".format(period))

        for case in switch(command):
            if case("returnTicker"):
                method = "get"
                params = {"command": command}
                break

            if case("return24hVolume"):
                method = "get"
                params = {"command": command}
                break

            if case("returnOrderBook"):
                method = "get"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "depth": depth
                }
                break

            if case("returnChartData"):
                method = "get"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "period": period,
                    "start": start,
                    "end": end
                }
                break

            if case("returnCurrencies"):
                method = "get"
                params = {"command": command}
                break

            if case("returnTradeHistory"):
                method = "get"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "start": start,
                    "end": end
                }
                break

            if case():
                raise NotImplementedError(
                    "There is no command '{}'.".format(command))

        return method, params
Ejemplo n.º 8
0
    def get_params(self, command, **kwargs):
        currency_pair = kwargs.get("currency_pair")
        if currency_pair and currency_pair not in constants.CURRENCY_PAIRS + [
                "all"
        ]:
            raise PoloniexError(
                "Currency pair '{}' not available.".format(currency_pair))

        currency = kwargs.get("currency")

        start = kwargs.get("start")
        if start:
            start = start.timestamp()

        end = kwargs.get("end")
        if end:
            end = end.timestamp()

        rate = kwargs.get("rate")
        amount = kwargs.get("amount")
        address = kwargs.get("address")
        order_number = kwargs.get("order_number")

        for case in switch(command):
            if case("returnBalances"):
                method = "post"
                params = {"command": command}
                break

            if case("returnCompleteBalances"):
                method = "post"
                params = {"command": command}
                break

            if case("returnDepositAddresses"):
                method = "post"
                params = {"command": command}
                break

            if case("generateNewAddress"):
                method = "post"
                params = {"command": command, "currency": currency}
                break

            if case("returnDepositsWithdrawals"):
                method = "post"
                params = {"command": command, "start": start, "end": end}
                break

            if case("returnOpenOrders"):
                method = "post"
                params = {"command": command, "currencyPair": currency_pair}
                break

            if case("returnTradeHistory"):
                method = "post"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "start": start,
                    "end": end
                }
                break

            if case("returnOrderTrades"):
                method = "post"
                params = {"command": command, "orderNumber": order_number}
                break

            if case("buy"):
                method = "post"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "rate": rate,
                    "amount": amount
                }
                break

            if case("sell"):
                method = "post"
                params = {
                    "command": command,
                    "currencyPair": currency_pair,
                    "rate": rate,
                    "amount": amount
                }
                break

            if case("withdraw"):
                method = "post"
                params = {
                    "currency": currency,
                    "amount": amount,
                    "address": address
                }
                break

            if case("cancelOrder"):
                method = "post"
                params = {
                    "command": "cancelOrder",
                    "orderNumber": order_number
                }
                break

            if case():
                raise NotImplementedError(
                    "There is no command '{}'.".format(command))

        return method, params
Ejemplo n.º 9
0
    def response_handler(self, response, **kwargs):
        if ("error" in response) and (response["error"] is not None):
            raise PoloniexError(response["error"])

        return response
Ejemplo n.º 10
0
 def trading(self):
     if not self._trading:
         raise PoloniexError(
             'In order to be able to use trading api you need provide API and SEC keys'
         )
     return self._trading