跳转到主要内容

Django + Webhooks 简化版

项目描述

https://pypip.in/d/dj-webhooks/badge.png https://badge.fury.io/py/dj-webhooks.png Wheel Status https://travis-ci.org/pydanny/dj-webhooks.png?branch=master

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 查看哈希值)

上传时间 Python 2 Python 3

支持