Skip to content

feat: Add example to docs #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/example/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ already constructed a :class:`Session`::
invite_user
workflow_schema
group_by
webhooks

65 changes: 65 additions & 0 deletions doc/example/webhooks.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
..
:copyright: Copyright (c) 2023 ftrack

.. _example/webhooks:

*********************
Working with Webhooks
*********************

.. currentmodule:: ftrack_api.session

Webhooks are a way to get notified when something happens in ftrack. The API
exposes a `Webhook` object that can be used to create and manage webhooks.

The following example shows how to create a webhook that will be triggered when
a task is created::

# Create a new automation.
automation = session.create('Automation', {
'name': 'Task created',
})

# Create a trigger.
trigger = session.create('Trigger', {
'automation_id': automation['id'],
'filter': 'entity.entity_type = "Task" and entity.operation = "create"',
})

# Create the webhook.
webhook = session.create('WebhookAction', {
'automation_id': automation['id'],
"webhook_url": "https://my-custom-webhook-url.com",
})

session.commit()

The trigger has a filter that will be evaluated when an event happens in ftrack.
If the filter evaluates to true, the webhook will be triggered. The filter operates on
the event data and can be used to filter on any data in the event.
The whole event is included webhook when sent.

The filter supports basic syntax like `and`, `or`, `=` and `!=`.

The following example shows how to create trigger for when
the status of an AssetVersion changes to "Pending Approval"::

# Get the status id for "Pending Approval".
status_id = session.query(
'select id from Status where name is "Pending Approval"'
).one()['id']

# Create a trigger.
trigger = session.create('Trigger', {
'automation_id': automation['id'],
'filter': f'entity.entity_type = "AssetVersion" and entity.operation = "update" and entity.new.status_id != entity.old.status_id and entity.new.status_id = "{status_id}"',
})

In the initial release of webhooks, only entity events are supported and
only operations `create` and `update`. The events contain all
the direct properties from the API schemas for the entity in entity.new and
entity.old. The event does not contain any data for related entities or
computed properties such as URLs. For more information on what entities
exist and their properties see the API reference in your workspace. The
reference can be found by clicking your own icon in the top left corner and then
select Help.