Package: django-zapier-trigger
Django Zapier TriggersDjango (DRF) app for managing Zapier triggers.Version supportThis app supports Django 3.2+ and Python 3.10+.BackgroundThis app provides the minimal scaffolding required to support Zapier triggers in your Django application. It is based on DRF.In addition to thezapier.triggersDjango app, this project includes two additional applications: a complete Zapier CLI application that you can publish to Zapier, and a Demo project that provides the Django support for it. With these two projects you have a complete end-to-end Zapier integration.Zapier TriggersA Zapier trigger is an event source for Zapier workflows ("Zaps"), that can operate in one of two modes - "Instant", or "Polling". Either way the net result is that JSON data objects are received by Zapier and can be used as the first step in a Zap.There is alotof documentation online from Zapier about how to create a trigger, and I would strongly recommend reading it before attempting to build your own. Here are a couple of good articles to start with:https://platform.zapier.com/docs/starthttps://platform.zapier.com/cli_tutorials/getting-startedPrequisitesIf you want to run the end-to-end demo you will need:A Zapier accountThe Zapier CLIngrok, or some equivalent tunnelling softwareWhat's in the box?The core implementation detail of this package is theTriggerView. This is a DRFAPIViewclass that handlesGET,POST, andDELETEmethods, mapping them to the Zapier trigger methods for polling ("list"), susbscribe and unsubscribe functions.GET /triggers/{{trigger}}/When Zapier makes aGETrequest to your application endpoint one of two things is happening. For a REST Hook ("Instant") trigger this is request sample data that Zapier can use to create its Zap builder UI. If your trigger is a push ("Instant") then you can just return static data - as long as it conforms to the same schema as real data. Thedemo.views.new_bookview demonstrates this.If your trigger is a polling trigger then this endpoint should return real data - thedemo.views.new_filmview is an example of this.The view returns a200status code.POST /triggers/{{trigger}}/subscriptions/When Zapier makes aPOSTrequest it is expecting to create a new webhook (rebranded "REST Hook" by Zapier) susbscription. This is handled automatically by the view, which creates a newTriggerSubscriptionobject for the user + trigger combination, and returns theuuidproperty to Zapier, which stores it in itsbundle.subscriptionData.idproperty.The view returns a201status code.DELETE /triggers/{{trigger}}/subscriptions/{{subscription_id}}When Zapier makes aDELETErequest it is expecting to delete the subscription identified by thesubscription_idvalue, which maps to theuuidproperty. We do not delete the subscription but instead mark it as "inactive". This is because we record all of the event data that is sent by a trigger subscription, and we we want to keep this for a period for auditing purposes. If a newPOSTrequest is made for the same user + trigger combination the subscription is reactivated.The view returns a204status code.SettingsThe settings are all read in from the Django settingZAPIER_TRIGGER, which is a dict containing the following keys:STRICT_MODEThe JSON key used to extract the Zapier subscription URL endpoint in the body of thePOSTrequest - defaults tohookUrl.TRIGGERSThis is a dict containing the name of the trigger and a string path to a view-like function that must accept a singleRequestarg and return a list of JSON-serializable dict objects. Every trigger that your Zapier app supports must be in this setting - otherwise any request made to/triggers/{{trigger}}will return a404.Demo + zapier-appThe easiest way to work out how this all fits together is to run the demo app and push the zapier-app to Zapier under your own account.