Skip to content

Instantly share code, notes, and snippets.

@shapiromatron
Last active March 16, 2018 20:46
Show Gist options
  • Select an option

  • Save shapiromatron/0f04fa1f2626229cce28b06b02b7af4f to your computer and use it in GitHub Desktop.

Select an option

Save shapiromatron/0f04fa1f2626229cce28b06b02b7af4f to your computer and use it in GitHub Desktop.
Example NTP sandbox clients

NTP Sandbox job clients

A job queue for executing computational toxicology jobs is avialable on the NTP Sandbox. Example job clients are provided below. Note that all clients require use of an authentication permission token before using the API.

# an example job submission
curl \
-X POST \
-H "Authorization: Token abcdefghijklmnopqrstuvwxyz1234567890" \
-H "Content-Type: application/json" \
-d '{"jobtype": "success", "name": "Isaac Newton"}' \
https://sandbox.ntp.niehs.nih.gov/job-runner/api/v1/test/
# an example request to check results of job
curl \
-X GET \
-H "Authorization: Token abcdefghijklmnopqrstuvwxyz1234567890" \
-H "Content-Type: application/json" \
https://sandbox.ntp.niehs.nih.gov/job-runner/api/v1/test/
import json
import time
import requests
class JobException(Exception):
pass
def run_job(url, data, api_token, interval=3, timeout=60):
with requests.Session() as s:
s.headers.update({
"Authorization": f"Token {api_token}",
"Content-Type": "application/json",
"Accept": "application/json",
})
# initial job request
response = s.post(url, data=json.dumps(data))
payload = response.json()
if response.status_code in [400, 403]:
raise JobException(payload['detail'])
# handle result if finished
if payload['is_finished']:
if payload['has_errors']:
raise JobException(payload['errors'])
else:
return payload['outputs']
# poll response until job is complete or client-timeout
wait_time = 0
url = payload['url']
while True:
time.sleep(interval)
payload = s.get(url).json()
# handle result if finished
if payload['is_finished']:
if payload['has_errors']:
raise JobException(payload['errors'])
else:
return payload['outputs']
wait_time += interval
if wait_time > timeout:
raise JobException('Client timeout')
# example use
result = run_job(
'https://sandbox.ntp.niehs.nih.gov/job-runner/api/v1/casrn-to-smiles/',
{"casrns": ["1135-66-6", "58918-34-6", "18172-6a-3", "18172-67-3"]},
'abcdefghijklmnopqrstuvwxyz1234567890',
)
library(stringr)
library(jsonlite)
library(httr)
run_job <- function(qurl, data, api_token, interval = 3, timeout = 60){
httr::set_config(httr::config(ssl_verifypeer = 0))
httr::set_config(httr::add_headers(
"Authorization" = paste("Token", api_token),
"Content-Type" = "application/json",
"Accept": "application/json"
))
response <- httr::POST(qurl, body = data, encode = "json", accept_json())
if (status_code(response) == 403) {
stop_for_status(response)
} else if (status_code(response) == 400) {
stop_for_status(response)
}
job_url <- content(response, "parsed") %>% .[['url']]
wait_time <- 0
while (1) {
Sys.sleep(interval)
response2 <- httr::GET(job_url)
response2l <- content(response2, "parsed")
if (response2l$is_finished) {
if (response2l$has_errors) {
stop(response2l$errors)
} else {
return(response2l$outputs)
}
}
wait_time = wait_time + interval
if (wait_time > timeout) {
stop("Client timeout")
}
}
}
# example use
result <- run_job(
'https://sandbox.ntp.niehs.nih.gov/job-runner/api/v1/casrn-to-smiles/',
list(casrns=c("1135-66-6", "58918-34-6", "18172-6a-3", "18172-67-3"))
'abcdefghijklmnopqrstuvwxyz1234567890',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment