Python SDK

The Python version of The Ops Platform SDK works in conjunction with our Docker base images. The SDK allows the development of automations with rich user interfaces, that run in both the local terminal and in our Slack App as Slack Ops. Other than The Ops Platform itself, this SDK depends on:

  • Python 3.6+
  • The requests library, version 2+

Python SDK Installation

The Python installer can be found on PyPi here: https://pypi.org/project/cto-ai

It can be installed with this command:

pip install cto-ai

sdk.get_host_os

sdk.get_host_os() -> str

Returns the Operating System the host is running on. For remote ops, will return "unknown" as no host is accessible in the remote environment.

Output:

Host's os is: darwin

sdk.home_dir

sdk.home_dir() -> str

Returns the home directory of the host machine.

Output:

Host's home directory is: /root

sdk.log

sdk.log(args)

Logs to the console in in a standardized way. Will not be relayed to
the user in a remote environment (use ux.print for that).

Output:

I am a log!
I am too!

sdk.get_state_path

sdk.get_state_path() -> str

Returns the path of the state directory. The contents of this directory are persistent through a workflow, including on remote.

Output:

The state path is: /root/.config/@cto.ai/ops/superman/test-op/00000000-1111-2222-3333-444444444444

sdk.config.get

sdk.config.get(key: str) -> str

Gets the value that is saved under the given key in the team configuration store, or None if the key is not set.

sdk.config.set

sdk.config.set(key: str, value: str)

Sets the given value in the team configuration store under the provided key.

sdk.config.delete

sdk.config.delete(key: str) -> bool

Deletes a key-value pair in the persistent team configuration. Returns True if the key exists and the deletion is successful, or False if the key cannot be found.

sdk.config.get_all

sdk.config.get_all() -> dict

Gets all of the keys and values in the team configuration store.

sdk.get_secret

sdk.get_secret(key: str) -> str

Requests a secret from the secret store with the given key.

If the secret exists, it is returned, printing a notification to the user that it has been accessed.

Otherwise, the user is prompted to provide a replacement, either from the secret store or by direct entry through their interface.

Example

from cto_ai import sdk

secret = sdk.get_secret('SECRET')
# `ux.print` hides secret values but `sdk.log` does not.
sdk.log(f'my secret is {secret}')

Output

my secret is SUPER_SECRET_VALUE

sdk.set_secret

sdk.set_secret(key: str, value: str) -> str

Sets a particular value into the secret store.

If the secret already exists, the user is prompted on whether to overwrite it.

Returns the key that the secret is set to, or null if the user declines to overwrite the existing value.

Example

from cto_ai import sdk, ux

key = sdk.set_secret("secret1", "it's a secret to everybody")
ux.print(key)

Output

secret1

sdk.track

sdk.track(tags, metadata) -> str

Send an workflow events to The Ops Platform.

  • tags: (String || String[]) Tags for the event you want to track
  • metaData: (Object) Data you want to be tracked

sdk.events

sdk.events(starttime, endtime?) -> list

Retrieve workflow events from The Ops Platform.

Starttime and endtime can be specified as ISO format dates, for example "2020-05-12T20:47:45Z".

🚀 What's next?

  • To learn more about the UX options available for you, please go to Python UX page.
  • To see which prompts we offer for this SDK, you can go to Python Prompts page.
  • Add Secrets Management page to the Password and Secrets prompts.