Skip to main content

Using Rest APIs

For the following examples, assume these credentials:

API Browser

Your main tool when working with REST APIs is the REST API browser available in the GDN web console. From there, you can execute various REST APIs and see exactly what the inputs and outputs are.

Connect to GDN

import requests
import json

# Constants

URL = "api-play.paas.macrometa.io"
HTTP_URL = "https://{}".format(URL)
EMAIL = "nemo@nautilus.com"
PASSWORD = "xxxxxx"
FABRIC = "_system"
AUTH_TOKEN = "bearer "

# Create HTTPS session

url = "{}/_open/auth".format(HTTP_URL)
payload = {
'email':EMAIL,
'password':PASSWORD
}
headers = {
'content-type': 'application/json'
}

response = requests.post(url, data = json.dumps(payload), headers = headers)

if response.status_code == 200:
resp_body = json.loads(response.text)
AUTH_TOKEN += resp_body["jwt"]
TENANT = resp_body["tenant"]
else:
raise Exception("Error while getting auth token. Code:{}, Reason:{}".format(response.status_code,response.reason))


session = requests.session()
session.headers.update({"content-type": 'application/json'})
session.headers.update({"authorization": AUTH_TOKEN})

Create Collection

Create a KV collection.

  # Create a collection

COLLECTION_NAME = "students"
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME

resp = session.post(url, data = json.dumps(payload))
resp = resp.json()
if 'error' in resp and resp['error']:
print("ERROR: " + resp['errorMessage'])
else:
print("\nCollection Created: ", resp["name"])

Insert KV Pairs

Insert Key Value pairs into collection.

  # Insert KV pairs into a collection
data = [
{
"_key": "John",
"value": "Science",
"expireAt": 0
},
{
"_key": "Alice",
"value": "Maths",
"expireAt": 0
},
{
"_key": "Alex",
"value": "Physics",
"expireAt": 0
},
{
"_key": "Monika",
"value": "Chemistry",
"expireAt": 0
}
]

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value"
print(url)
resp = session.put(url, data = json.dumps(data))
print("\nMultiple Documents Inserted: ", resp.text)

Get Value

Get value for a given key.

  # Get value for a key

KEY = "Monika"
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME + "/value/" + KEY
resp = session.get(url)
print("\nDocument with specified Key is: ",resp.text)

Get Count

Get size of the kv collection.

  # Get collection count

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME + "/count"
resp = session.get(url)
print("\nNumber of kv pairs in your collection: ",resp.text)

Update Value

Update value for a given key in the collection.

  # Update value for a key
data = {
"_key": "Monika",
"value": "Biology",
"expireAt": 0
}
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value"
resp = session.put(url, data = json.dumps(data))
print("\nDocument Updated: ", resp.text)

Delete Value

Delete value for a given key.

  # Delete value for a Key
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value/" + KEY
resp = session.delete(url)
print("\nDocument with specified Key Deleted: ", resp.text)

# Delete value for multiple Keys
data = ["Alex", "Alice", "John"]
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/values"
resp = session.delete(url, data = json.dumps(data))
print("\nDocument with specified Key Deleted: ", resp.text)

Get Collections

Get collections.

  # Get collections
url = HTTP_URL + "/_api/kv"
resp = session.get(url)
print("\nCollections : ",resp.text)

Delete Collections

Delete collection.

  # Delete collection

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME
resp = session.delete(url)
print("\nCollection Deleted: ", resp.text)

Complete example

import requests
import json

# Constants

URL = "api-play.paas.macrometa.io"
HTTP_URL = f"https://{URL}"
EMAIL = "nemo@nautilus.com"
PASSWORD = "xxxxxx"
FABRIC = "_system"
AUTH_TOKEN = "bearer "
COLLECTION_NAME = "students"

# Create HTTPS session

url = f"{HTTP_URL}/_open/auth"
payload = {
'email':EMAIL,
'password':PASSWORD
}
headers = {
'content-type': 'application/json'
}
response = requests.post(url, data = json.dumps(payload), headers = headers)
if response.status_code == 200:
resp_body = json.loads(response.text)
AUTH_TOKEN += resp_body["jwt"]
TENANT = resp_body["tenant"]
else:
raise Exception(f"Error while getting auth token. Code:{response.status_code}, Reason:{response.reason}")
session = requests.session()
session.headers.update({"content-type": 'application/json'})
session.headers.update({"authorization": AUTH_TOKEN})

# Get list of all regions

url = HTTP_URL + "/_api/datacenter/all"
dcl_resp = session.get(url)
dcl_list = json.loads(dcl_resp.text)
regions = []
for dcl in dcl_list:
dcl_url = dcl['tags']['url']
regions.append(dcl_url)
print("\nList of Regions: ",regions)

# Create a collection

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME
resp = session.post(url, data = json.dumps(payload))
resp = json.loads(resp.text)
if 'error' in resp and resp['error']:
print("ERROR: " + resp['errorMessage'])
else:
print("\nCollection Created: ", resp["name"])

# Insert KV pairs in a Collection

data = [
{
"_key": "John",
"value": "Science",
"expireAt": 0
},
{
"_key": "Alice",
"value": "Maths",
"expireAt": 0
},
{
"_key": "Alex",
"value": "Physics",
"expireAt": 0
},
{
"_key": "Monika",
"value": "Chemistry",
"expireAt": 0
}
]
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value"
print(url)
resp = session.put(url, data = json.dumps(data))
print("\nMultiple Documents Inserted: ", resp.text)

# Get value for a given key

KEY = "Monika"
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME + "/value/" + KEY
resp = session.get(url)
print("\nDocument with specified Key is: ",resp.text)

# Get collection count

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME + "/count"
resp = session.get(url)
print("\nNumber of kv pairs in your collection: ",resp.text)

# Update value for a key

data = {
"_key": "Monika",
"value": "Biology",
"expireAt": 0
}
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value"
resp = session.put(url, data = json.dumps(data))
print("\nDocument Updated: ", resp.text)

# Delete value for a key

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/value/" + KEY
resp = session.delete(url)
print("\nDocument with specified Key Deleted: ", resp.text)

# Delete value for multiple keys

data = ["Alex", "Alice", "John"]
url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME +"/values"
resp = session.delete(url, data = json.dumps(data))
print("\nDocument with specified Key Deleted: ", resp.text)

# Get collections

url = HTTP_URL + "/_api/kv"
resp = session.get(url)
print("\nCollections : ",resp.text)

# Delete collection

url = HTTP_URL + "/_api/kv/" + COLLECTION_NAME
resp = session.delete(url)
print("\nCollection Deleted: ", resp.text)