You can install the official Unicat client lib from PyPI.
pip install unicat
import sys
from unicat import Unicat
from .env import project_gid, local_folder
import keyring
secret_api_key = keyring.get_password("unicat-my-project", "my-key-name")
unicat = Unicat("https://unicat.app", project_gid, secret_api_key, local_folder)
if not unicat.connect():
print("Invalid connection settings")
sys.exit(1)
You'll need the project key (a gid
) and the API key. The local folder is used for uploads and downloads (for assets).
Once we have the connection, we can explore the project and its settings:
project = unicat.project
print(project.name)
print(project.owner.username)
print(project.languages)
print(project.channels)
print(project.orderings)
print(project.fieldlists)
language = project.default_language
channel = unicat.project.channels["Main channel"] # or unicat.project.default_channel
ordering = unicat.project.orderings["Main ordering"] # .default_ordering
fieldlist = unicat.project.fieldlists["Main fieldlist"] # .default_fieldlist
Once we have the connection, we can iterate over all the fields, for example:
for field in unicat.walk_fields():
print(field.name, field.type)
There are a number of walk_…
methods where we iterate over a list, or over a resultset.
If we want to download all assets:
for asset in unicat.walk_asset_tree():
if asset.is_file:
asset.download()
Or, if we want to go through all records that have a definition named 'brand', then print all the field names and values:
brand_definition = unicat.get_definition_by_name("brand")
for totalsize, record in unicat.walk_record_query(
language,
DuckObject(
q="",
filter=["definition", "is", brand_definition.gid],
),
):
fields = record.fields[language]
for field in fields:
print(field.field.name, field.value)
Let's print the whole record tree, with fieldlist fields for each article record:
for record in unicat.walk_record_tree():
print(record.treelevel, record.definition.name, record.title[language])
if record.definition.name == "article":
fields = record.fields[language]
for field in record.definition.fieldlists[fieldlist]:
print(field.field.name, field.value)
unicat.mutate.update_record(record, {language: {"price": 19.95}})
For a reference, see the Python Unicat SDK documentation.