REST API tutorial throwing errors

I’m using the following code to try to test retrieval of data from a Dremio dataset. I am able to query the dataset via the Dremio interface, and my Python code (shown below) connects to the API and gets a valid job id, however when I try to retrieve data via a SQL query, I get the following error: {‘errorMessage’: ‘Something went wrong’, ‘moreInfo’: ‘Can not fetch details for a job that is in [RUNNING] state.’}

import json
import requests

username = 'summersmd'
password = 'abc1234'
headers = {'content-type':'application/json'}
dremioServer = 'http://localhost:9047'

def apiGet(endpoint):
  return json.loads(requests.get('{server}/api/v3/{endpoint}'.format(server=dremioServer, endpoint=endpoint), headers=headers).text)

def apiPost(endpoint, body=None):
  text = requests.post('{server}/api/v3/{endpoint}'.format(server=dremioServer, endpoint=endpoint), headers=headers, data=json.dumps(body)).text

  # a post may return no data
  if (text):
    return json.loads(text)
  else:
    return None

def apiPut(endpoint, body=None):
  return requests.put('{server}/api/v3/{endpoint}'.format(server=dremioServer, endpoint=endpoint), headers=headers, data=json.dumps(body)).text

def apiDelete(endpoint):
  return requests.delete('{server}/api/v3/{endpoint}'.format(server=dremioServer, endpoint=endpoint), headers=headers)


def login(username, password):
  # we login using the old api for now
  loginData = {'userName': username, 'password': password}
  response = requests.post('http://localhost:9047/apiv2/login', headers=headers, data=json.dumps(loginData))
  data = json.loads(response.text)

  # retrieve the login token
  token = data['token']
  return {'content-type':'application/json', 'authorization':'_dremio{authToken}'.format(authToken=token)}

headers = login(username, password)
print (headers)

def querySQL(query):
  queryResponse = apiPost('sql', body={'sql': query})
  jobid = queryResponse['id']
  return jobid

path = ['\"OneSource\"', 'CV_CANDIDATE_PORTFOLIO']
path = '.'.join([str(x) for x in path])
query = "SELECT * FROM {source}".format(source=path)
jobid = querySQL(query)
print (jobid)
results = apiGet('job/{id}/results?offset={offset}&limit={limit}'.format(id=jobid, offset=0, limit=100))

print (results)

Hi,

The jobs API is asynchronous - when you submit a job, the API returns immediately and doesn’t wait for the job to complete. In your case, the job is in the RUNNING state and therefore there is no job results available.

You can use this endpoint to monitor the status of the job. All possible states can be found here.