Django Rest Framework - Webhooks
Configurable webhooks for DRF Serializers
Goals:
- Use existing DRF Serializers from REST API to serialize data in webhooks
- Consistent data formatting
- Reusable OpenAPI schemas
- Configurable webhooks that simply work (by way of django signals magic) without the developer having to keep track of where to trigger them
- Still allow for "manual" triggering of webhooks
- This is useful because signals aren't always triggered
- For example:
QuerySet.updatedoes not trigger signals
- Still allow for "manual" triggering of webhooks
- Disable webhooks using context managers
- This can be useful when syncing large chunks of data
- or with a duplex sync (when two systems sync with each other) to avoid endless loops
- Webhook Signal Session
- A context manager gathers all models signals and at the end of the session only triggers the resulting webhooks
- If a model instance is both
createdanddeletedwithin the session, then no webhook is sent for that model instance - If a model instance is
createdand then alsoupdatedwithin the session, then acreatedevent is sent with the data from the lastupdatedsignal. Only one webhook even is sent - If a models instance is
updatedmultiple times within the session, then only one webhook event is sent
- If a model instance is both
- Middleware wraps each request in Webhook Signal Session context
- NOTE: The developer will have to call the context manager in code that runs outside of requests (for example in celery tasks) manually
- A context manager gathers all models signals and at the end of the session only triggers the resulting webhooks
- Automatically determine which nested models need to be monitored for changes
Examples:
from django.db import models
from drf_webhooks import ModelSerializerWebhook, register_webhook
from rest_framework import serializers
class MyModel(models.Model):
name = models.CharField(max_lenght=100)
class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = ['id', 'name']
# Automatic:
register_webhook(MyModel)()
# ---- OR ----
# If you need more configuration:
@register_webhook(MyModel)
class MyModelWebhook(ModelSerializerWebhook):
base_name = 'core.my_model'
Documentation:
Quckstart:
Install drf-webhooks
poetry add drf-webhooks
# ... or ...
pip install drf-webhooks
Update settings.py:
INSTALLED_APPS = [
# ...
'drf_webhooks',
]
MIDDLEWARE = [
# ...
'drf_webhooks.middleware.WebhooksMiddleware',
]
# This is required if you don't want your database to fill up with logs:
CELERY_BEAT_SCHEDULE = {
'clean-webhook-log': {
'task': 'drf_webhooks.tasks.auto_clean_log',
'schedule': 60,
'options': {'expires': 10},
},
}
Create a new django app
Recommended app name: webhooks
# ----------------------------------------------------------------------
# apps.py
# ----------------------------------------------------------------------
from django.apps import AppConfig
class WebhooksAppConfig(AppConfig):
name = "<your module name>"
label = "webhooks"
# ----------------------------------------------------------------------
# models.py
# ----------------------------------------------------------------------
from django.contrib.auth import get_user_model
from django.db import models
from django.utils.translation import gettext_lazy as _
from drf_webhooks.models import AbstractWebhook, AbstractWebhookLogEntry
class Webhook(AbstractWebhook):
# This can also be a group or an organization that the user belongs to:
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
def __str__(self):
return 'id=%s, events=%s, owner=%s' % (
str(self.id),
', '.join(self.events),
str(self.owner),
)
class WebhookLogEntry(AbstractWebhookLogEntry):
# This can also be a group or an organization that the user belongs to:
owner = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)
Metadata
Release files for drf-webhooks 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_webhooks-0.2.0.tar.gz | 15.6 kB | Without provenance | Details for drf_webhooks-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_webhooks-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for drf_webhooks-0.2.0-py3-none-any.whl |
Total release size: 34.9 kB
Release files / drf_webhooks-0.2.0.tar.gz
| Download URL | drf_webhooks-0.2.0.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
182ed328bb318c5aee947c63874a094229c62f1324a3f2061751f6c792e80635
|
|
BLAKE2b-256 checksum How to use checksums |
43bb34eedab40dad3e40c9c57d95f78f69b8308dfb129928e35abaa0a72394dc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.2 Darwin/21.4.0
|
Release files / drf_webhooks-0.2.0-py3-none-any.whl
| Download URL | drf_webhooks-0.2.0-py3-none-any.whl |
|---|---|
| Size | 19.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
969e82cbf4299a562cea57bbe32a5e08dee9ccc514f0f9e2bab9a1dc15debc58
|
|
BLAKE2b-256 checksum How to use checksums |
6529418beacb90df30b243fb509c8e8f6cbb430f5979b8059fefe44731975abc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.3.2 CPython/3.10.2 Darwin/21.4.0
|
Security for drf-webhooks 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.