【问题标题】:Using API to create a new query on Redash使用 API 在 Redash 上创建新查询
【发布时间】:2019-12-01 06:29:35
【问题描述】:

我设法将查询导入另一个帐户。我使用了 Redash 提供的端点 POST 函数,它只适用于“修改/替换”:https://github.com/getredash/redash/blob/5aa620d1ec7af09c8a1b590fc2a2adf4b6b78faa/redash/handlers/queries.py#L178

实际上,如果我想导入一个新查询,我应该怎么做?我想创建一个在我的帐户中不存在的新查询。我在看https://github.com/getredash/redash/blob/5aa620d1ec7af09c8a1b590fc2a2adf4b6b78faa/redash/handlers/queries.py#L84

如果 query_id 不存在,以下是我创建新查询的函数。

url = 路径,api = 用户 api,f = 文件名,query_id = 本地桌面文件的 query_id

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)

我收到 response.status_code 500。我的代码有什么问题吗?我该如何解决?

【问题讨论】:

    标签: python rest redash


    【解决方案1】:

    供将来参考 :-) 这是一个创建新查询的 python POST:

        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)
    

    (通过与@JohnDenver 的离线讨论找到了解决方案)

    【讨论】:

      【解决方案2】:

      TL;DR:

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

      详细说明:

      我遇到了类似的问题。检查redash source code,它寻找data_source_id。我将data_source_id 添加到我的有效数据负载中。

      您可以通过查看“获取查询”调用的响应来找到合适的 data_source_id:

      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']
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        • 2023-01-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多