Setup webhooks using existing DRF Serializers
Project description
Django Rest Framework - Webhooks
Configurable webhooks based on 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.update
does 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
created
anddeleted
within the session, then no webhook is sent for that model instance - If a model instance is
created
and then alsoupdated
within the session, then acreated
event is sent with the data from the lastupdated
signal. Only one webhook even is sent - If a models instance is
updated
multiple 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. Currently this must be done by setting
signal_model_instance_base_getters
Example:
from auth.models import User
from core.models import Address, Landlord, RentalUnit, Tenant
from drf_webhooks.utils import ModelSerializerWebhook
class DepositSerializerWebhook(ModelSerializerWebhook):
serializer_class = DepositSerializer
base_name = 'core.deposit'
@staticmethod
def get_address_instance_base(address):
tenants = address.tenant_set.all()
for tenant in tenants:
yield from tenant.deposits.all()
unit = getattr(address, "unit", None)
if unit:
yield from address.unit.deposits.all()
# Monitor changes to data in nested serializers:
signal_model_instance_base_getters = {
Tenant: lambda x: x.deposits.all(),
User: lambda x: x.tenant.deposits.all(),
RentalUnit: lambda x: x.deposits.all(),
Address: get_address_instance_base,
Landlord: lambda x: [], # Not important for this hook
}
...
class DepositSerializer(serializers.ModelSerializer):
tenant = TenantSerializer(read_only=True)
landlord = LandlordSerializer(read_only=True)
unit = RentalUnitSerializer(read_only=True)
class Meta:
model = Deposit
fields = [
'id',
'created',
'tenant',
'landlord',
'unit',
'date_from',
'date_to',
'security_deposit_amount',
'last_months_rent_amount',
'fee_rate',
'fee_amount',
'status',
'initiator',
]
...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
drf_webhooks-0.1.5.tar.gz
(13.9 kB
view details)
Built Distribution
File details
Details for the file drf_webhooks-0.1.5.tar.gz
.
File metadata
- Download URL: drf_webhooks-0.1.5.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ca1e8434455b9405fb7aac139bf1fefd6972fae89aae6c783aa21ab6988f3b3 |
|
MD5 | 9e223d043aad7712693ea757fe6b23e7 |
|
BLAKE2b-256 | 82467a9ffafecf9cc43088ba52376a9c6f1fb0bdda421912861231bfcea2e071 |
File details
Details for the file drf_webhooks-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: drf_webhooks-0.1.5-py3-none-any.whl
- Upload date:
- Size: 17.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.2 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7d321cf0f5dfc41fd351e0c64a11e548f3facc0e4315b170a5b1865dc02cb29 |
|
MD5 | e2708b563a450fd4db104d52d227cc96 |
|
BLAKE2b-256 | 2e281d03f1d95840740a638c43a610816800a1a07a8a8743774300b7fd65262c |