WData API Execute Query and get results
Thanks to Jesus Bouzada's guidance, I'm able to execute a query using the API. (Code follows)
When I go to fetch the results and/or the status, it requires a `queryResultId` - how do I get that?
----
import requests
import json
import os
AUTH_URL = "https://api.sandbox.wdesk.com/iam/v1/oauth2/token"
WDATA_API_URL = "https://h.sandbox.wdesk.com/s/wdata/prep"
def get_headers():
bearer_headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'charset': 'UTF-8'
}
bearer_data = {'client_id':client_id, 'client_secret':secret, 'grant_type':'client_credentials'}
bearer_result = requests.post(AUTH_URL, data= bearer_data, headers=bearer_headers)
bearer_json = json.loads(bearer_result.text)
bearer_token = bearer_json['access_token']
user_headers = {
'Authorization': 'Bearer ' + bearer_token
}
return user_headers
headers = get_headers()
#print(headers)
query_response = requests.get(WDATA_API_URL + "/api/v1/query/aebeae767ada4639b173b3637e419bd5", headers = headers).json()
print(query_response['body'])
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'}
headers['Authorization'] = get_headers()['Authorization']
body = {}
body['isExplain'] = False
body['queryDto'] = query_response['body']
body['queryId'] = 'aebeae767ada4639b173b3637e419bd5'
print(body)
query_execution_response = requests.post(WDATA_API_URL + "/api/v1/queryresult", json = body, headers = headers)
print(query_execution_response.request.body)
print(query_execution_response.json())
-
Hi Nick Ryberg
When you successfully Execute A Query, the response body is the query result entity QueryResultDto. An example success response can be found here. Below I've added the top portion of the example response that includes the ID in bold.
{
"body": {
"bytesScanned": "integer",
"columns": [
{
"name": "string",
"type": "string"
}
],
"created": "string",
"duration": "integer",
"error": "string",
"id": "string",
....This ID can then be used to poll the status with the Retrieve A Single Query Result and the Download A Query Result endpoints.
0Thanks Jeff Hickey - that worked - I was able to query status and then download the results - it would help to have a callout in the documentation that `id` maps to `queryResultId`.
queryResultId = result['body']['id']
url = 'https://h.app.wdesk.com/s/wdata/prep/api/v1/queryresult/' + queryResultId
r = requests.get(url, headers=headers)
print(r.json())0Accedi per aggiungere un commento.
Commenti
2 commenti