This example makes use of an open-source GraphQL client module for Python, gql. You must first install this module into your Python environment to use it, as well as other modules referenced if not already installed.
import requests
import json
#
# Import from GraphQL client library for Python...
#
from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport
#
# Call token discovery endpoint...
#
response = requests.get("https://sandbox.vertalo.com/authenticate/token/discovery")
# print(response.text)
#
# Get LOGIN access token...
#
client_id = "<your client ID>"
client_secret = "<your client secret>"
response = requests.get(f"https://sandbox.vertalo.com/authenticate/token/login?client_id={client_id}&client_secret={client_secret}")
response = json.loads(response.text)
access_token = response["token"]["access_token"]
users_account_id = response["roles"]["data"][0]["users_account_id"]
# print(access_token)
# print(users_account_id)
#
# Get ROLE access token...
#
if access_token and users_account_id:
headers = {"Authorization": f"Bearer {access_token}"}
response = requests.post(f"https://sandbox.vertalo.com/authenticate/token/role/{users_account_id}", headers=headers)
#
# Parse response and grab key values...
#
response = json.loads(response.text)
access_token = response["token"]["access_token"]
# print(access_token)
#
# Execute GraphQL query...
#
if access_token:
url = "https://sandbox.vertalo.com/token/api/v2/graphql"
headers = {"Authorization": f"Bearer {access_token}", "Content-Type": "application/json; charset=utf-8"}
transport = AIOHTTPTransport(url=url, headers=headers)
client = Client(transport=transport)
query = gql("""
query {
allAssets {
nodes {
id
name
type
}
}
}
"""
)
response = client.execute(query)
print(response)