Django + Webhooks 简化版
项目描述
Django + Webhooks 简化版
完整文档位于 https://dj-webhooks.readthedocs.org。
需求
Python 2.7.x 或 3.3.2 或更高版本
django>=1.5.5
django-jsonfield>=0.9.12
django-model-utils>=2.0.2
django-rq>=0.6.1
webhooks>=0.3.1
快速入门
安装dj-webhooks
pip install dj-webhooks
配置一些Webhook事件
# settings.py
WEBHOOK_EVENTS = (
"purchase.paid",
"purchase.refunded",
"purchase.fulfilled"
)
添加一些Webhook目标
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(username="pydanny")
from webhooks.models import Webhook
WebhookTarget.objects.create(
owner=user,
event="purchase.paid",
target_url="https://mystorefront.com/webhooks/",
header_content_type=Webhook.CONTENT_TYPE_JSON,
)
然后在项目中使用它
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(username="pydanny")
from djwebhooks.decorators import hook
from myproject.models import Purchase
# Event argument helps identify the webhook target
@hook(event="purchase.paid")
def send_purchase_confirmation(purchase, owner): # Webhook_owner also helps identify the webhook target
return {
"order_num": purchase.order_num,
"date": purchase.confirm_date,
"line_items": [x.sku for x in purchase.lineitem_set.filter(inventory__gt=0)]
}
for purchase in Purchase.objects.filter(status="paid"):
send_purchase_confirmation(purchase=purchase, owner=user)
在队列中使用django-rq
假设您正在运行Redis并且已经配置了django-rq
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.get(username="pydanny")
# import redis hook
from djwebhooks.decorators import redis_hook
from myproject.models import Purchase
# Event argument helps identify the webhook target
@redis_hook(event="purchase.paid")
def send_purchase_confirmation(purchase, owner): # Webhook_owner also helps identify the webhook target
return {
"order_num": purchase.order_num,
"date": purchase.confirm_date,
"line_items": [x.sku for x in purchase.lineitem_set.filter(inventory__gt=0)]
}
for purchase in Purchase.objects.filter(status="paid"):
job = send_purchase_confirmation(purchase=purchase, owner=user)
特性
同步Webhook
通过Django ORM进行投递跟踪。
异步Webhook的选项。
计划中的特性
通过Redis和其他快速写数据存储进行投递跟踪。
历史
0.2.1 (2014-05-17)
删除了conf.py文件,因为它只增加了抽象。
显式导入钩子,使设置管理更简单。
删除了utils.py,因为我们不再进行复杂的动态导入(请参阅前面的项目符号)。
覆盖率现在为100%
0.2.0 (2014-05-15)
重构了发送者,使其非常易于扩展。
添加了基于ORM的发送者。
添加了基于Redis的发送者,它使用django-rq。
添加了redis-hook装饰器。
添加了管理视图。
提高测试覆盖率到89%。
setup.py现在包含所有依赖项。
0.1.0 (2014-05-12)
PyPI上的第一个发布。
项目详情
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源代码发行版
dj-webhooks-0.2.1.tar.gz (10.5 kB 查看哈希值)
构建发行版
dj_webhooks-0.2.1-py2.py3-none-any.whl (12.9 kB 查看哈希值)
关闭
dj-webhooks-0.2.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1e345baa6c03563c41d391206fd5461a346706610e6fa9fbb3fb7d981b8e31e8 |
|
MD5 | 87f8ac81725090dbdf61cae40965328b |
|
BLAKE2b-256 | fa8b355c730b6f4e0ebd48ed51a52e5ed2dc8e80be5b09465d369062e5eeab4f |
关闭
dj_webhooks-0.2.1-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 95d3c987e3a1655c402d55e1f3627a3d63eaa67e949b71699e253c9be364a27b |
|
MD5 | fefb968aa6195add3247359a6562a3eb |
|
BLAKE2b-256 | 3b00168e6e6eec2cd5be07af9bcc8cb07ad55e47a787b6a59ef89c212e372ee6 |