How to implement TRKD JSON application with Python chapter 1: the basic

Overview

The Thomson Reuters Knowledge Direct (TRKD) API 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.

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.

With new HTTP JSON services, developers can integrate TRKD information to their application easier then before. This article provides an information regarding the basic knowledge of how to implement TRKD JSON application with Python language.

TRKD JSON application implementation process

The JSON application requires the following steps to consume data from TRKD API services

  1. Authentication with TRKD Authentication service to get an authen token
  2. Send a request message with the required input information and authen token to the interested TRKD service

Authentication Process

The consumer application needs to authenticate with TRKD Service Token with the following information

  • Username
  • Application ID (AppID)
  • Password

Once the application is permissioned success, the application gets the following information from TRKD

  • Token: an encrypted, expiring string that securely identifies the service user (aka service Token)
  • Expiration: Token expires after a configurable time period. The default expiration is 90 minutes.

Subscribe Data Process

After the application get the Service Token, the application can subscribes data to the TRKD service. The application needs to set the Application ID and service Token with the request message HTTP header to identify the permissioned.

  • X-Trkd-Auth-ApplicationID: Application ID
  • X-Trkd-Auth-Token: service Token

Data Format

All request and response messages of the TRKD REST Services are in the JSON format. We will use the Python json library to encode the request message via the HTTP Post request. For the response message, we will use the JSON builtin encoder of the requests library to decode incoming JSON data.

{
  "CreateServiceToken_Response_1": {
    "Expiration": "2017-05-09T08:55:52.6017Z",
    "Token": "AAA"
  }
}

example 1: Example JSON message response for TRKD Authenticacion CreateServiceToken_1 service

"trkd_authen.py" Example Application

TRKD API uses an encrypted, expiring token to authenticate each request. To create this token, your application submits credentials to the Token Management service. The Token Management service authenticates the credentials and returns the token.

This section describes how to implement the trkd_authen.py script that performs authentication with the TRDK API with the inputted username, password and appid via the command line.

TRKD Sservice Token Detail

TRKD Service Token URL and Header

The URL enponint for the TRKD Service Token is following: https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1

Header:

  • Content-type = application/json;charset=utf-8

Method:

  • Post
TRKD Service TOKEN Request Message

The CreateServiceToken_1 operation requires the following information to perform authentication

  • ApplicationID
  • Username
  • Password

The request message structure is following

{
    “CreateServiceToken_Request_1”:{
      “ApplicationID”: <application id>,
      “Username”: <username>,
      “Password”: <password>
   }
}

The example of the response message is shown below

{
  "CreateServiceToken_Response_1": {
    "Expiration": "2016-09-26T09:42:54.4335265Z",
    "Token": "674E12E4EF35F181602672D5529D98379D4B42216057C7FF…"
  }
}

IMPLEMENTATION DETAILS

  1. Firstly, we create a file named “trkd_authen.py” in the working directory. Then we import all required libraries at the top of the source code. 
    import os
    import sys
    import requests
    import json
    import getpass

     

  2. Then we add the code to receive the inputted username, password and application id from the console. 

    ##Get username, password and applicationid
    username = input('Please input username: ')
    ##use getpass.getpass to hide user inputted password
    password = getpass.getpass(prompt='Please input password: ')
    appid = input('Please input appid: ')

     

  3. Next, we create the request message (authenMsg), the url (authenURL) and headers variables. 

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

     

  4. Then we create a HTTP Post request message to the TRKD Service Token endpoint that specified in the authenURL variable with the authenMsg request message. We use the requests.post function of the requests library to handle this operation. 

    ## send request
    result = requests.post(authenURL, data = json.dumps(authenMsg), headers=headers)

     

  5. Finally, we check if the status code is 200 (OK), prints out the Token and Expiration. Alterwise, prints out "request fail message" and raise an exception. If the return status code is 500, prints out the whole return JSON message. 

    ## request success
    if result.status_code == 200:
        print('Request success')
        print('response status %s'%(result.status_code))
        ## get Token
        token = result.json()['CreateServiceToken_Response_1']['Token']
        print('Token: %s'%(token))
        ## get expiraion
        expire = result.json()['CreateServiceToken_Response_1']['Expiration']
        print('Expire: %s'%(expire))
    ## handle error
    else:
        print('Request fail')
        print('response status %s'%(result.status_code))
        if result.status_code == 500: ## if username or password or appid is wrong
            print('Error: %s'%(result.json()))
        result.raise_for_status()

     

Running the application

The application source code is available at GitHub. You can get it via the following git command

$>git clone git@github.com:TR-API-Samples/Example.TRKD.Python.HTTPJSON.git

All TRKD HTTP JSON python examples support Python 3 only and they require the the requests library, please check the README.md in the source code project for more detail.

You can run trkd_authen.py application via the following command

$>python trkd_authen.py

Then input your username, password and application id as shown in the Figure-1 below.

 

Figure-1: The trkd_authen receives username, password and appid via commandline

The result is shown in the Figure-2 below

Figure-2: The Authentication result from TRKD Service Token endpoint

Please note that the application must keeps the application id and this service Token for futher operations.

Conclusion

All TRKD HTTP JSON applications require authenticaiton to access TRKD data. The application needs to request for Service Token from TRKD server, then keeps a response Service Token for later use in other request message header.

References

For further details, please check out the following resources: