Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using API to create a new query on Redash

I managed to import queries into another account. I used the endpoint POST function given by Redash, it sort of just applies to just “modifying/replacing”: https://github.com/getredash/redash/blob/5aa620d1ec7af09c8a1b590fc2a2adf4b6b78faa/redash/handlers/queries.py#L178

So actually, if I want to import a new query what should I do? I want to create a new query that doesn’t exist on my account. I’m looking at https://github.com/getredash/redash/blob/5aa620d1ec7af09c8a1b590fc2a2adf4b6b78faa/redash/handlers/queries.py#L84

Following is the function which I made to create new queries if the query_id doesn’t exist.

url = path, api = user api, f = filename, query_id = query_id of file in local desktop

def new_query(url, api, f, query_id):
    headers ={'Authorization': 'Key {}'.format(api), 'Content-Type': 'application/json'}
    path = "{}/api/queries".format(url)
    query_content = get_query_content(f)
    query_info = {'query':query_content}
    print(json.dumps(query_info))
    response = requests.post(path, headers = headers, data = json.dumps(query_info))
    print(response.status_code)

I am getting response.status_code 500. Is there anything wrong with my code? How should I fix it?

like image 602
Nicholas Lee Avatar asked Sep 01 '25 10:09

Nicholas Lee


1 Answers

For future reference :-) here's a python POST that creates a new query:

    payload = {
               "query":query, ## the select query
               "name":"new query name",
               "data_source_id":1, ## can be determined from the /api/data_sources end point
               "schedule":None,
               "options":{"parameters":[]}
               }
    res = requests.post(redash_url + '/api/queries', 
                        headers = {'Authorization':'Key YOUR KEY'},
                        json=payload)

(solution found thanks to an offline discussion with @JohnDenver)

like image 130
Nicolas Busca Avatar answered Sep 02 '25 23:09

Nicolas Busca