Skip to content

Instantly share code, notes, and snippets.

@tomas-wood
Last active October 18, 2018 22:02
Show Gist options
  • Save tomas-wood/a454db26e1dc08fa151807671be7ed6f to your computer and use it in GitHub Desktop.
Save tomas-wood/a454db26e1dc08fa151807671be7ed6f to your computer and use it in GitHub Desktop.
GES API HOW TO

Hey! So you want to use the GES API to do gremlin queries? Well we can help you with that!

We're going to assume you have an account and have already uploaded your graph to GES. This walkthrough will help you get programmatic access to that graph from anywhere in the world.

Generate a token
Use this code and enter in your own region, username, password, and domain_name.

import urllib3
import codecs
import certifi

http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED", ca_certs=certifi.where())

headers = {"Content-Type": "text/plain;charset=UTF-8",}
region = <your_region>

data = '{ \
        "auth": { \
          "identity": { \
            "methods": [ \
              "password" \
            ], \
            "password": { \
              "user": { \
                "name": <your_username>, \
                "password": <your_password>, \
                "domain": { \
                  "name": <your_domain_name> \
                } \
              } \
            } \
          }, \
          "scope": { \
            "project": { \
              "id": <your_project_id> \
          } \
        } \
      } \
    }'

r = http.request(
    "POST",
    "https://{}.myhuaweicloud.com/v3/auth/tokens".format(region),
    headers=headers,
    body=data
    )

# Make sure the request was received. should be 201.
print(r.status)

# Get the token that was created out of the headers.
token = r.headers.get("X-Subject-Token")

# Save us a copy.
with codecs.open("token", "w", "utf-8") as f:
    f.write(token)

And after you've done that you should have a new file named token sitting there on your drive which you can use to access the API.

The token expires after 24 hours, so if you're having any trouble, generate a new token and try whatever you were doing again before reaching out.

Access the API
So we have a token now that will let us access the API. Now we need to figure out how to send gremlin queries to the API.

This code can help. Make sure you look up your graph's ID in the Huawei Console to substitute in for <my_graph_id>.

import urllib3
import json
import certifi
from pprint import pprint

http = urllib3.PoolManager(cert_reqs="CERT_REQUIRED", ca_certs=certifi.where())

with open("/path/to/token", "r") as f:
    token = f.read()

# Headers are how we pass through the token.
headers = {"Content-Type":"application/json"}
headers["X-Language"] = "en-us"
headers["X-Auth-Token"] = token

my_project_id = <my_project_id>
my_graph_id = <my_graph_id>
# You can find your region through the Huawei Console, same as the project and graph ids.
my_region = <my_region>

# We can start/stop the graph with the API in addition to sending queries, which can be useful.
# action = "start"
# action = "stop"
action = "execute-gremlin-query"

# Format the url.
base = "{}.myhuaweicloud.com".format(my_region)
url = "https://{}/v1.0/{}/graphs/{}/action?action_id={}".format(
    base, my_project_id, my_graph_id, action
    )

# This is how you form the query.
data = {"command":"g.V().limit(10)"}

# Send the query through with the request body. 
r = http.request("POST", url, headers=headers, body=json.dumps(data))

# Should be 200.
print(r.status)

# We don't want a JSON string. We want a python dictionary.
output = json.loads(r.data)
pprint(output)

For more information on the GES API, please check out the documentation.

For more information on the Gremlin graph database query language, please go through the tutorials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment