Code Monkey home page Code Monkey logo

barker's Introduction

Barker

Barker is a python module for notifying other servers using the webhook protocol. Webhooks are a way for a web server to communicate out to another server without direct user interaction. They can be used for notifications or triggers to start other services.

Initialization

To use Barker, initialize the Webhook object:

from barker import Webhook
hook = Webhook()

The Webhook object has a number of attributes set by the user (either in the initialization call or afterwards):

  1. url: the url you want to send the webhook notification
  2. data: the information you want to post in the webhook
  3. timeout: the number of seconds to wait for a server response
  4. key: the hashing key used to verify message data. If not given, it defaults to BARKER.

Additionally, some attributes are set by Barker:

  1. headers: custom HTTP headers containing information for message verification (BARKER_SIGNATURE and BARKER_TIMESTAMP)
  2. response: the response received from the server as a requests.Response object

Sending

Once the object is populated with url and data at a minimum, it can be sent by calling the send method:

hook.send()
print(hook.response)

Receiving

When a webhook is sent, it will include a signature in a header (BARKER_SIGNATURE) that is an HMAC hash of the time the webhook was generated (also a header, BARKER_TIMESTAMP) and the data joined by a ";". Local verification ensures the message is both correct and current.

Here is a snippet of code that can be used to verify message integrity. This assumes you have set the received headers to a headers variable, received data to a data variable, and hash key provided to the sender to hash_key (or the string 'BARKER' if none supplied).

hash = hmac.new(bytes(hash_key, 'utf-8'))
hash.update(bytes(";".join((headers['BARKER_TIMESTAMP'], data.decode('utf-8'))), 'utf-8'))
print(hmac.compare_digest(headers['BARKER_SIGNATURE'], hash.hexdigest()))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.