Skip to content

Commit 4dc1c21

Browse files
committed
Merge branch 'develop' into feature/list-data-file
2 parents 3ff8bd8 + e4a7d78 commit 4dc1c21

File tree

3 files changed

+162
-45
lines changed

3 files changed

+162
-45
lines changed

jupyter-notebooks/tutorials/3_working_with_jobs.ipynb

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"tags": []
2222
},
2323
"source": [
24-
"## 0. Set up imports and predefined variables\n",
24+
"## 0. Set up imports, predefined variables, and authentication\n",
2525
"\n",
2626
"For this Tutorial we will make use of a wrapper API that is called api.py (in the utils folder). It helps to manage things like authentication."
2727
]
@@ -34,13 +34,108 @@
3434
"outputs": [],
3535
"source": [
3636
"from utils import api\n",
37+
"import getpass\n",
3738
"import json\n",
3839
"from IPython.display import JSON\n",
3940
"import requests\n",
4041
"import time\n",
4142
"from datetime import datetime\n",
4243
"\n",
43-
"app_name = \"job-l1b-cwl:develop\""
44+
"app_name = \"job-l1b-cwl:develop\"\n",
45+
"\n",
46+
"# This portion of the code is env specific for Dev, Test, Ops, etc. \n",
47+
"# define the environment as our test venue\n",
48+
"env = {\n",
49+
" \"clientId\":\"71894molftjtie4dvvkbjeard0\",\n",
50+
"}\n",
51+
"\n",
52+
"# The auth_json is template for authorizing with AWS Cognito for a token that can be used for calls to the data service.\n",
53+
"# For now this is just an empty data structure. You will be prompted for your username and password in a few steps.\n",
54+
"auth_json = '''{\n",
55+
" \"AuthParameters\" : {\n",
56+
" \"USERNAME\" : \"\",\n",
57+
" \"PASSWORD\" : \"\"\n",
58+
" },\n",
59+
" \"AuthFlow\" : \"USER_PASSWORD_AUTH\",\n",
60+
" \"ClientId\" : \"\"\n",
61+
" }'''"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"id": "814a3678-dde6-44d9-ad94-5021b5813eb7",
67+
"metadata": {
68+
"tags": []
69+
},
70+
"source": [
71+
"### Authentication Code\n",
72+
"\n",
73+
"The below method is a helper function for getting an access token for accessing Unity SDS services. You must pass the token along with any API requests in order to access the various Unity SDS services."
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": null,
79+
"id": "dcd5e87b-8f36-4985-977a-e6b4bc3173b5",
80+
"metadata": {
81+
"tags": []
82+
},
83+
"outputs": [],
84+
"source": [
85+
"# This method is used for taking a username and password and client ID and fetching a cognito token\n",
86+
"def get_token(username, password, clientID):\n",
87+
" aj = json.loads(auth_json)\n",
88+
" aj['AuthParameters']['USERNAME'] = username\n",
89+
" aj['AuthParameters']['PASSWORD'] = password\n",
90+
" aj['ClientId'] =clientID \n",
91+
" token = None\n",
92+
" try:\n",
93+
" response = requests.post('https://cognito-idp.us-west-2.amazonaws.com', headers={\"Content-Type\":\"application/x-amz-json-1.1\", \"X-Amz-Target\":\"AWSCognitoIdentityProviderService.InitiateAuth\"}, json=aj)\n",
94+
" token = response.json()['AuthenticationResult']['AccessToken']\n",
95+
" except:\n",
96+
" print(\"Error, check username and password and try again.\")\n",
97+
" return token"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"id": "78549288-db6c-454c-a1dc-92d1a098b601",
103+
"metadata": {
104+
"tags": []
105+
},
106+
"source": [
107+
"### Prompt for your Unity username and password\n",
108+
"\n",
109+
"These are required to get the token (described above) to connect to the data services."
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": null,
115+
"id": "1f69d59c-65a9-4014-924e-255fd8ff158d",
116+
"metadata": {
117+
"tags": []
118+
},
119+
"outputs": [],
120+
"source": [
121+
"print(\"Please enter your username...\")\n",
122+
"user_name = input()\n",
123+
"\n",
124+
"print(\"Please enter your password...\")\n",
125+
"password = getpass.getpass()"
126+
]
127+
},
128+
{
129+
"cell_type": "code",
130+
"execution_count": null,
131+
"id": "a142588e-5710-401a-bd62-b46dd109212b",
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"token = get_token(user_name, password, env['clientId'])\n",
136+
"\n",
137+
"if(token):\n",
138+
" print(\"Token received.\")"
44139
]
45140
},
46141
{
@@ -62,7 +157,7 @@
62157
"metadata": {},
63158
"outputs": [],
64159
"source": [
65-
"apps = api.get_apps()\n",
160+
"apps = api.get_apps(token)\n",
66161
"for application in apps:\n",
67162
" print(\"Process ID: {}\".format(application['id']))\n",
68163
" print(\"Process Version: {}\".format(application['processVersion']))\n",
@@ -112,7 +207,7 @@
112207
"try:\n",
113208
"\n",
114209
" # Store Job ID to use in future steps\n",
115-
" job_id = api.submit_job(app_name, data)\n",
210+
" job_id = api.submit_job(token, app_name, data)\n",
116211
"\n",
117212
" # If the job submission is successful, print a success message along with the returned JOB-ID\n",
118213
" print(\"\\nJob Submission Successful!\\nJOB ID: {}\\n\".format(job_id))\n",
@@ -145,12 +240,12 @@
145240
"\n",
146241
"try:\n",
147242
" \n",
148-
" job_status = api.get_job_status(app_name, job_id)\n",
243+
" job_status = api.get_job_status(token, app_name, job_id)\n",
149244
"\n",
150245
" while job_status == \"running\":\n",
151246
" print(\"Status for job \\\"{}\\\" ({}): {}\".format(job_id, datetime.now().strftime(\"%H:%M:%S\"), job_status))\n",
152247
" time.sleep(5)\n",
153-
" job_status = api.get_job_status(app_name, job_id)\n",
248+
" job_status = api.get_job_status(token, app_name, job_id)\n",
154249
" \n",
155250
" # Print the job status\n",
156251
" print(\"\\nStatus for job \\\"{}\\\" ({}): {}\\n\".format(job_id, datetime.now().strftime(\"%H:%M:%S\"),job_status))\n",
@@ -183,7 +278,7 @@
183278
"\n",
184279
"try:\n",
185280
"\n",
186-
" job_data = api.get_job_by_id(app_name, job_id)\n",
281+
" job_data = api.get_job_by_id(token, app_name, job_id)\n",
187282
"\n",
188283
" #for data in job_data:\n",
189284
" # print(\"ID: \" + data['id'])\n",
@@ -224,7 +319,7 @@
224319
"metadata": {},
225320
"outputs": [],
226321
"source": [
227-
"jobs = api.get_jobs_for_app(app_name)\n",
322+
"jobs = api.get_jobs_for_app(token, app_name)\n",
228323
"\n",
229324
"print(\"Jobs running for Process: \", app_name, \"\\n\")\n",
230325
"\n",

jupyter-notebooks/utils/api.py

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from . import settings
44

5-
def get_apps():
5+
def get_apps(token):
66

77
try:
8-
9-
response = requests.get(settings.WPST_API_DOMAIN + "/processes")
8+
headers = _get_headers(token)
9+
response = requests.get(settings.WPST_API_DOMAIN + "/processes", headers=headers)
10+
response.raise_for_status()
1011
json_result = response.json()['processes']
1112

1213
except requests.exceptions.HTTPError as e:
@@ -15,11 +16,12 @@ def get_apps():
1516

1617
return json_result
1718

18-
def get_application(app_name):
19+
def get_application(token, app_name):
1920

2021
try:
21-
22-
response = requests.get(settings.WPST_API_DOMAIN + "/processes/{}".format(app_name))
22+
headers = _get_headers(token)
23+
response = requests.get(settings.WPST_API_DOMAIN + "/processes/{}".format(app_name), headers=headers)
24+
response.raise_for_status()
2325
json_result = response.json()['process']
2426

2527
except requests.exceptions.HTTPError as e:
@@ -28,15 +30,14 @@ def get_application(app_name):
2830

2931
return json_result
3032

31-
def deploy_application(app_config):
32-
33-
headers = {
34-
'Content-type': 'application/json'
35-
}
33+
def deploy_application(token, app_config):
3634

3735
try:
38-
36+
headers = _get_headers(token, {
37+
'Content-type': 'application/json'
38+
})
3939
response = requests.post(settings.WPST_API_DOMAIN + "/processes", headers=headers, json=app_config)
40+
response.raise_for_status()
4041
json_result = response.json()
4142

4243
except requests.exceptions.HTTPError as e:
@@ -45,11 +46,12 @@ def deploy_application(app_config):
4546

4647
return json_result
4748

48-
def dismiss_application(app_name):
49+
def dismiss_application(token, app_name):
4950

5051
try:
51-
52-
response = requests.delete(settings.WPST_API_DOMAIN + "/processes/{}".format(app_name))
52+
headers = _get_headers(token)
53+
response = requests.delete(settings.WPST_API_DOMAIN + "/processes/{}".format(app_name), headers=headers)
54+
response.raise_for_status()
5355
json_result = response.json()['undeploymentResult']
5456

5557
except requests.exceptions.HTTPError as e:
@@ -59,17 +61,18 @@ def dismiss_application(app_name):
5961
return json_result
6062

6163

62-
def submit_job(app_name, data):
64+
def submit_job(token, app_name, data):
6365

64-
headers = {
66+
headers = _get_headers(token, {
6567
'Content-type': 'application/json'
66-
}
68+
})
6769

6870
try:
6971
job_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs".format(app_name)
70-
r = requests.post(job_url, headers=headers, json=data)
72+
response = requests.post(job_url, headers=headers, json=data)
73+
response.raise_for_status()
7174

72-
job_location = r.headers['location']
75+
job_location = response.headers['location']
7376
if "http://127.0.0.1:5000" in job_location:
7477
job_location = job_location.replace("http://127.0.0.1:5000",settings.WPST_API_DOMAIN)
7578

@@ -81,26 +84,29 @@ def submit_job(app_name, data):
8184

8285
return job_id
8386

84-
def get_job_by_id(app_name, job_id):
87+
def get_job_by_id(token, app_name, job_id):
8588

8689
try:
87-
90+
headers = _get_headers(token)
8891
job_status_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs/{}".format(app_name, job_id)
89-
response = requests.get(job_status_url).json()
92+
response = requests.get(job_status_url, headers=headers)
93+
response.raise_for_status()
94+
json_result = response.json()
9095

9196
except requests.exceptions.HTTPError as e:
9297
# Add Logging Mechanism
9398
raise
9499

95-
return response
100+
return json_result
96101

97102

98-
def get_job_status(app_name, job_id):
103+
def get_job_status(token, app_name, job_id):
99104

100105
try:
101-
106+
headers = _get_headers(token)
102107
job_status_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs/{}".format(app_name, job_id)
103-
response = requests.get(job_status_url)
108+
response = requests.get(job_status_url, headers=headers)
109+
response.raise_for_status()
104110
job_status = response.json()['status']
105111

106112
except requests.exceptions.HTTPError as e:
@@ -109,12 +115,13 @@ def get_job_status(app_name, job_id):
109115

110116
return job_status
111117

112-
def get_job_result(app_name, job_id):
118+
def get_job_result(token, app_name, job_id):
113119

114120
try:
115-
121+
headers = _get_headers(token)
116122
job_result_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs/{}/result".format(app_name, job_id)
117-
response = requests.get(job_result_url)
123+
response = requests.get(job_result_url, headers=headers)
124+
response.raise_for_status()
118125
json_result = response.json()['outputs']
119126

120127
except requests.exceptions.HTTPError as e:
@@ -123,28 +130,43 @@ def get_job_result(app_name, job_id):
123130

124131
return json_result
125132

126-
def get_jobs_for_app(app_name):
133+
def get_jobs_for_app(token, app_name):
127134

128135
try:
136+
headers = _get_headers(token)
129137
job_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs".format(app_name)
130-
response = requests.get(job_url)
138+
response = requests.get(job_url, headers=headers)
139+
response.raise_for_status()
131140
json_result = response.json()['jobs']
132141
except:
133142
# Add Logging Mechanism
134143
raise
135144

136145
return json_result
137146

138-
def dismiss_job(app_name, job_id):
147+
def dismiss_job(token, app_name, job_id):
139148

140149
try:
141-
150+
headers = _get_headers(token)
142151
job_url = settings.WPST_API_DOMAIN + "/processes/{}/jobs/{}".format(app_name,job_id)
143-
response = requests.delete(job_url)
152+
response = requests.delete(job_url, headers=headers)
153+
response.raise_for_status()
144154
json_result = response.json()['statusInfo']
145155

146156
except:
147157
# Add Logging Mechanism
148158
raise
149159

150-
return json_result
160+
return json_result
161+
162+
163+
def _get_headers(token, additional_headers = {}):
164+
165+
if token == None:
166+
raise Exception("No authorization token was provided.")
167+
168+
base_header = {
169+
"Authorization": "Bearer " + token,
170+
}
171+
172+
return {**base_header, **additional_headers}

jupyter-notebooks/utils/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
WPST_API_DOMAIN = "http://a706c2e790a7a46489443f4f64b521e5-1186363756.us-west-2.elb.amazonaws.com:5001"
1+
WPST_API_DOMAIN = "https://71t9bgvuyc.execute-api.us-west-2.amazonaws.com/test/ades-wpst"

0 commit comments

Comments
 (0)