Skip to content

Commit 521e3c0

Browse files
committed
add workable python console app
1 parent 10669ad commit 521e3c0

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

Python/rdp_apis_console.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,84 @@
11
import os
22
import json
3+
import sys
34
from dotenv import load_dotenv
5+
from icecream import ic
46
import requests
57

68
if __name__ == '__main__':
79

810
client_secret = ''
911
scope = 'trapi'
1012
universe = '7203.T'
13+
auth_obj = None
1114

1215
load_dotenv()
1316

14-
print(os.getenv('RTO_USER'))
17+
# Get RDP Token service information from Environment Variables
18+
base_URL = os.getenv('RDP_BASE_URL')
19+
auth_endpoint = base_URL + os.getenv('RDP_AUTH_URL')
20+
21+
# Get RDP Credentials information from Environment Variables
22+
username = os.getenv('RDP_USER')
23+
password = os.getenv('RDP_PASSWORD')
24+
app_key = os.getenv('RDP_APP_KEY')
1525

16-
# RDP Login
17-
auth_endpoint = os.getenv('RDP_WS_AUTH_URL')
1826
# -- Init and Authenticate Session
1927
auth_request_msg = {
20-
'username': os.getenv('RDP_USER') ,
21-
'password': os.getenv('RDP_PASSWORD') ,
28+
'username': username ,
29+
'password': 'password' ,
2230
'grant_type': "password",
2331
'scope': scope,
2432
'takeExclusiveSignOnControl': "true"
2533
}
2634

2735
try:
28-
response = requests.post(auth_endpoint, headers = {'Accept':'application/json'}, data = auth_request_msg, auth = (os.getenv('RDP_APP_KEY'), client_secret))
36+
response = requests.post(auth_endpoint, headers = {'Accept':'application/json'}, data = auth_request_msg, auth = (app_key, client_secret))
2937
except Exception as exp:
30-
print('Caught exception: %s' % str(exp))
38+
ic('Caught exception: %s' % str(exp))
3139

3240
if response.status_code == 200: # HTTP Status 'OK'
3341
print('Authentication success')
3442
auth_obj = response.json()
3543
else:
3644
print('RDP authentication result failure: %s %s' % (response.status_code, response.reason))
3745
print('Text: %s' % (response.text))
46+
47+
if auth_obj is None:
48+
print('Authentication fail, exit program')
49+
sys.exit(0)
3850

39-
# ESG Data
40-
esg_url = os.getenv('RDP_ESG_URP')
41-
payload = {'universe': universe}
51+
# Get RDP Token service information from Environment Variables
52+
esg_url = base_URL + os.getenv('RDP_ESG_URL')
4253

54+
payload = {'universe': universe}
55+
esg_object = None
4356
try:
44-
response = requests.get(esg_url, headers={'Authorization': 'Bearer {}'.format(auth_obj['access_token'])}, params = payload)
57+
response = requests.get(esg_url, headers={'Authorization': 'Bearer {}'.format(auth_obj['refresh_token'])}, params = payload)
4558
except Exception as exp:
4659
print('Caught exception: %s' % str(exp))
4760

4861
if response.status_code == 200: # HTTP Status 'OK'
49-
print('This is a ESG data result from RDP API Call')
50-
print(response.json())
62+
print('Receive ESG Data from RDP, transform data to Pandas Dataframe format')
63+
#print(response.json())
5164
esg_object=response.json()
5265
else:
5366
print('RDP APIs: ESG data request failure: %s %s' % (response.status_code, response.reason))
5467
print('Text: %s' % (response.text))
5568

5669
print('\n')
5770

58-
59-
60-
# https://developers.refinitiv.com/en/article-catalog/article/using-rdp-api-request-esg-data-jupyter-notebook
61-
import pandas as pd
62-
import numpy as np
63-
headers=esg_object['headers']
64-
65-
#Get column headers/titles using lambda
66-
titles=map(lambda header:header['title'], headers)
67-
68-
dataArray=np.array(esg_object['data'])
69-
df=pd.DataFrame(data=dataArray,columns=titles)
70-
71-
if df.empty is False:
72-
print(df)
71+
if esg_object is not None:
72+
# https://developers.refinitiv.com/en/article-catalog/article/using-rdp-api-request-esg-data-jupyter-notebook
73+
import pandas as pd
74+
import numpy as np
75+
headers=esg_object['headers']
76+
77+
#Get column headers/titles using lambda
78+
titles=map(lambda header:header['title'], headers)
79+
80+
dataArray=np.array(esg_object['data'])
81+
df=pd.DataFrame(data=dataArray,columns=titles)
82+
83+
if df.empty is False:
84+
print(df)

Python/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
asttokens==2.0.5
12
certifi==2021.5.30
23
charset-normalizer==2.0.3
4+
colorama==0.4.4
35
cycler==0.10.0
6+
executing==0.7.0
47
idna==3.2
58
kiwisolver==1.3.1
69
matplotlib==3.4.2
710
numpy==1.21.1
811
pandas==1.3.1
912
Pillow==8.3.1
13+
Pygments==2.9.0
1014
pyparsing==2.4.7
1115
python-dateutil==2.8.2
1216
python-dotenv==0.19.0

0 commit comments

Comments
 (0)