Created
July 22, 2016 11:40
-
-
Save arikfr/c86366ce55b79842cb7c76fcedf01496 to your computer and use it in GitHub Desktop.
Example code of how to use Redash refresh API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REDASH_HOST = os.environ.get('REDASH_HOST', 'https://app.redash.io/name') | |
def poll_job(s, job): | |
# TODO: timeout | |
while job['status'] not in (3,4): | |
response = s.get('{}/api/jobs/{}'.format(REDASH_HOST, job['id'])) | |
job = response.json()['job'] | |
time.sleep(1) | |
return job['status'] == 3 | |
def get_fresh_query_result(query_id, api_key): | |
s = requests.Session() | |
s.headers.update({'Authorization': 'Key {}'.format(api_key)}) | |
response = s.post('{}/api/queries/{}/refresh'.format(REDASH_HOST, query_id)) | |
if response.status_code != 200: | |
raise Exception('Refresh failed.') | |
# TODO: make sure it's true. | |
poll_job(s, response.json()['job']) | |
response = s.get('{}/api/queries/{}/results.json'.format(REDASH_HOST, query_id)) | |
if response.status_code != 200: | |
raise Exception('Failed getting results.') | |
return response.json()['query_result']['data']['rows'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello arik ,
actually i got job['status']=1 , so do you recommend what is the issue here to get this value ?