3

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?

Nicholas Lee
  • 31
  • 1
  • 4

3 Answers3

1

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)

Nicolas Busca
  • 1,100
  • 7
  • 14
1

The Redash official API document is so lame, it doesn't give any examples for the documented "Common Endpoints". I was having no idea how I should use the API key.

Instead check this saviour https://github.com/damienzeng73/redash-api-client .

Rick
  • 7,007
  • 2
  • 49
  • 79
0

TL;DR:

...
query_info = {'query':query_content,'data_source_id':<find this number>}
...

Verbose:

I had a similar problem. Checked redash source code, it looks for data_source_id. I added the data_source_id to my data payload which worked.

You can find the appropriate data_source_id by looking at the response from a 'get query' call:

import json
def find_data_source_id(url,query_number,api)
    path = "{}/api/queries/{}".format(url,query_number)
    headers ={'Authorization': 'Key {}'.format(api), 'Content-Type': 'application/json'}
    response = requests.get(path, headers = headers)
    return json.loads(response.text)['data_source_id']

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83