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+ The TRKD API sample code is provided for informational purposes only
11+ and without knowledge or assumptions of the end users development environment.
12+ We offer this code to provide developers practical and useful guidance while developing their own code.
13+ However, we do not offer support and troubleshooting of issues that are related to the use of this code
14+ in a particular environment; it is offered solely as sample code for guidance.
15+ Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com
16+ for additional information regarding the TRKD API.'''
17+
18+ import os
19+ import sys
20+ import requests
21+ import json
22+ import getpass
23+
24+ ## Perform authentication
25+ def CreateAuthorization (username , password , appid ):
26+ token = None
27+ ##create authentication request URL, message and header
28+ authenMsg = {'CreateServiceToken_Request_1' : { 'ApplicationID' :appid , 'Username' :username ,'Password' :password }}
29+ authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
30+ headers = {'content-type' : 'application/json;charset=utf-8' }
31+ print '############### Sending Authentication request message to TRKD ###############'
32+ try :
33+ ##send request
34+ result = requests .post (authenURL , data = json .dumps (authenMsg ), headers = headers )
35+ if result .status_code == 200 :
36+ print 'Authen success'
37+ print 'response status %s' % (result .status_code )
38+ ##get Token
39+ token = result .json ()['CreateServiceToken_Response_1' ]['Token' ]
40+ elif result .status_code == 500 :
41+ print 'Request fail'
42+ print 'response status %s' % (result .status_code )
43+ print 'Error: %s' % (result .json ())
44+ except requests .exceptions .RequestException as e :
45+ print 'Exception!!!'
46+ print e
47+ sys .exit (1 )
48+
49+ return token
50+
51+ ## Perform Quote request
52+ def RetrieveQuotes (token , appid ):
53+
54+ ricName = raw_input ('Please input Symbol: ' )
55+ fieldFiltering = raw_input ('Subscribe all Field? (Yes|No)' )
56+ quoteRequestMsg = None
57+ fieldsName = 'CF_LAST:CF_HIGH:CF_LOW:CF_BID:CF_ASK:CF_YIELD:CF_SOURCE:CF_SRC_PAGE:CF_LOTSIZE:CF_DATE:CF_TIME:CF_TICK:CF_NETCHNG:CF_EXCHNG:CF_VOLUME:CF_CLOSE:CF_OPEN:CF_NAME:CF_CURRENCY'
58+ if fieldFiltering == 'Yes' :
59+ ## Request all Fields
60+ quoteRequestMsg = \
61+ {'RetrieveItem_Request_3' : {'TrimResponse' : False ,
62+ 'ItemRequest' : [{'RequestKey' : [{'Name' : ricName , 'NameType' : 'RIC' }], 'Scope' : 'All' ,
63+ 'ProvideChainLinks' : True }]}}
64+ elif fieldFiltering == 'No' :
65+ ## Request specific Fields
66+ fieldsName = raw_input ('Input interested Field Name in the following format (BID:ASK:TRDPRC_1)' )
67+ quoteRequestMsg = \
68+ {'RetrieveItem_Request_3' : {'TrimResponse' : False ,
69+ 'ItemRequest' : [{
70+ 'RequestKey' : [{'Name' : ricName , 'NameType' : 'RIC' }],
71+ 'Fields' : fieldsName ,
72+ 'Scope' : 'List' ,
73+ 'ProvideChainLinks' : True ,
74+ }]}}
75+
76+ quoteURL = 'https://api.trkd.thomsonreuters.com/api/Quotes/Quotes.svc/REST/Quotes_1/RetrieveItem_3'
77+ headers = {'content-type' : 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID' : appid , 'X-Trkd-Auth-Token' : token }
78+
79+ print '############### Sending Quote request message to TRKD ###############'
80+ try :
81+ ##send request
82+ quoteresult = requests .post (quoteURL , data = json .dumps (quoteRequestMsg ), headers = headers )
83+ if quoteresult .status_code == 200 :
84+ print 'Quote request success'
85+ print quoteresult .json ()
86+ else :
87+ print 'Request fail'
88+ print 'response status %s' % (quoteresult .status_code )
89+ print 'Error: %s' % (quoteresult .json ())
90+
91+ except requests .exceptions .RequestException as e :
92+ print 'Exception!!!'
93+ print e
94+ sys .exit (1 )
95+
96+
97+ ## ------------------------------------------ Main App ------------------------------------------ ##
98+ ##Get username, password and applicationid
99+ username = raw_input ('Please input username: ' )
100+ ##use getpass.getpass to hide user inputted password
101+ password = getpass .getpass (prompt = 'Please input password: ' )
102+ appid = raw_input ('Please input appid: ' )
103+
104+ token = CreateAuthorization (username ,password ,appid )
105+ print 'Token = %s' % (token )
106+
107+ RetrieveQuotes (token ,appid )
108+
109+
110+
111+
112+
113+
114+
115+
116+
117+
118+
119+
120+
121+
0 commit comments