在Label Studio中为Webhook创建自定义事件
如果你想在Label Studio中为webhooks触发自定义事件,可以扩展webhook事件模型。
创建自定义webhook事件
要创建自定义webhook事件,请将您自己的操作添加到WebhookActions
模型中。
例如:
class WebhookAction(models.Model):
...
SOMETHING_HAPPENED = 'SOMETHING_HAPPENED'
...
ACTIONS = {
SOMETHING_HAPPENED: {
'name': _('Something happened'),
'description': _("A thing happened. We wanted to let you know."),
'key': 'something',
},
...
...
在WebhookAction
类中声明动作及相关属性和负载详情后,在代码中触发该动作的位置调用事件动作。例如:
...python
result = do_something()
emit_webhooks(organization, WebhookAction.SOMETHING_HAPPENED, {'something': [result]})
...
您可以使用Organization.objects.first()
来获取组织详情。
使用Python函数调用事件操作
您可以使用以下函数来调用事件动作。请参考下表:
Python函数 | 使用场景 | 附加说明 |
---|---|---|
get_active_webhooks() |
Get all active webhooks. | |
run_webhook() |
Run one webhook and pass some payload. | |
emit_webhooks() |
Send requests for all webhooks for an action. | |
emit_webhooks_for_instances() |
Send serialized instances with webhook requests. | You must declare a serializer in the WebhookAction.ACTIONS model. |
在API源代码中使用装饰器调用事件操作
您可以在CRUD REST方法中使用装饰器来向webhooks发送事件动作。您可以使用以下内容:
装饰器语法 | 使用场景 | 详情 |
---|---|---|
@api_webhook() |
POST /PUT /PATCH requests |
Expects a response with an id and uses the .get_object() function after the request to send that information. |
@api_webhook_for_delete() |
DELETE |
Sends only the id field after a successfully delete operation. |