Ejemplo n.º 1
0
    async def _authenticate(self, websocket: Websocket):
        requires_authentication = False
        for subscription in self.subscriptions:
            if subscription.requires_authentication():
                requires_authentication = True
                break

        if requires_authentication:
            authentication_message = {
                "type": "AUTHENTICATE",
                "api_token": self.api_key
            }

            LOG.debug(f"> {authentication_message}")
            await websocket.send(json.dumps(authentication_message))

            message = await websocket.receive()
            LOG.debug(f"< {message}")

            message = json.loads(message)
            if 'type' in message and message['type'] == 'AUTHENTICATED':
                LOG.info(f"Websocket authenticated successfully.")
            else:
                raise BitpandaException(
                    f"Authentication error. Response [{message}]")
Ejemplo n.º 2
0
    async def _process_message(self, websocket: Websocket, message: str) -> None:
        message = json.loads(message)

        # subscription negative response
        if "error" in message or message['type'] == "ERROR":
            raise BitpandaException(
                f"Subscription error. Request [{json.dumps(self._get_subscription_message())}] Response [{json.dumps(message)}]")

        # subscription positive response
        elif message['type'] == "SUBSCRIPTIONS":
            LOG.info(f"Subscription confirmed for channels [" + ",".join(
                [channel["name"] for channel in message["channels"]]) + "]")

        # remote termination with an opportunity to reconnect
        elif message["type"] == "CONNECTION_CLOSING":
            LOG.warning(f"Server is performing connection termination with an opportunity to reconnect.")
            raise WebsocketReconnectionException("Graceful connection termination.")

        # heartbeat message
        elif message["type"] == "HEARTBEAT":
            pass

        # regular message
        else:
            await self.publish_message(WebsocketMessage(subscription_id = message['channel_name'], message = message))
Ejemplo n.º 3
0
    async def delete_account_order(self,
                                   order_id: str = None,
                                   client_id: str = None) -> dict:
        if order_id is None and client_id is None:
            raise BitpandaException(
                'One of order_id/client_id has to be provided.')

        if order_id is not None and client_id is not None:
            raise BitpandaException(
                'Only one of order_id/client_id can be provided.')

        if order_id is not None:
            return await self._create_delete("account/orders/" + order_id,
                                             signed=True)
        else:
            return await self._create_delete("account/orders/client/" +
                                             client_id,
                                             signed=True)
Ejemplo n.º 4
0
    async def update_order(self,
                           amount: str,
                           order_id: str = None,
                           client_id: str = None) -> dict:
        if order_id is None and client_id is None:
            raise BitpandaException(
                'One of order_id/client_id has to be provided.')

        if order_id is not None and client_id is not None:
            raise BitpandaException(
                'Only one of order_id/client_id can be provided.')

        data = {"amount": amount}

        if order_id is not None:
            return await self._create_put("account/orders/" + order_id,
                                          data=data,
                                          signed=True)
        else:
            return await self._create_put("account/orders/client/" + client_id,
                                          data=data,
                                          signed=True)