Validio SDK
The Validio SDK provides functionality to interact with the Validio API using Python.
The Validio Software Development Kit (SDK) provides a basic GraphQL client to interact with the Validio API. The Validio SDK client is written in Python and all its public functions are documented according to pydoc and generated using Sphinx documentation. See Validio SDK Reference documentation.
The client allows you to send your queries or mutations as regular strings and pass a Python dictionary as variable_values
. For more information, see Using the SDK Client. You can find examples of how to use the client at Validio SDK Recipes.
Install the Validio SDK
The Validio SDK is published as Python packages on PyPI as validio-sdk.
You can use a Python package installer, such as ‘pip’, to install the SDK in your desired environment:
pip install validio-sdk
Authentication
You need to generate and use an API key and API secret key to interact with the Validio API. In our client that’s done automatically with a sequential process of looking for credentials:
- Look for environment variables. The variables that is needed to automatically set up authorization in the client is
VALIDIO_ENDPOINT
VALIDIO_ACCESS_KEY
VALIDIO_ACCESS_SECRET_KEY
- Looking for config.json in the path set by
VALIDIO_CONFIG_PATH
- Looking for
config.json
in an OS specific path defined by platformdirs
The config.json
can be created by using the Validio CLI tool and run validio config init
.
To manually specify the credentials, simply pass a ValidioConfig
when creating the client.
from validio_sdk.client import Client
from validio_sdk.config import ValidioConfig
client_with_automatic_config = Client()
client_with_custom_config = Client(
ValidioConfig(
endpoint="https://my.validio.app.com",
access_key=my_access_key,
access_secret=my_access_secret,
default_namespace="default",
),
)
More information
For more information, refer to the complete Validio SDK Reference documentation.
Using the SDK Client
You can find examples of how to use the client at Validio SDK Recipes.
Synchronous or Asynchronous
The client is written to be used asynchronously, so the client needs an asynchronous runtime. The reason is to allow safe concurrent use and allow multiple operations at the same time when needed. If you need to use the client in a non asynchronous context the recommended solution is to wrap the request in asyncio.run
.
import asyncio
from validio_sdk.client import Client
client = Client()
query = "..."
result = asyncio.run(client.execute(query))
This is not to be confused with how the server executes queries. When multiple queries are sent in the same request they’re always executed asynchronously on the server whereas mutations are always executed in sequence. For more information, refer to the official GraphQL documentation
Using the same client in different threads
To properly use the client across multiple threads at the same time, you need to use a session to ensure it's opened before the first request and not closed until all requests are done. This can be done by using a Python context.
import asyncio
from validio_sdk.client import Client
async def session_example():
client = Client()
with client as session:
await asyncio.gather(
task1(session),
task2(session),
task3(session),
)
Updated about 2 months ago