|
1 | 1 | import os |
2 | 2 | import json |
| 3 | +import sys |
3 | 4 | from dotenv import load_dotenv |
| 5 | +from icecream import ic |
4 | 6 | import requests |
5 | 7 |
|
6 | 8 | if __name__ == '__main__': |
7 | 9 |
|
8 | 10 | client_secret = '' |
9 | 11 | scope = 'trapi' |
10 | 12 | universe = '7203.T' |
| 13 | + auth_obj = None |
11 | 14 |
|
12 | 15 | load_dotenv() |
13 | 16 |
|
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') |
15 | 25 |
|
16 | | - # RDP Login |
17 | | - auth_endpoint = os.getenv('RDP_WS_AUTH_URL') |
18 | 26 | # -- Init and Authenticate Session |
19 | 27 | auth_request_msg = { |
20 | | - 'username': os.getenv('RDP_USER') , |
21 | | - 'password': os.getenv('RDP_PASSWORD') , |
| 28 | + 'username': username , |
| 29 | + 'password': 'password' , |
22 | 30 | 'grant_type': "password", |
23 | 31 | 'scope': scope, |
24 | 32 | 'takeExclusiveSignOnControl': "true" |
25 | 33 | } |
26 | 34 |
|
27 | 35 | 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)) |
29 | 37 | except Exception as exp: |
30 | | - print('Caught exception: %s' % str(exp)) |
| 38 | + ic('Caught exception: %s' % str(exp)) |
31 | 39 |
|
32 | 40 | if response.status_code == 200: # HTTP Status 'OK' |
33 | 41 | print('Authentication success') |
34 | 42 | auth_obj = response.json() |
35 | 43 | else: |
36 | 44 | print('RDP authentication result failure: %s %s' % (response.status_code, response.reason)) |
37 | 45 | print('Text: %s' % (response.text)) |
| 46 | + |
| 47 | + if auth_obj is None: |
| 48 | + print('Authentication fail, exit program') |
| 49 | + sys.exit(0) |
38 | 50 |
|
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') |
42 | 53 |
|
| 54 | + payload = {'universe': universe} |
| 55 | + esg_object = None |
43 | 56 | 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) |
45 | 58 | except Exception as exp: |
46 | 59 | print('Caught exception: %s' % str(exp)) |
47 | 60 |
|
48 | 61 | 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()) |
51 | 64 | esg_object=response.json() |
52 | 65 | else: |
53 | 66 | print('RDP APIs: ESG data request failure: %s %s' % (response.status_code, response.reason)) |
54 | 67 | print('Text: %s' % (response.text)) |
55 | 68 |
|
56 | 69 | print('\n') |
57 | 70 |
|
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) |
0 commit comments