Skip to content

Commit 351fdd2

Browse files
Fine tuned errors message when REST API fails
1 parent 38db7da commit 351fdd2

3 files changed

Lines changed: 30 additions & 21 deletions

File tree

.gitignore.swp

-12 KB
Binary file not shown.

src/systemathics/helpers/token_helpers.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
functions:
66
* get_token - get token by autodecting environment variables
77
* create_bearer_token - create a bearer token (used by get_token when AUTH0_TOKEN env variable is set)
8-
* create_oauth_token - create oauth token (used by get_token when AUTH0_TOKEN env variable is not set and CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables are set)
8+
* create_bearer_token_using_rest - create beared token using REST API (used by get_token when AUTH0_TOKEN env variable is not set and CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables are set)
99
"""
1010

1111
import os
@@ -43,17 +43,17 @@ def get_token() -> str:
4343
missing.append("TENANT")
4444

4545
if (len(missing) == 0):
46-
return create_oauth_token(client_id, client_secret, audience, tenant)
47-
48-
raise Exception(f"AUTH0_TOKEN environment variable is not set, therefore CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables must be set. Missing env variables {missing}")
46+
return create_bearer_token_using_rest(client_id, client_secret, audience, tenant)
47+
else:
48+
raise Exception(f"AUTH0_TOKEN environment variable is not set, therefore CLIENT_ID, CLIENT_SECRET, AUDIENCE and TENANT environment variables must be set. Missing env variables {missing}")
4949

5050
def create_bearer_token(auth0_token) -> str:
5151
if (auth0_token == ''):
5252
raise Exception(f"auth0_token cannot be null")
5353

5454
return f"Bearer {auth0_token}"
5555

56-
def create_oauth_token(client_id, client_secret, audience, tenant) -> str:
56+
def create_bearer_token_using_rest(client_id, client_secret, audience, tenant) -> str:
5757
if (client_id == ''):
5858
raise Exception(f"client_id cannot be null")
5959
if (client_secret == ''):
@@ -63,19 +63,28 @@ def create_oauth_token(client_id, client_secret, audience, tenant) -> str:
6363
if (tenant == ''):
6464
raise Exception(f"tenant cannot be null")
6565

66-
# Setup connection and payload
67-
conn = http.client.HTTPSConnection(tenant)
68-
headers = { 'content-type': "application/json" }
69-
params = {"client_id": client_id, "client_secret": client_secret, "grant_type" : "client_credentials", "audience": audience }
70-
payload = json.dumps(params)
66+
try:
67+
# Setup connection and payload
68+
conn = http.client.HTTPSConnection(tenant)
69+
headers = { 'content-type': "application/json" }
70+
params = {"client_id": client_id, "client_secret": client_secret, "grant_type" : "client_credentials", "audience": audience }
71+
payload = json.dumps(params)
7172

72-
# Send Request
73-
conn.request("POST", "/oauth/token", payload, headers)
74-
res = conn.getresponse()
75-
data = res.read()
76-
json_data = json.loads(data.decode("utf-8"))
77-
78-
# Get access token to be used to authenticate against API
79-
token = f"{json_data['token_type']} {json_data['access_token']}"
73+
# Send Request
74+
conn.request("POST", "/oauth/token", payload, headers)
75+
res = conn.getresponse()
76+
data = res.read()
77+
78+
json_data = json.loads(data.decode("utf-8"))
79+
80+
# Get access token to be used to authenticate against API
81+
try:
82+
token = f"{json_data['token_type']} {json_data['access_token']}"
83+
return token
84+
except Exception as ee:
85+
print(f"create_bearer_token_using_rest: Returned JSON doesn't contain 'token_type' and/or 'access_token'. Check your client_id, client_secret, audience and tenant: {json_data}")
86+
return ""
8087

81-
return token
88+
except Exception as e:
89+
print(f"create_bearer_token_using_rest: Got exception {e}")
90+
return ""

test/intraday-bars-test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
In order for this to work, environment must be setup accordingly
66
You need to go to your dashboard in tokens section and adapt the bash snippet below
77
8-
export CLIENT_ID="YOUR_CLIENT_ID"
9-
export CLIENT_SECRET="YOUR_CLIENT_SECRET"
8+
export CLIENT_ID="wrFSU96w9Q1lXGaJC8zQJqNOFU16fsCl"
9+
export CLIENT_SECRET="5CsoEkx387Uqy9J-6PZFDYOC0ptbYp04pJGuc-luCrdXtZLleILwM_RV501M5FY4"
1010
export AUDIENCE="https://prod.ganymede-prod"
1111
export TENANT="ganymede-prod.eu.auth0.com"
1212
export GRPC_APIS="grpc.systemathics.cloud"

0 commit comments

Comments
 (0)