|
| 1 | +import requests |
| 2 | +import json |
| 3 | + |
| 4 | +class RDPHTTPController(): |
| 5 | + |
| 6 | + # Constructor Method |
| 7 | + def __init__(self): |
| 8 | + self.scope = 'trapi' |
| 9 | + self.client_secret = '' |
| 10 | + pass |
| 11 | + |
| 12 | + # Send HTTP Post request to get Access Token (Password Grant and Refresh Grant) from RDP Auth Service |
| 13 | + def rdp_authentication(self, auth_url, username, password, client_id, old_refresh_token = None): |
| 14 | + """ |
| 15 | + Send Authentication to RDP Auth service |
| 16 | + """ |
| 17 | + |
| 18 | + if not auth_url or not username or not password or not client_id: |
| 19 | + raise TypeError('Received invalid (None or Empty) arguments') |
| 20 | + |
| 21 | + access_token = None |
| 22 | + refresh_token = None |
| 23 | + expires_in = 0 |
| 24 | + if old_refresh_token is None: # For the Password Grant scenario |
| 25 | + payload=f'username={username}&password={password}&grant_type=password&scope={self.scope}&takeExclusiveSignOnControl=true&client_id={client_id}' |
| 26 | + else: # For the Refresh Token scenario |
| 27 | + payload=f'username={username}&refresh_token={old_refresh_token}&grant_type=refresh_token&client_id={client_id}' |
| 28 | + |
| 29 | + # Send HTTP Request |
| 30 | + try: |
| 31 | + response = requests.post(auth_url, |
| 32 | + headers = {'Content-Type':'application/x-www-form-urlencoded'}, |
| 33 | + data = payload, |
| 34 | + auth = (client_id, self.client_secret) |
| 35 | + ) |
| 36 | + except Exception as exp: |
| 37 | + print(f'Caught exception: {exp}') |
| 38 | + |
| 39 | + |
| 40 | + if response.status_code == 200: # HTTP Status 'OK' |
| 41 | + print('Authentication success') |
| 42 | + access_token = response.json()['access_token'] |
| 43 | + refresh_token = response.json()['refresh_token'] |
| 44 | + expires_in = int(response.json()['expires_in']) |
| 45 | + if response.status_code != 200: |
| 46 | + print(f'RDP authentication failure: {response.status_code} {response.reason}') |
| 47 | + print(f'Text: {response.text}') |
| 48 | + raise requests.exceptions.HTTPError(f'RDP authentication failure: {response.status_code} - {response.text} ', response = response ) |
| 49 | + |
| 50 | + return access_token, refresh_token, expires_in |
| 51 | + |
| 52 | + def rdp_getESG(self, esg_url, access_token, universe): |
| 53 | + |
| 54 | + if not esg_url or not access_token or not universe: |
| 55 | + raise TypeError('Received invalid (None or Empty) arguments') |
| 56 | + |
| 57 | + payload = {'universe': universe} |
| 58 | + # Request data for ESG Score Full Service |
| 59 | + try: |
| 60 | + response = requests.get(esg_url, headers={'Authorization': f'Bearer {access_token}'}, params = payload) |
| 61 | + except Exception as exp: |
| 62 | + print(f'Caught exception: {exp}') |
| 63 | + |
| 64 | + if response.status_code == 200: # HTTP Status 'OK' |
| 65 | + print('Receive ESG Data from RDP APIs') |
| 66 | + #print(response.json()) |
| 67 | + else: |
| 68 | + print(f'RDP APIs: ESG data request failure: {response.status_code} {response.reason}') |
| 69 | + print(f'Text: {response.text}') |
| 70 | + raise requests.exceptions.HTTPError(f'ESG data request failure: {response.status_code} - {response.text} ', response = response ) |
| 71 | + |
| 72 | + return response.json() |
| 73 | + |
| 74 | + def rdp_getSearchExplore(self, search_url, access_token, payload): |
| 75 | + |
| 76 | + if not search_url or not access_token or not payload: |
| 77 | + raise TypeError('Received invalid (None or Empty) arguments') |
| 78 | + |
| 79 | + headers = { |
| 80 | + 'Accept': 'application/json', |
| 81 | + 'Authorization': f'Bearer {access_token}' |
| 82 | + } |
| 83 | + |
| 84 | + try: |
| 85 | + response = requests.post(search_url, headers = headers, data = json.dumps(payload)) |
| 86 | + except Exception as exp: |
| 87 | + print(f'Caught exception: {exp}') |
| 88 | + |
| 89 | + if response.status_code == 200: # HTTP Status 'OK' |
| 90 | + print('Receive Search Explore Data from RDP APIs') |
| 91 | + #print(response.json()) |
| 92 | + else: |
| 93 | + print(f'RDP APIs: Search Explore request failure: {response.status_code} {response.reason}') |
| 94 | + print(f'Text: {response.text}') |
| 95 | + raise requests.exceptions.HTTPError(f'Search Explore request failure: {response.status_code} - {response.text} ', response = response ) |
| 96 | + |
| 97 | + return response.json() |
| 98 | + |
0 commit comments