Skip to content

Commit a9d9bce

Browse files
author
Wasin Waeosri
committed
Merge branch 'jsonformatter'
Update thomson reuters -> refintiv Update JSON message format Update time-series request message to be full message
2 parents 33896d0 + 40c29aa commit a9d9bce

File tree

11 files changed

+163
-114
lines changed

11 files changed

+163
-114
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ note.txt
55
test.py
66
market_price_edpgw_authentication.py
77
.vscode/
8+
interday_result.txt
9+
interday_result2.txt
10+
interday_result3.txt
11+
interday_result4.txt

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Overview
33
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.
44

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. All data are snapshot (non-streaming) data.
5+
TRKD offers a wide range of Refinitiv' 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. All data are snapshot (non-streaming) data.
66

77
This is an example project that shows how to implement TRKD HTTP JSON client and TRKD Streaming client with Python programming lanugage. This project contains the following example scripts for each TRKD services
88
- trkd_authen.py: An example application that shows how to authenticate with TRKD service
@@ -93,3 +93,5 @@ For further details, please check out the following resources:
9393
- version 1.0.11: January 2019
9494
- Add trkd_wsstreaming.py application for TRKD Streaming service.
9595
- Add License.md file
96+
- version 1.0.12: March 2019
97+
- Change all scripts to print JSON message in beauty format.

trkd_authen.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
We offer this code to provide developers practical and useful guidance while developing their own code.
55
However, we do not offer support and troubleshooting of issues that are related to the use of this code
66
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
7+
Please see the Thomson Reuters Knowledge Direct product page at https://my.refinitiv.com
88
for additional information regarding the TRKD API.'''
99

1010
import sys
@@ -14,39 +14,44 @@
1414

1515

1616
if __name__ == '__main__':
17-
## Get username, password and applicationid
17+
# Get username, password and applicationid
1818
username = input('Please input username: ')
19-
## use getpass.getpass to hide user inputted password
19+
# use getpass.getpass to hide user inputted password
2020
password = getpass.getpass(prompt='Please input password: ')
2121
appid = input('Please input appid: ')
2222
print('############### Sending Authentication request message to TRKD ###############')
2323

24-
##create authentication request URL, message and header
25-
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
24+
# create authentication request URL, message and header
25+
authenMsg = {'CreateServiceToken_Request_1': {
26+
'ApplicationID': appid, 'Username': username, 'Password': password}}
2627
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
2728
headers = {'content-type': 'application/json;charset=utf-8'}
2829

2930
try:
30-
## send request
31-
result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)
32-
## request success
31+
# send request
32+
result = requests.post(
33+
authenURL, data=json.dumps(authenMsg), headers=headers)
34+
# request success
3335
if result.status_code == 200:
3436
print('Request success')
35-
print('response status %s'%(result.status_code))
36-
##get Token
37+
print('response status %s' % (result.status_code))
38+
# get Token
3739
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
40+
print('Token: %s' % (token))
41+
# get expiraion
42+
expire = result.json()[
43+
'CreateServiceToken_Response_1']['Expiration']
44+
print('Expire: %s' % (expire))
45+
# handle error
4346
else:
4447
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+
print('response status %s' % (result.status_code))
49+
if result.status_code == 500: # if username or password or appid is wrong
50+
#print('Error: %s' % (result.json()))
51+
print('Error: %s' % (json.dumps(result.json(),
52+
sort_keys=True, indent=2, separators=(',', ':'))))
4853
result.raise_for_status()
4954
except requests.exceptions.RequestException as e:
5055
print('Exception!!!')
5156
print(e)
52-
sys.exit(1)
57+
sys.exit(1)

trkd_chart.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
We offer this code to provide developers practical and useful guidance while developing their own code.
55
However, we do not offer support and troubleshooting of issues that are related to the use of this code
66
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
7+
Please see the Thomson Reuters Knowledge Direct product page at https://my.refinitiv.com
88
for additional information regarding the TRKD API.'''
99

1010

@@ -27,7 +27,8 @@ def doSendRequest(url, requestMsg, headers):
2727
print('Request fail')
2828
print('response status %s'%(result.status_code))
2929
if result.status_code == 500: ## if username or password or appid is wrong
30-
print('Error: %s'%(result.json()))
30+
#print('Error: %s'%(result.json()))
31+
print('Error: %s' % (json.dumps(result.json(),sort_keys=True, indent=2, separators=(',', ':'))))
3132
result.raise_for_status()
3233
except requests.exceptions.RequestException as e:
3334
print('Exception!!!')

trkd_interday.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
We offer this code to provide developers practical and useful guidance while developing their own code.
55
However, we do not offer support and troubleshooting of issues that are related to the use of this code
66
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
7+
Please see the Thomson Reuters Knowledge Direct product page at https://my.refinitiv.com
88
for additional information regarding the TRKD API.'''
99

1010

@@ -26,7 +26,8 @@ def doSendRequest(url, requestMsg, headers):
2626
print('Request fail')
2727
print('response status %s'%(result.status_code))
2828
if result.status_code == 500: ## if username or password or appid is wrong
29-
print('Error: %s'%(result.json()))
29+
#print('Error: %s'%(result.json()))
30+
print('Error: %s' % (json.dumps(result.json(),sort_keys=True, indent=2, separators=(',', ':'))))
3031
result.raise_for_status()
3132
except requests.exceptions.RequestException as e:
3233
print('Exception!!!')
@@ -65,12 +66,11 @@ def RetrieveInteraday(token, appid):
6566
interdayRequestMsg = {
6667
'GetInterdayTimeSeries_Request_4':{
6768
'Field': fields,
68-
'TrimResponse': True,
69+
'TrimResponse': False,
6970
'Symbol': ricName,
7071
'StartTime':startTime,
7172
'EndTime':endtime,
7273
'Interval':interval,
73-
'TrimResponse': True,
7474
'MetaField': ['NAME','QOS','CCY','TZ','TZOFFSET','NAME_LL']
7575
}
7676
}
@@ -83,7 +83,9 @@ def RetrieveInteraday(token, appid):
8383
interdayResult = doSendRequest(interdayURL, interdayRequestMsg, headers)
8484
if interdayResult and interdayResult.status_code == 200:
8585
print('Time Series Interday response message: ')
86-
print(interdayResult.json())
86+
#print(interdayResult.json())
87+
print(json.dumps(interdayResult.json(), sort_keys=True, indent=2, separators=(',', ':')))
88+
8789

8890

8991
## ------------------------------------------ Main App ------------------------------------------ ##
@@ -97,5 +99,5 @@ def RetrieveInteraday(token, appid):
9799
token = CreateAuthorization(username, password, appid)
98100
print('Token = %s'%(token))
99101
## if authentiacation success, continue subscribing Time Series interday
100-
if token is not None:
102+
if token:
101103
RetrieveInteraday(token, appid)

trkd_intraday.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
We offer this code to provide developers practical and useful guidance while developing their own code.
55
However, we do not offer support and troubleshooting of issues that are related to the use of this code
66
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
7+
Please see the Thomson Reuters Knowledge Direct product page at https://my.refinitiv.com
88
for additional information regarding the TRKD API.'''
99

1010

@@ -29,7 +29,9 @@ def doSendRequest(url, requestMsg, headers):
2929
print('Request fail')
3030
print('response status %s' % (result.status_code))
3131
if result.status_code == 500: # if username or password or appid is wrong
32-
print('Error: %s' % (result.json()))
32+
#print('Error: %s' % (result.json()))
33+
print('Error: %s' % (json.dumps(result.json(),
34+
sort_keys=True, indent=2, separators=(',', ':'))))
3335
result.raise_for_status()
3436
except requests.exceptions.RequestException as e:
3537
print('Exception!!!')
@@ -74,12 +76,11 @@ def RetrieveIntraday(token, appid):
7476
intradayRequestMsg = {
7577
'GetIntradayTimeSeries_Request_4': {
7678
'Field': fields,
77-
'TrimResponse': True,
79+
'TrimResponse': False,
7880
'Symbol': ricName,
7981
'StartTime': startTime,
8082
'EndTime': endtime,
8183
'Interval': interval,
82-
'TrimResponse': True,
8384
'MetaField': ['NAME', 'QOS', 'CCY', 'TZ', 'TZOFFSET', 'NAME_LL']
8485
}
8586
}
@@ -92,7 +93,9 @@ def RetrieveIntraday(token, appid):
9293
intradayResult = doSendRequest(intradayURL, intradayRequestMsg, headers)
9394
if intradayResult and intradayResult.status_code == 200:
9495
print('Time Series Intraday response message: ')
95-
print(intradayResult.json())
96+
# print(intradayResult.json())
97+
print(json.dumps(intradayResult.json(),
98+
sort_keys=True, indent=2, separators=(',', ':')))
9699

97100

98101
## ------------------------------------------ Main App ------------------------------------------ ##

trkd_newsheadline.py

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
We offer this code to provide developers practical and useful guidance while developing their own code.
55
However, we do not offer support and troubleshooting of issues that are related to the use of this code
66
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
7+
Please see the Thomson Reuters Knowledge Direct product page at https://my.refinitiv.com
88
for additional information regarding the TRKD API.'''
99

1010
import os
@@ -14,18 +14,23 @@
1414
import getpass
1515

1616
# Send HTTP request for all services
17+
18+
1719
def doSendRequest(url, requestMsg, headers):
1820
result = None
1921
try:
20-
##send request
21-
result = requests.post(url, data=json.dumps(requestMsg), headers=headers)
22-
# print('outgoing message is %s'%(json.dumps(requestMsg)))
23-
## handle error
22+
# send request
23+
result = requests.post(
24+
url, data=json.dumps(requestMsg), headers=headers)
25+
# print('outgoing message is %s'%(json.dumps(requestMsg)))
26+
# handle error
2427
if result.status_code is not 200:
2528
print('Request fail')
26-
print('response status %s'%(result.status_code))
27-
if result.status_code == 500: ## if username or password or appid is wrong
28-
print('Error: %s'%(result.json()))
29+
print('response status %s' % (result.status_code))
30+
if result.status_code == 500: # if username or password or appid is wrong
31+
#print('Error: %s'%(result.json()))
32+
print('Error: %s' % (json.dumps(result.json(),
33+
sort_keys=True, indent=2, separators=(',', ':'))))
2934
result.raise_for_status()
3035
except requests.exceptions.RequestException as e:
3136
print('Exception!!!')
@@ -34,39 +39,43 @@ def doSendRequest(url, requestMsg, headers):
3439
return result
3540

3641

37-
## Perform authentication
42+
# Perform authentication
3843
def CreateAuthorization(username, password, appid):
3944
token = None
40-
##create authentication request URL, message and header
41-
authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }}
45+
# create authentication request URL, message and header
46+
authenMsg = {'CreateServiceToken_Request_1': {
47+
'ApplicationID': appid, 'Username': username, 'Password': password}}
4248
authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1'
4349
headers = {'content-type': 'application/json;charset=utf-8'}
4450
print('############### Sending Authentication request message to TRKD ###############')
4551
authenResult = doSendRequest(authenURL, authenMsg, headers)
4652
if authenResult and authenResult.status_code == 200:
4753
print('Authen success')
48-
print('response status %s'%(authenResult.status_code))
49-
##get Token
54+
print('response status %s' % (authenResult.status_code))
55+
# get Token
5056
token = authenResult.json()['CreateServiceToken_Response_1']['Token']
5157

5258
return token
5359

54-
## Perform News Headline request
60+
# Perform News Headline request
61+
62+
5563
def RetrieveNewsHeadline(token, appid):
56-
##construct news headline URL and header
64+
# construct news headline URL and header
5765
newsURL = 'https://api.trkd.thomsonreuters.com/api/News/News.svc/REST/News_1/RetrieveHeadlineML_1'
58-
headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token}
59-
##construct a news headline request message
66+
headers = {'content-type': 'application/json;charset=utf-8',
67+
'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token}
68+
# construct a news headline request message
6069
ricName = input('Please input Symbol: ')
6170
newsRequestMsg = {'RetrieveHeadlineML_Request_1': {
62-
'HeadlineMLRequest':{
63-
'MaxCount':10,
64-
'Filter':[
71+
'HeadlineMLRequest': {
72+
'MaxCount': 10,
73+
'Filter': [
6574
{
66-
'MetaDataConstraint':{
75+
'MetaDataConstraint': {
6776
'class': 'any',
6877
'Value': {
69-
'Text' : ricName
78+
'Text': ricName
7079
}
7180
}
7281
}
@@ -75,25 +84,25 @@ def RetrieveNewsHeadline(token, appid):
7584
}}
7685

7786
print('############### Sending News Headline request message to TRKD ###############')
78-
newsResult = doSendRequest(newsURL, newsRequestMsg,headers)
87+
newsResult = doSendRequest(newsURL, newsRequestMsg, headers)
7988
if newsResult and newsResult.status_code == 200:
8089
print('News Headline response message: ')
81-
print(newsResult.json())
82-
90+
# print(newsResult.json())
91+
print(json.dumps(newsResult.json(), sort_keys=True,
92+
indent=2, separators=(',', ':')))
8393

8494

8595
## ------------------------------------------ Main App ------------------------------------------ ##
86-
8796
if __name__ == '__main__':
88-
##Get username, password and applicationid
97+
# Get username, password and applicationid
8998
username = input('Please input username: ')
90-
##use getpass.getpass to hide user inputted password
99+
# use getpass.getpass to hide user inputted password
91100
password = getpass.getpass(prompt='Please input password: ')
92101
appid = input('Please input appid: ')
93102

94-
token = CreateAuthorization(username,password,appid)
95-
print('Token = %s'%(token))
103+
token = CreateAuthorization(username, password, appid)``
104+
print('Token = %s' % (token))
96105

97-
## if authentiacation success, continue subscribing News Headline
106+
# if authentiacation success, continue subscribing News Headline
98107
if token:
99-
RetrieveNewsHeadline(token,appid)
108+
RetrieveNewsHeadline(token, appid)

0 commit comments

Comments
 (0)