Skip to content

Commit 6114574

Browse files
author
Wasin Waeosri
committed
Change logs:
1. Add trkd_wsstreaming.py for Elektron WebSocket connection 2. add Python requirements.txt file
1 parent 6d45020 commit 6114574

File tree

2 files changed

+205
-0
lines changed

2 files changed

+205
-0
lines changed

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
###### Requirements Libraries ######
2+
requests
3+
websocket-client>0.49

trkd_wsstreaming.py

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
'''
2+
The TRKD API sample code is provided for informational purposes only
3+
and without knowledge or assumptions of the end users development environment.
4+
We offer this code to provide developers practical and useful guidance while developing their own code.
5+
However, we do not offer support and troubleshooting of issues that are related to the use of this code
6+
in a particular environment; it is offered solely as sample code for guidance.
7+
Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com
8+
for additional information regarding the TRKD API.'''
9+
10+
import os
11+
import sys
12+
import requests
13+
import json
14+
import getpass
15+
import time
16+
import getopt
17+
import socket
18+
import websocket
19+
import threading
20+
from threading import Thread, Event
21+
22+
23+
# Global Default Variables
24+
ws_address = 'wss://streaming.trkd.thomsonreuters.com/WebSocket/'
25+
trkd_authen_address = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
26+
ws_protocol = 'tr_json2'
27+
position = socket.gethostbyname(socket.gethostname())
28+
29+
# Global Variables
30+
web_socket_app = None
31+
web_socket_open = False
32+
username = None
33+
password = None
34+
appid = None
35+
token = None
36+
expiration = None
37+
38+
## ------------------------------------------ TRKD HTTP REST functions ------------------------------------------ ##
39+
40+
# Send HTTP request for all services
41+
def doSendRequest(url, requestMsg, headers):
42+
result = None
43+
try:
44+
##send request
45+
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
46+
# print('outgoing message is %s'%(json.dumps(requestMsg)))
47+
## handle error
48+
if result.status_code is not 200:
49+
print('Request fail')
50+
print('response status %s'%(result.status_code))
51+
if result.status_code == 500: ## if username or password or appid is wrong
52+
print('Error: %s'%(result.json()))
53+
result.raise_for_status()
54+
except requests.exceptions.RequestException as e:
55+
print('Exception!!!')
56+
print(e)
57+
sys.exit(1)
58+
return result
59+
60+
## Perform authentication
61+
def CreateAuthorization(username, password, appid):
62+
token = None
63+
expiration = None
64+
##create authentication request URL, message and header
65+
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
66+
headers = {'content-type': 'application/json;charset=utf-8'}
67+
print('############### Sending Authentication request message to TRKD ###############')
68+
authenResult = doSendRequest(trkd_authen_address, authenMsg, headers)
69+
if authenResult and authenResult.status_code == 200:
70+
print('Authentication success')
71+
print('response status %s'%(authenResult.status_code))
72+
print('Authentication response %s'%json.dumps(authenResult.json(), sort_keys=True, indent=2, separators=(',', ':')))
73+
##get Token
74+
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
75+
expiration = authenResult.json()['CreateServiceToken_Response_1']['Expiration']
76+
77+
return token, expiration
78+
79+
## ------------------------------------------ TRKD WebSocket functions ------------------------------------------ ##
80+
81+
def process_message(ws, message_json):
82+
""" Parse at high level and output JSON of message """
83+
message_type = message_json['Type']
84+
85+
if message_type == "Refresh":
86+
if 'Domain' in message_json:
87+
message_domain = message_json['Domain']
88+
if message_domain == "Login":
89+
process_login_response(ws, message_json)
90+
elif message_type == "Ping":
91+
pong_json = { 'Type':'Pong' }
92+
ws.send(json.dumps(pong_json))
93+
print("SENT:")
94+
print(json.dumps(pong_json, sort_keys=True, indent=2, separators=(',', ':')))
95+
96+
97+
def process_login_response(ws, message_json):
98+
""" Send item request """
99+
send_market_price_request(ws)
100+
101+
102+
def send_market_price_request(ws):
103+
""" Create and send simple Market Price request """
104+
mp_req_json = {
105+
'ID': 2,
106+
'Key': {
107+
'Name': 'EUR=',
108+
},
109+
}
110+
ws.send(json.dumps(mp_req_json))
111+
print("SENT:")
112+
print(json.dumps(mp_req_json, sort_keys=True, indent=2, separators=(',', ':')))
113+
114+
115+
def send_login_request(ws):
116+
""" Generate a login request from command line data (or defaults) and send """
117+
login_json = {
118+
'ID': 1,
119+
'Domain': 'Login',
120+
'Key': {
121+
'Name': '',
122+
'NameType': 'AuthnToken',
123+
'Elements': {
124+
'ApplicationId': '',
125+
'Position': '',
126+
'AuthenticationToken': ''
127+
}
128+
}
129+
}
130+
131+
login_json['Key']['Name'] = username
132+
login_json['Key']['Elements']['ApplicationId'] = appid
133+
login_json['Key']['Elements']['Position'] = position
134+
login_json['Key']['Elements']['AuthenticationToken'] = token
135+
136+
ws.send(json.dumps(login_json))
137+
print("SENT:")
138+
print(json.dumps(login_json, sort_keys=True, indent=2, separators=(',', ':')))
139+
140+
141+
def on_message(ws, message):
142+
""" Called when message received, parse message into JSON for processing """
143+
print("RECEIVED: ")
144+
message_json = json.loads(message)
145+
print(json.dumps(message_json, sort_keys=True, indent=2, separators=(',', ':')))
146+
147+
for singleMsg in message_json:
148+
process_message(ws, singleMsg)
149+
150+
151+
def on_error(ws, error):
152+
""" Called when websocket error has occurred """
153+
print(error)
154+
155+
156+
def on_close(ws):
157+
""" Called when websocket is closed """
158+
global web_socket_open
159+
print("WebSocket Closed")
160+
web_socket_open = False
161+
162+
163+
def on_open(ws):
164+
""" Called when handshake is complete and websocket is open, send login """
165+
166+
print("WebSocket successfully connected!")
167+
global web_socket_open
168+
web_socket_open = True
169+
send_login_request(ws)
170+
171+
## ------------------------------------------ Main App ------------------------------------------ ##
172+
173+
if __name__ == '__main__':
174+
## Get username, password and applicationid
175+
username = input('Please input username: ')
176+
## use getpass.getpass to hide user inputted password
177+
password = getpass.getpass(prompt='Please input password: ')
178+
appid = input('Please input appid: ')
179+
180+
token, expiration = CreateAuthorization(username,password,appid)
181+
# print('Token = %s'%(token))
182+
## if authentiacation success, continue subscribing Quote
183+
if token and expiration:
184+
print('Do WS here')
185+
# doWebSocketConnection(username, appid, token)
186+
print("Connecting to WebSocket " + ws_address + " ...")
187+
web_socket_app = websocket.WebSocketApp(ws_address, header=['User-Agent: Python'],
188+
on_message=on_message,
189+
on_error=on_error,
190+
on_close=on_close,
191+
subprotocols=[ws_protocol])
192+
web_socket_app.on_open = on_open
193+
194+
# Event loop
195+
wst = threading.Thread(target=web_socket_app.run_forever)
196+
wst.start()
197+
198+
try:
199+
while True:
200+
time.sleep(1)
201+
except KeyboardInterrupt:
202+
web_socket_app.close()

0 commit comments

Comments
 (0)