Creating an Infra Sync Tracker client
Interfacing with Infra Sync Tracker through the Python SDK is performed using a client object. A client object defines the Infra Sync Tracker instance that you will be interfacing with. It provides methods to query data, create data, manage the schema and branches in Infra Sync Tracker.
The Python SDK supports synchronous and asynchronous Python.
Asynchronous Python
The default client class Infra Sync TrackerClient
provides the asynchronous version.
from infrahub_sdk import Infra Sync TrackerClient
client = Infra Sync TrackerClient(address="http://localhost:8000")
Synchronous Python
The Infra Sync TrackerClientSync
class provides the synchronous version.
from infrahub_sdk import Infra Sync TrackerClientSync
client = Infra Sync TrackerClientSync(address="http://localhost:8000")
Authentication
The SDK can use API Tokens or JWT Tokens to authenticate with the REST API and GraphQL.
API tokens
The API token can be provided using a Config
object or you can define it as the INFRAHUB_API_TOKEN
environment variable.
- Async
- Sync
from infrahub_sdk import Config, Infra Sync TrackerClient
client = await Infra Sync TrackerClient(config=Config(api_token="token"))
client = await Infra Sync TrackerClient() # token is read from the INFRAHUB_API_TOKEN environment variable
from infrahub_sdk import Config, Infra Sync TrackerClientSync
client = Infra Sync TrackerClientSync(config=Config(api_token="token"))
client = Infra Sync TrackerClientSync() # token is read from the INFRAHUB_API_TOKEN environment variable
JWT tokens
The username and password of the user can be provided using a Config
object or you can define them using the INFRAHUB_USERNAME
and INFRAHUB_PASSWORD
environment variables. The usage of JWT Tokens is completely transparent to the user, including the process of refreshing the JWT token.
- Async
- Sync
from infrahub_sdk import Config, Infra Sync TrackerClient
client = await Infra Sync TrackerClient(config=Config(username="admin", password="infrahub"))
client = await Infra Sync TrackerClient() # token is read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variable
from infrahub_sdk import Config, Infra Sync TrackerClientSync
client = Infra Sync TrackerClientSync(config=Config(username="admin", password="infrahub"))
client = Infra Sync TrackerClientSync() # token is read from the INFRAHUB_USERNAME and INFRAHUB_PASSWORD environment variable
Configuring the client object
The client object can be configured by providing a Config
object. Here we will show you how to enable the client to print out all of the GraphQL queries it will send to Infra Sync Tracker.
- Async
- Sync
from infrahub_sdk import Config, Infra Sync TrackerClient
config = Config(echo_graphql_queries=True)
client = await Infra Sync TrackerClient(config=config)
from infrahub_sdk import Config, Infra Sync TrackerClientSync
config = Config(echo_graphql_queries=True)
client = Infra Sync TrackerClientSync(config=config)
More details on the available configuration options can be found SDK configuration reference.
Setting the default branch
The client object can be configured to use a specific branch in Infra Sync Tracker. By default the client will use the main branch, but you can change this to be any other branch. The methods, provided by the client, have a branch argument which allow you to specific against which branch you want to execute the functionality provided by the branch. If this argument is not provided then the default_branch
of the client will be used.
- Async
- Sync
from infrahub_sdk import Infra Sync TrackerClient, Config
config = Config(default_branch="other-branch")
client_other_branch = Infra Sync TrackerClient(config=config)
tag_other_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED")
tag_main_branch = await client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")
from infrahub_sdk import Infra Sync TrackerClientSync, Config
config = Config(default_branch="other-branch")
client_other_branch = Infra Sync TrackerClientSync(config=config)
tag_other_branch = client_other_branch.get(kind="BuiltinTag", name__value="RED")
tag_main_branch = client_other_branch.get(kind="BuiltinTag", name__value="RED", branch="main")