|
13 | 13 | import json |
14 | 14 | import getpass |
15 | 15 |
|
| 16 | +# Send HTTP request for all services |
16 | 17 | def doSendRequest(url, requestMsg, headers): |
17 | 18 | result = None |
18 | 19 | try: |
19 | | - ##send request |
20 | | - result = requests.post(url, data=json.dumps(requestMsg), headers=headers) |
21 | | - if result.status_code == 500: |
22 | | - print 'Request fail' |
23 | | - print 'response status %s' % result.status_code |
24 | | - print 'Error: %s' % result.json() |
25 | | - sys.exit(1) |
| 20 | + # send request |
| 21 | + result = requests.post( |
| 22 | + url, data=json.dumps(requestMsg), headers=headers) |
| 23 | + # handle error |
| 24 | + if result.status_code is not 200: |
| 25 | + 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 | + result.raise_for_status() |
26 | 30 | except requests.exceptions.RequestException, e: |
27 | | - print 'Exception!!!' |
28 | | - print e |
| 31 | + print('Exception!!!') |
| 32 | + print(e) |
29 | 33 | sys.exit(1) |
30 | 34 | return result |
31 | 35 |
|
32 | 36 |
|
33 | | -## Perform authentication |
| 37 | +# Perform authentication |
34 | 38 | def CreateAuthorization(username, password, appid): |
35 | 39 | token = None |
36 | | - ##create authentication request URL, message and header |
37 | | - authenMsg = {'CreateServiceToken_Request_1': { 'ApplicationID':appid, 'Username':username,'Password':password }} |
| 40 | + # create authentication request URL, message and header |
| 41 | + authenMsg = {'CreateServiceToken_Request_1': { |
| 42 | + 'ApplicationID': appid, 'Username': username, 'Password': password}} |
38 | 43 | authenURL = 'https://api.trkd.thomsonreuters.com/api/TokenManagement/TokenManagement.svc/REST/Anonymous/TokenManagement_1/CreateServiceToken_1' |
39 | 44 | headers = {'content-type': 'application/json;charset=utf-8'} |
40 | 45 | print '############### Sending Authentication request message to TRKD ###############' |
41 | 46 | authenResult = doSendRequest(authenURL, authenMsg, headers) |
42 | 47 | if authenResult is not None and authenResult.status_code == 200: |
43 | | - print 'Authen success' |
44 | | - print 'response status %s'%(authenResult.status_code) |
45 | | - ##get Token |
| 48 | + print('Authen success') |
| 49 | + print('response status %s' % (authenResult.status_code)) |
| 50 | + # get Token |
46 | 51 | token = authenResult.json()['CreateServiceToken_Response_1']['Token'] |
47 | | - |
| 52 | + |
48 | 53 | return token |
49 | 54 |
|
50 | | -## Perform Online Report request |
| 55 | +# Perform Online Report request |
| 56 | + |
| 57 | + |
51 | 58 | def RetrieveOnlineReport(token, appid): |
52 | | - ##construct Online Report URL and header |
| 59 | + # construct Online Report URL and header |
53 | 60 | onlinereportURL = 'http://api.rkd.reuters.com/api/OnlineReports/OnlineReports.svc/REST/OnlineReports_1/GetSummaryByTopic_1' |
54 | | - headers = {'content-type': 'application/json;charset=utf-8' ,'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token' : token} |
55 | | - ##construct a Online Report request message |
| 61 | + headers = {'content-type': 'application/json;charset=utf-8', |
| 62 | + 'X-Trkd-Auth-ApplicationID': appid, 'X-Trkd-Auth-Token': token} |
| 63 | + # construct a Online Report request message |
56 | 64 | onelinereportRequestMsg = {'GetSummaryByTopic_Request_1': { |
57 | 65 | 'Topic': 'OLRUTOPNEWS', |
58 | 66 | 'MaxCount': 20, |
59 | 67 | 'ReturnPrivateNetworkURL': False |
60 | | - } |
61 | 68 | } |
62 | | - print '############### Sending News - Online Report request message to TRKD ###############' |
63 | | - onlinereportResult = doSendRequest(onlinereportURL, onelinereportRequestMsg,headers) |
| 69 | + } |
| 70 | + print('############### Sending News - Online Report request message to TRKD ###############') |
| 71 | + onlinereportResult = doSendRequest( |
| 72 | + onlinereportURL, onelinereportRequestMsg, headers) |
64 | 73 | if onlinereportResult is not None and onlinereportResult.status_code == 200: |
65 | | - print 'Online Report response message: ' |
66 | | - print onlinereportResult.json() |
| 74 | + print('Online Report response message: ') |
| 75 | + print(onlinereportResult.json()) |
67 | 76 |
|
68 | 77 |
|
69 | 78 |
|
70 | 79 | ## ------------------------------------------ Main App ------------------------------------------ ## |
71 | | -##Get username, password and applicationid |
72 | | -username = raw_input('Please input username: ') |
73 | | -##use getpass.getpass to hide user inputted password |
74 | | -password = getpass.getpass(prompt='Please input password: ') |
75 | | -appid = raw_input('Please input appid: ') |
76 | 80 |
|
77 | | -token = CreateAuthorization(username,password,appid) |
78 | | -print 'Token = %s'%(token) |
| 81 | +if __name__ == '__main__': |
| 82 | + # Get username, password and applicationid |
| 83 | + username = raw_input('Please input username: ') |
| 84 | + # use getpass.getpass to hide user inputted password |
| 85 | + password = getpass.getpass(prompt='Please input password: ') |
| 86 | + appid = raw_input('Please input appid: ') |
79 | 87 |
|
80 | | -## if authentiacation success, continue subscribing Online Report |
81 | | -if token is not None: |
82 | | - RetrieveOnlineReport(token,appid) |
| 88 | + token = CreateAuthorization(username, password, appid) |
| 89 | + print 'Token = %s' % (token) |
83 | 90 |
|
| 91 | + # if authentiacation success, continue subscribing Online Report |
| 92 | + if token is not None: |
| 93 | + RetrieveOnlineReport(token, appid) |
0 commit comments