|
| 1 | +import os |
| 2 | +import json |
| 3 | +from dotenv import load_dotenv |
| 4 | +import requests |
| 5 | + |
| 6 | +if __name__ == '__main__': |
| 7 | + |
| 8 | + client_secret = '' |
| 9 | + scope = 'trapi' |
| 10 | + universe = '7203.T' |
| 11 | + |
| 12 | + load_dotenv() |
| 13 | + |
| 14 | + print(os.getenv('RTO_USER')) |
| 15 | + |
| 16 | + # RDP Login |
| 17 | + auth_endpoint = os.getenv('RDP_WS_AUTH_URL') |
| 18 | + # -- Init and Authenticate Session |
| 19 | + auth_request_msg = { |
| 20 | + 'username': os.getenv('RDP_USER') , |
| 21 | + 'password': os.getenv('RDP_PASSWORD') , |
| 22 | + 'grant_type': "password", |
| 23 | + 'scope': scope, |
| 24 | + 'takeExclusiveSignOnControl': "true" |
| 25 | + } |
| 26 | + |
| 27 | + try: |
| 28 | + response = requests.post(auth_endpoint, headers = {'Accept':'application/json'}, data = auth_request_msg, auth = (os.getenv('RDP_APP_KEY'), client_secret)) |
| 29 | + except Exception as exp: |
| 30 | + print('Caught exception: %s' % str(exp)) |
| 31 | + |
| 32 | + if response.status_code == 200: # HTTP Status 'OK' |
| 33 | + print('Authentication success') |
| 34 | + auth_obj = response.json() |
| 35 | + else: |
| 36 | + print('RDP authentication result failure: %s %s' % (response.status_code, response.reason)) |
| 37 | + print('Text: %s' % (response.text)) |
| 38 | + |
| 39 | + # ESG Data |
| 40 | + esg_url = os.getenv('RDP_ESG_URP') |
| 41 | + payload = {'universe': universe} |
| 42 | + |
| 43 | + try: |
| 44 | + response = requests.get(esg_url, headers={'Authorization': 'Bearer {}'.format(auth_obj['access_token'])}, params = payload) |
| 45 | + except Exception as exp: |
| 46 | + print('Caught exception: %s' % str(exp)) |
| 47 | + |
| 48 | + if response.status_code == 200: # HTTP Status 'OK' |
| 49 | + print('This is a ESG data result from RDP API Call') |
| 50 | + print(response.json()) |
| 51 | + esg_object=response.json() |
| 52 | + else: |
| 53 | + print('RDP APIs: ESG data request failure: %s %s' % (response.status_code, response.reason)) |
| 54 | + print('Text: %s' % (response.text)) |
| 55 | + |
| 56 | + print('\n') |
| 57 | + |
| 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) |
0 commit comments