# mute warning
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Common Utilities
from time import sleep
import json, urllib, requests, datetime
import pandas as pd
# Internet access with IBMid
myUserId = ''
myClientId = ''
myClientSecret = ''
myRestBase= 'api.ibm.com/industryresearch/run/v1'
apiBase = 'https://{0}:@{1}'.format(myUserId, myRestBase)
apiHeaders = {
'x-ibm-client-id': myClientId,
'x-ibm-client-secret': myClientSecret,
'Content-Type': 'application/json',
}
print(apiBase)
# Validate API invocation credentials via "GET /noop"
# Assert:
# HTTP status code: 204
apiName = '/noop'
response = requests.get((apiBase + apiName), headers=apiHeaders)
print('HTTP status code: ' + str(response.status_code))
# assert "HTTP status code: 204"
if (response.status_code != 204):
print(response.content)
# No need to change in this step
apiName = '/tempobjs'
apiBody = {
'name': 'molset source'
}
postTempobj = requests.post((apiBase + apiName), headers=apiHeaders, data=json.dumps(apiBody))
print('HTTP status code: ' + str(postTempobj.status_code))
if postTempobj.status_code in range(400, 600):
print(postTempobj.content)
elif postTempobj.status_code != 204:
print(json.dumps(postTempobj.json(), indent=2))
# A csv file in local
csvfile = './example.csv'
if postTempobj.status_code == 201:
putUrl = postTempobj.json()['tempobj'][0]['ref']['put']
getUrl = postTempobj.json()['tempobj'][0]['ref']['get']
print(f'Dataset GET URL: {getUrl}')
# Upload dataset to target temporary object
with open(csvfile, 'rb') as data:
requests.put(putUrl, data=data)
# Please do not change this cell
apiName = '/collections'
apiBody = {
"name": "PPF Moleset",
"type": "md_gm",
"md_gm_molset": {
"option": "ref",
"ref": {
"url": getUrl,
"mime": "text/csv"
}
},
"md_gm_metadata": {
"option": "ref",
"ref": {
"url": "http://foc.mybluemix.net/public/md_gm_metadata_1.yml",
"mime": "application/x-yaml"
}
}
}
postCollection = requests.post((apiBase + apiName), headers=apiHeaders, data=json.dumps(apiBody))
print('HTTP status code: ' + str(postCollection.status_code))
if postCollection.status_code in range(400, 600):
print(postCollection.content)
elif postCollection.status_code != 204:
print(json.dumps(postCollection.json(), indent=2))
# Please do not change this cell. The request should be finished less than 1 minute.
#
# Assert:
# molsetId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 2022-xx-xx 00:00:00.000000
# 2022-xx-xx 00:00:00.000000
# ...
# Job done with status code: 201
molsetId = postCollection.json()['collection'][0]['id']
print('molsetId:', molsetId)
apiName = '/jobs/' + postCollection.json()['collection'][0]['job']['id'] + '?done=true'
getJob = requests.get((apiBase + apiName), headers=apiHeaders)
while getJob.status_code == 404:
print(datetime.datetime.now())
sleep(3)
getJob = requests.get((apiBase + apiName), headers=apiHeaders)
print(f'Job done with status code: {getJob.json()["job"][0]["status"]}')
# assert: Job done with status code: 201
# Please do not change this cell
apiName = '/md/gm/features'
apiBody = {
"name": "ppf molset feature 1",
"mid": molsetId,
"fkey": 'tg+td+pco2',
"option": "ppf",
}
postFeature = requests.post((apiBase + apiName), headers=apiHeaders, data=json.dumps(apiBody))
print('HTTP status code: ' + str(postFeature.status_code))
if postFeature.status_code in range(400, 600):
print(postFeature.content)
elif postFeature.status_code != 204:
print(json.dumps(postFeature.json(), indent=2))
# Please do not change this cell
# The request should be finished less than 1 minute
#
# Assert:
# featureId: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# 2022-xx-xx 00:00:00.000000
# HTTP status code: 200
# Job wsStatus: finished
# Job message: {"httpCode":"201"}
featureId = postFeature.json()['md_gm_feature'][0]['id']
print(f'featureId: {featureId}')
apiName = '/jobs/' + postFeature.json()['md_gm_feature'][0]['job']['id']
getJob = requests.get((apiBase + apiName), headers=apiHeaders)
while (getJob.status_code == 200) and \
(not getJob.json()['job'][0]['done']):
print(f'{datetime.datetime.now()} wsStatus: {getJob.json()["job"][0]["wsStatus"]}')
sleep(5)
getJob = requests.get((apiBase + apiName), headers=apiHeaders)
print(f'HTTP status code: {getJob.status_code}')
if getJob.status_code == 200:
print(f'Job wsStatus: {getJob.json()["job"][0]["wsStatus"]}')
print(f'Job message: {getJob.json()["job"][0]["message"]}')
# Please do not change this cell
apiName = '/md/gm/features' + '/' + featureId
response = requests.get((apiBase + apiName), headers=apiHeaders)
print('HTTP status code: ' + str(response.status_code))
# assert: HTTP status code: 200
if response.status_code in range(400, 600):
print(response.content)
elif response.status_code != 204:
print(json.dumps(response.json(), indent=2))
# Please do not change this cell
# Display result
result = pd.read_csv(response.json()['md_gm_feature'][0]['result']['ref']['url'][0]['get'])
result