Skip to main content

REST API Query Worker Example

This page shows you how to use the Macrometa API and query workers to perform basic CRUD operations on documents stored in a document collection.

For more information about using Macrometa APIs, refer to APIs.

Prerequisites

REST API Doc Query Example

  1. Copy and paste the code block below in your favorite IDE.
  2. Update constants with your values, such as the API key.
  3. Run the code.
import json
import time
import requests

# Set constants
URL = "api-play.paas.macrometa.io"
HTTP_URL = f"https://{URL}"
API_KEY = "XXXXX" # Use your API key here
AUTH_TOKEN = f"apikey {API_KEY}"
FABRIC = "_system"
READ_QUERY = "FOR doc IN @@collection FILTER doc.result > 90 RETURN doc"
COLLECTION_NAME = "api_query_tutorial"
QUERY_PARAMS = {"@collection": f"{COLLECTION_NAME}"}
INSERT_QUERY = "FOR i IN 1..100 INSERT { result: i } INTO @@collection"
UPDATE_QUERY = "FOR doc IN @@collection FILTER doc.result <= 35 " \
"UPDATE doc._key WITH { qualified :true } IN @@collection"
DELETE_QUERY = "FOR c IN @@collection REMOVE c IN @@collection"
UPDATE_READ_QUERY = "FOR doc IN @@collection FILTER doc.result < 10 RETURN doc"

# Create an HTTPS session
print("1. Connecting to GDN")
session = requests.session()
session.headers.update({"content-type": 'application/json'})
session.headers.update({"authorization": AUTH_TOKEN})

# Create a document collection
print(f"\n2. Creating collection {COLLECTION_NAME}")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/collection"
payload = {
"name": COLLECTION_NAME
}
resp = session.post(URL, data=json.dumps(payload))
resp = json.loads(resp.text)
if 'error' in resp and resp['error']:
if resp['errorNum'] == 1207:
print("Collection already exists.")
else:
print("ERROR: " + resp["errorMessage"])
else:
print("Collection created successfully: ", resp['name'])

# Create a query worker
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql"

print("\n3. Creating query workers")
# Save Read query worker
payload = {
"query": {
"name": "Read",
"parameter": QUERY_PARAMS,
"value": READ_QUERY
}
}

resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Read query worker saved successfully.")
else:
print(resp_json)
time.sleep(1)

# Save Insert query worker
payload = {
"query": {
"name": "Insert",
"value": INSERT_QUERY,
"parameter": QUERY_PARAMS,

}
}

resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Insert query worker saved successfully.")
else:
print(resp_json)
time.sleep(1)

# Save Update query worker
payload = {
"query": {
"name": "Update",
"value": UPDATE_QUERY,
"parameter": QUERY_PARAMS,

}
}

resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Update query worker saved successfully.")
else:
print(resp_json)
time.sleep(1)

# Save Delete query worker
payload = {
"query": {
"name": "Delete",
"value": DELETE_QUERY,
"parameter": QUERY_PARAMS,

}
}
resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Delete query worker saved successfully.")
else:
print(resp_json)
time.sleep(1)

# Execute Insert query worker
print("\n4. Executing query worker: Insert")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/execute/Insert"
payload = {
"bindVars": QUERY_PARAMS,
}
resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False or resp_json.get("result") == []:
print("Insert query worker executed successfully.")
else:
print(resp_json)
time.sleep(1)

# Execute Read query worker
print("\n5. Executing query worker: Read")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/execute/Read"
payload = {
"bindVars": QUERY_PARAMS,
}
resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Read query worker executed successfully. Data: ")
for doc in resp_json.get("result", []):
print(json.dumps(doc, indent=4))
else:
print(resp_json)
time.sleep(1)

# Execute Update query worker
print("\n6. Executing query worker: Update")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/execute/Update"
payload = {
"bindVars": QUERY_PARAMS,
}
resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False or resp_json.get("result") == []:
print("Update query worker executed successfully.")
else:
print(resp_json)
time.sleep(1)

# Update Read query worker
print("\n7. Updating query worker: Read")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/Read"
payload = {
"query": {
"parameter": QUERY_PARAMS,
"value": UPDATE_READ_QUERY
}
}
resp = session.put(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False:
print("Read query worker updated successfully.")
else:
print(resp_json)
time.sleep(1)

# Execute Delete query worker
print("\n8. Executing query worker: Delete")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/execute/Delete"
payload = {
"bindVars": QUERY_PARAMS,
}
resp = session.post(URL, data=json.dumps(payload))
resp_json = resp.json()
if resp_json.get("error") == False or resp_json.get("result") == []:
print("Delete query worker executed successfully.")
else:
print(resp_json)
time.sleep(1)

# Delete query workers
print("\n9. Deleting query workers")
URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/Read"
resp = session.delete(URL)
resp_json = resp.json()
if resp_json.get("error") == False:
print("Read query worker deleted successfully.")
else:
print(resp_json)
time.sleep(1)

URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/Insert"
resp = session.delete(URL)
resp_json = resp.json()
if resp_json.get("error") == False:
print("Insert query worker deleted successfully.")
else:
print(resp_json)
time.sleep(1)

URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/Update"
resp = session.delete(URL)
resp_json = resp.json()
if resp_json.get("error") == False:
print("Update query worker deleted successfully.")
else:
print(resp_json)
time.sleep(1)

URL = f"{HTTP_URL}/_fabric/{FABRIC}/_api/restql/Delete"
resp = session.delete(URL)
resp_json = resp.json()
if resp_json.get("error") == False:
print("Delete query worker deleted successfully.")
else:
print(resp_json)