Kotak-Neo / kotak-neo-api

96 stars 80 forks source link

Python SDK v1.2.1 subscribe_to_orderfeed custom message call #135

Open crypt0inf0 opened 5 months ago

crypt0inf0 commented 5 months ago

@bheemaray-hb I'm checking out the new SDK v1.2.1, I noticed that the new SDK does not have custom call back message like

def subscribe_to_orderfeed(self, on_message, on_close, on_error):
        """
            Subscribe To OrderFeed

            Raises:
                Exception: If the user hasn't completes his 2FA.

            Returns:
                Order Feed information.
        """
        if self.configuration.edit_token and self.configuration.edit_sid:
            url = "wss://mlhsi.kotaksecurities.com/realtime?sId="
            neo_api_client.ConnectHSM().hsm_connection(url=url, token=self.configuration.edit_token,
                                                       sid=self.configuration.edit_sid,
                                                       server_id=self.configuration.serverId,
                                                       on_message=on_message,
                                                       on_close=on_close,
                                                       on_error=on_error)

        else:
            return {"Error Message": "Complete the 2fa process before accessing this application"}

instead it has,

def subscribe_to_orderfeed(self):
        """
            Subscribe To OrderFeed

            Raises:
                Exception: If the user hasn't completes his 2FA.

            Returns:
                Order Feed information.
        """
        if self.configuration.edit_token and self.configuration.edit_sid:
            self.check_callbacks()
            if not self.NeoWebSocket:
                self.NeoWebSocket = neo_api_client.NeoWebSocket(self.configuration.edit_sid,
                                                                self.configuration.edit_token,
                                                                self.configuration.serverId)
            self.set_neowebsocket_callbacks()
            self.NeoWebSocket.get_order_feed()

        else:
            return {"Error Message": "Complete the 2fa process before accessing this application"}

It uses the,

def set_neowebsocket_callbacks(self):
        if self.NeoWebSocket is not None:
            self.NeoWebSocket.on_message = self.__on_message
            self.NeoWebSocket.on_error = self.__on_error
            self.NeoWebSocket.on_open = self.__on_open
            self.NeoWebSocket.on_close = self.__on_close

How can I set custom callback function like,

def on_feed(message):
    message = json.loads(message)
    # print(message)
    # Check if 'type' is 'order' and then fetch 'ordSt'
    if message.get('type') == 'order':
        ordSt = message.get('data', {}).get('ordSt')
        print(ordSt)
crypt0inf0 commented 5 months ago

Is it possible to use a custom on message call back for client.subscribe, client.quotes, Can you give me a example or documentation on how to use custom call back functions?

crypt0inf0 commented 5 months ago

I can use the custom callback using client.on_message

def on_feed(message):
    # print(message)
    # Check if 'type' is 'order' and then fetch 'ordSt'
    if message.get('type') == 'order':
        ordSt = message.get('data', {}).get('ordSt')
        #print(ordSt)
        if ordSt == 'complete':
            nOrdNo = message.get('data', {}).get('nOrdNo')
            print(nOrdNo)
    else:
        print("Type is not 'order'")
        return

def app():
    c.on_message = on_feed
    c.subscribe_to_orderfeed()

t1 = Thread(target=app)
t1.start()
crypt0inf0 commented 5 months ago

How to use client.on_message on different function using different requirements?