0

I'm trying to establish a hub connection and negotiate through an API provided by a stock exchange company. This is the piece of code developed using the Signalr_aio in Python.

from signalr_aio import Connection
import asyncio
from requests import Session

async def pushMessage(**msg):
    print(msg)
    if 'R' in msg and type(msg['R']) is not bool:
        token = msg['R']
        sessionRealtime.headers.update({'Authorization': 'Bearer {}'.format(token)})

server_url = 'https://edbi.ephoenix.ir/realtime'
sessionRealtime = Session()
connection = Connection(server_url, session=sessionRealtime)
connection.received += pushMessage
hub = connection.register_hub('omsclienttokenhub')
hub.server.invoke('GetAPIToken', 'xxxxxxxxx', 'yyyyyyyyyy')
hub = connection.register_hub('omsclienthub')
connection.start()

I tried the 'GetTime' method and it did successfully returned the time,

hub.server.invoke('GetTime')

{'R': '00:19:10', 'I': '2'}

However, I get an error when I invoke the 'GetInstrumentList' method,

hub.server.invoke('GetInstrumentList')

{'R': {'ex': {'i': None, 'm': 'Object reference not set to an instance of an object.'}}, 'I': '1'}

I guess that there is a problem in updating the header of the request? Or should I transport the token as a querystring in a modified URL?!

1 Answers1

1

Try out below code

from requests import Session
from signalr import Connection
import asyncio

server_url = 'https://edbi.ephoenix.ir/realtime'

async def pushMessage(**msg):
    print(msg)
    if 'R' in msg and type(msg['R']) is not bool:
        token = msg['R']
        global server_url
        server_url = 'https://edbi.ephoenix.ir/realtime' +  "?Token={}".format(token)

with Session() as session:
    #create a connection  
    connection = Connection(server_url, session)
    connection.received += pushMessage
    hub = connection.register_hub('omsclienttokenhub')
    hub.server.invoke('GetAPIToken', 'xxxxxxxxx', 'yyyyyyyyyy')
    hub = connection.register_hub('omsclienthub')
    connection.start()

I had taken some reference from this link

Nandan Rana
  • 539
  • 3
  • 12
  • I cannot put connection = Connection(server_url + "?Token={}".format(token), session) at that line since the token is released after registering the connection connection.register_hub('omsclienttokenhub'). – Mohammad Namakshenas Apr 16 '20 at 20:09
  • Another issue is that the token is retrieved in a serialized fashion through an **async** procedure. That's why I defined the function **async def pushMessage(**msg)**. – Mohammad Namakshenas Apr 16 '20 at 20:12
  • Yes. {'R': {'ex': {'i': None, 'm': 'Object reference not set to an instance of an object.'}}, 'I': '1'} – Mohammad Namakshenas Apr 16 '20 at 20:29