Skip to content

Commit c8062cc

Browse files
author
Wasin Waeosri
committed
Merge branch 'version2_rest_quote'
2 parents ff8afb4 + c488b05 commit c8062cc

File tree

5 files changed

+212
-84
lines changed

5 files changed

+212
-84
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Introduction
2+
This is an example project that shows how to implement TRKD REST Client with python
3+
- trkd_authen.py: An example application that shows how to authenticate with TRKD service
4+
- trkd_quote.py: An example application that shows how to subscribe (all fields and specific fields) the Quote data from TRKD service
5+
6+
7+
#prerequisite
8+
The following softwares are required to use this script
9+
- Python 2.7.10
10+
- The [requests](http://docs.python-requests.org/en/master/) library
11+
12+
The script does not support Python 3!
13+
14+
#how to run the script
15+
Run the script via the command line (or shell)
16+
```
17+
$>python <application>.py
18+
```
19+
20+
21+
#Optional - How to install requests
22+
The best way is to get the pip package management tool
23+
1. export <Python_folder>\Scripts to your OS PATH environment
24+
2. call pip command to install lxml
25+
```
26+
$>pip install requests
27+
```
28+
3. If you are behind proxy, set the proxy first
29+
```
30+
export https_proxy="http://<proxy.server>:<port>"
31+
$>pip install requests
32+
```
33+
#Releae Note
34+
- Version 1: 6 Sep 2016
35+
- trkd_authen.py
36+
- trkd_quote.py

note.txt

Lines changed: 0 additions & 73 deletions
This file was deleted.

testpy.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

trkd_authen.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
11+
import os
12+
import sys
13+
import requests
14+
import json
15+
import getpass
16+
17+
##Get username, password and applicationid
18+
username = raw_input('Please input username: ')
19+
##use getpass.getpass to hide user inputted password
20+
password = getpass.getpass(prompt='Please input password: ')
21+
appid = raw_input('Please input appid: ')
22+
23+
print '############### Sending Authentication request message to TRKD ###############'
24+
25+
##create authentication request URL, message and header
26+
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
27+
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
28+
headers = {'content-type': 'application/json;charset=utf-8'}
29+
30+
try:
31+
##send request
32+
result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)
33+
if result.status_code == 200:
34+
print 'Request success'
35+
print 'response status %s'%(result.status_code)
36+
##get Token
37+
token = result.json()['CreateServiceToken_Response_1']['Token']
38+
print 'Token: %s'%(token)
39+
##get expiraion
40+
expire = result.json()['CreateServiceToken_Response_1']['Expiration']
41+
print 'Exipre: %s'%(expire)
42+
elif result.status_code == 500:
43+
print 'Request fail'
44+
print 'response status %s'%(result.status_code)
45+
print 'Error: %s'%(result.json())
46+
except requests.exceptions.RequestException as e:
47+
print 'Exception!!!'
48+
print e
49+
sys.exit(1)
50+
51+
52+
53+
54+
55+

trkd_quote.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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

Comments
 (0)