Acknowledgement
Many thanks to @Danell
His posts encouraged me to create this guide.
Disclaimer
This post aims to scratch under the surface of ayon-python-api
where you learn about how to make the requests yourself.
If you want a simpler guide, go to AYON Python API First Steps
TL;DR
Go to the Extended Example Code section.
Intro
AYON API
allows communication with AYON
Server via code.
From a user perspective, it seems like magic when you hit a button and things start to work automatically!
Let’s shed some light on the AYON API
and unlock one of the greatest powers of AYON
.
Get Started
Probably, the easiest way is to perform the requests manually.
For this example, We are going to use postman or Thunder Client - VS Code
Both almost have the same UI!
Get Authentication Token
By making a login request
- Write a login request (
<your-ayon-server-url>/api/auth/login
) - Add name and password
- Send!
- Find the token.
Thunder Client | Postman |
Your first API call
Let’s ask AYON for the available projects, List Projects request.
- Write a login request (
<your-ayon-server-url>/api/projects
) - Add your token
- Send!
- Find the available projects.
Make API calls from code
There are many libraries to make API calls.
For this guide we are going to use httpx
It’s as simple as
import httpx
ayon_server_url = "<your-ayon-server-url"
username = "<your-user-name>"
password = "<your-password>"
req = f"{ayon_server_url}/api/auth/login"
response = httpx.post(
req,
json={
"name": username,
"password": password
}
)
token = response.json().get("token")
req = f"{self.ayon_server_url}/api/projects"
response = httpx.get(
req,
headers={
"Authorization": f"Bearer {self.token}"
}
)
projects = response.json().get("projects")
# you can write some code to process the result.
# But, I'll just print the result as it is.
print(projects)
AYON API day-to-day usage
How using AYON API can be useful in my day to day work ?
Well, it can speed up your dev process a lot!
E.g. I use it on daily basis to upload addons Instead of doing it manually!
where I run this in terminal.
python E:\Ynput\ayon-houdini\create_package.py
python upload-addon.py --addon E:\Ynput\ayon-houdini
Find my
upload-addon.py
script here
These commands do the following:
- Create
houdini.#.#.#.zip
- Delete the Houdini addon with the same version.
- Upload the addon’s zip file.
Before using my upload-addon.py
script, I used a simpler script simple_ayon_rest_commands.py
that expects simple_ayon_rest_commands.json
In simple_ayon_rest_commands.py
, I’m using my class SimpleAyonRestCommands
. find its definition in the Extended Example Code.
# fetch ayon server credentials
filepath = __file__.replace(".py", ".json")
with open(filepath, "r") as fp:
data = json.load(fp)
username = data["username"]
password = data["password"]
ayon_server_url = data["ayon_server_url"]
addon_path = data["addon_path"]
my_ayon = SimpleAyonRestCommands(ayon_server_url,
username,
password)
file_name = os.path.basename(addon_path).rstrip(".zip")
addon_name, addon_version = file_name.split("-", 1)
## Delete and addon by version
print("Delete addon: {}, version: {}".format(addon_name, addon_version))
my_ayon.delete_addon_version(addon_name, addon_version)
## Upload Addon from file on disk
print("Upload addon: {}, version: {}".format(addon_name, addon_version))
res = my_ayon.upload_addon_zip_file(addon_path)
print (res)
Extended Example Code
You can find an extended example here