Skip to content

Commit cbd81e6

Browse files
author
Wasin Waeosri
committed
Merge branch 'v13_modulequote'
2 parents 36e6bae + 5997494 commit cbd81e6

File tree

3 files changed

+91
-65
lines changed

3 files changed

+91
-65
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
#Introduction
2-
This is an example project that shows how to implement TRKD REST Client with python
1+
# TRKD HTTP JSON with Python Example
2+
## Overview
3+
The [Thomson Reuters Knowledge Direct (TRKD) API](https://developers.thomsonreuters.com/thomson-reuters-knowledge-direct-trkd) integrates into your website, trading platform, company intranet/extranet, advisory portal and mobile applications to provide up-to-date financial market data, news and analytics and powerful investment tools.
4+
5+
TRKD offers a wide range of Thomson Reuters' information and services delivered in a request-response scenario via web services using today's industry standard protocols (SOAP/XML and REST/JSON). Connectivity can be via HTTP and HTTPS, over the Internet or Delivery Direct.Level 2. All data are snapshot (non-streaming) data.
6+
7+
This is an example project that shows how to implement TRKD HTTP JSON Client with python. This project contains the following example scripts for each TRKD services
38
- trkd_authen.py: An example application that shows how to authenticate with TRKD service
49
- trkd_quote.py: An example application that shows how to subscribe (all fields and specific fields) the Quote data from TRKD service
510
- trkd_newsheadline.py: An example application that shows how to subscribe the News Headline data from TRKD service
@@ -11,20 +16,20 @@ This is an example project that shows how to implement TRKD REST Client with pyt
1116
- docs\TRKD_REST_with_Python.docx: A document that describes the trkd_authen.py and trkd_quote.py applications
1217

1318

14-
#prerequisite
19+
## Prerequisite
1520
The following softwares are required to use this script
16-
- Python 2.7.10
21+
- Python 2.7.10 or above
1722
- The [requests](http://docs.python-requests.org/en/master/) library
1823

19-
The script does not support Python 3!
24+
The scripts are based on Python 2 but you can modify it to run with Python 3 (see "Optional - How to run with Python 3" section).
2025

21-
#how to run the script
26+
## How to run the script
2227
Run the script via the command line (or shell)
2328
```
2429
$>python <application>.py
2530
```
2631

27-
#Optional - How to install requests
32+
## Optional - How to install requests
2833
The best way is to get the pip package management tool
2934
1. export <Python_folder>\Scripts to your OS PATH environment
3035
2. call pip command to install requests
@@ -36,7 +41,18 @@ The best way is to get the pip package management tool
3641
export https_proxy="http://<proxy.server>:<port>"
3742
$>pip install requests
3843
```
39-
#Releae Note
44+
## Optional - How to run with Python 3
45+
You can modify the scripts to run with Python 3 (with requests library installed) by just change the code from "**raw_input()**" to "**input()**" as the following example
46+
- Python 2
47+
```
48+
username = raw_input('Please input username: ')
49+
```
50+
- Python 3
51+
```
52+
username = input('Please input username: ')
53+
```
54+
55+
## Release Note
4056
- Version 1: 6 Sep 2016
4157
- trkd_authen.py
4258
- trkd_quote.py
@@ -53,4 +69,10 @@ The best way is to get the pip package management tool
5369
- version 1.0.4: 28 Oct 2016
5470
- docs\TRKD_REST_with_Python.docx
5571
- revise some code
72+
- version 1.0.5: 27 Apr 2017
73+
- revies README.md to support markdown
74+
- version 1.0.6: 3 May 2017
75+
- revies README.md
76+
- modify trkd_authen.py
77+
- modify trkd_quote.py
5678

trkd_authen.py

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,49 @@
77
Please see the Thomson Reuters Knowledge Direct product page at http://customers.thomsonreuters.com
88
for additional information regarding the TRKD API.'''
99

10-
11-
import os
1210
import sys
13-
import requests
1411
import json
1512
import getpass
13+
import requests
14+
1615

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)
16+
if __name__ == '__main__':
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+
print('############### Sending Authentication request message to TRKD ###############')
23+
24+
##create authentication request URL, message and header
25+
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
26+
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
27+
headers = {'content-type': 'application/json;charset=utf-8'}
28+
29+
try:
30+
## send request
31+
result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)
32+
## request success
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('Expire: %s'%(expire))
42+
## handle error
43+
else:
44+
print('Request fail')
45+
print('response status %s'%(result.status_code))
46+
if result.status_code == 500: ## if username or password or appid is wrong
47+
print('Error: %s'%(result.json()))
48+
result.raise_for_status()
49+
except requests.exceptions.RequestException as e:
50+
print('Exception!!!')
51+
print(e)
52+
sys.exit(1)
5053

5154

5255

trkd_quote.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ def doSendRequest(url, requestMsg, headers):
2121
##send request
2222
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
2323
if result.status_code == 500:
24-
print 'Request fail'
25-
print 'response status %s' % result.status_code
26-
print 'Error: %s' % result.json()
24+
print('Request fail')
25+
print('response status %s' % result.status_code)
26+
print('Error: %s' % result.json())
2727
sys.exit(1)
2828
except requests.exceptions.RequestException, e:
29-
print 'Exception!!!'
30-
print e
29+
print('Exception!!!')
30+
print(e)
3131
sys.exit(1)
3232
return result
3333

@@ -39,11 +39,11 @@ def CreateAuthorization(username, password, appid):
3939
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
4040
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
4141
headers = {'content-type': 'application/json;charset=utf-8'}
42-
print '############### Sending Authentication request message to TRKD ###############'
42+
print('############### Sending Authentication request message to TRKD ###############')
4343
authenResult = doSendRequest(authenURL, authenMsg, headers)
4444
if authenResult is not None and authenResult.status_code == 200:
45-
print 'Authen success'
46-
print 'response status %s'%(authenResult.status_code)
45+
print('Authen success')
46+
print('response status %s'%(authenResult.status_code))
4747
##get Token
4848
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
4949

@@ -77,26 +77,27 @@ def RetrieveQuotes(token, appid):
7777
quoteURL = 'https://api.trkd.thomsonreuters.com/api/Quotes/Quotes.svc/REST/Quotes_1/RetrieveItem_3'
7878
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
7979

80-
print '############### Sending Quote request message to TRKD ###############'
80+
print('############### Sending Quote request message to TRKD ###############')
8181
quoteResult = doSendRequest(quoteURL, quoteRequestMsg,headers)
8282
if quoteResult is not None and quoteResult.status_code == 200:
8383
print 'Quote response message: '
8484
print quoteResult.json()
8585

8686

8787
## ------------------------------------------ Main App ------------------------------------------ ##
88-
##Get username, password and applicationid
89-
username = raw_input('Please input username: ')
90-
##use getpass.getpass to hide user inputted password
91-
password = getpass.getpass(prompt='Please input password: ')
92-
appid = raw_input('Please input appid: ')
93-
94-
95-
token = CreateAuthorization(username,password,appid)
96-
print 'Token = %s'%(token)
97-
## if authentiacation success, continue subscribing Quote
98-
if token is not None:
99-
RetrieveQuotes(token,appid)
88+
89+
if __name__ == '__main__':
90+
## Get username, password and applicationid
91+
username = raw_input('Please input username: ')
92+
## use getpass.getpass to hide user inputted password
93+
password = getpass.getpass(prompt='Please input password: ')
94+
appid = raw_input('Please input appid: ')
95+
96+
token = CreateAuthorization(username,password,appid)
97+
print('Token = %s'%(token))
98+
## if authentiacation success, continue subscribing Quote
99+
if token is not None:
100+
RetrieveQuotes(token,appid)
100101

101102

102103

0 commit comments

Comments
 (0)