Skip to content

Commit

Permalink
Added travis, coveralls, and async sender #2, #4, #5
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed May 13, 2014
1 parent 0a680a6 commit 7dfecdc
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
service_name: travis-pro
repo_token: nLWjoaBWJ6HpMFDq10AlCwoVMlGZxEV5W
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

language: python

services:
- redis

python:
- "3.3"
- "2.7"
Expand Down
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
History
-------

0.3.0 (2014-05-??)
++++++++++++++++++

* Added async_redis sender


0.2.0 (2014-05-13)
++++++++++++++++++

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ test-all:

coverage:
py.test --cov webhooks
coverage html
open htmlcov/index.html

docs:
Expand Down
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ webhooks
:alt: Build Status
:target: https://travis-ci.org/pydanny/webhooks

.. image:: https://coveralls.io/repos/pydanny/webhooks/badge.png
:target: https://coveralls.io/r/pydanny/webhooks

.. image:: https://pypip.in/wheel/webhooks/badge.png
:target: https://pypi.python.org/pypi/webhooks/
:alt: Wheel Status
Expand All @@ -34,12 +37,13 @@ Existing Features

* Easy to integrate into any package or project
* Comes with several built-in senders for synchronous webhooks.
* Comes with a RedisQ-powered asynchronous webhook.
* Extendable functionality through the use of custom senders and hash functions.

Planned Features
-----------------

* Comes with numerous built-in senders for synchronous and asynchronous webhooks.
* Comes with many built-in senders for synchronous and asynchronous webhooks.
* Special functions for combining multiple sends of identical payloads going to one target into one.
* Follows http://resthooks.org patterns
* Great documentation
Expand Down
1 change: 1 addition & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# webhook dependencies
wrapt>=1.8.0
requests>=2.2.1
rq>=0.4.5

# Webhook test dependencies
coverage
Expand Down
20 changes: 20 additions & 0 deletions tests/test_webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


from webhooks import webhook, unhashed_hook
from webhooks.senders import async_redis
from webhooks.senders import simpleprint
from webhooks.senders import targeted

Expand Down Expand Up @@ -58,3 +59,22 @@ def basic(url, wife, husband):
assert status['husband'] == "Daniel Roy Greenfeld"
assert len(status['hash']) > 10


def test_async():

# First, test the async.worker function
@webhook(sender_callable=async_redis.worker)
def worker(url, language):
return {"language": language, "url": url}

status = worker(url="http://httpbin.org/post", language="python")

assert status['language'] == "python"
assert len(status['hash']) > 10

# Second, test the sender, which handles the async components
@webhook(sender_callable=async_redis.sender)
def sender(url, language):
return {"language": language, "url": url}

sender(url="http://httpbin.org/post", language="python")
79 changes: 79 additions & 0 deletions webhooks/senders/async_redis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-

import json
import logging
from time import sleep

import requests
from redis import Redis
import rq
from rq.decorators import job

from ..encoders import WebHooksJSONEncoder


logging.basicConfig(filename="async.log", level=logging.DEBUG)

connection = Redis()
q = rq.Queue(connection=connection)


@job('default', connection=connection, timeout=5)
def worker(wrapped, dkwargs, hash_value=None, *args, **kwargs):

# First element is 0 to represent the first attempt
# TODO - pass this in as a decorator argument
ATTEMPTS = [0, 1, 2, 3, 4]

# Get the URL from the kwargs
url = kwargs.get('url', None)
if url is None:
url = dkwargs['url']

# Create the payload by calling the hooked/wrapped function.
payload = wrapped(*args, **kwargs)

# Add the hash value if there is one.
if hash_value is not None and len(hash_value) > 0:
payload['hash'] = hash_value

# Dump the payload to json
data = json.dumps(payload, cls=WebHooksJSONEncoder)

for attempt in range(len(ATTEMPTS) - 1):
# Print each attempt. In practice, this would write to logs
msg = "Attempt: {attempt}, {url}\n{payload}".format(
attempt=attempt,
url=url,
payload=data
)
logging.debug(msg)

# post the payload
r = requests.post(url, payload)

# anything with a 200 status code is a success
if r.status_code >= 200 and r.status_code < 300:
# Exit the sender function. Here we provide the payload as a result.
# In practice, this means writing the result to a datastore.
logging.debug("Success!")
return payload

# Log the current status of things and try again.
# TODO - add logs

# Wait a bit before the next attempt
sleep(attempt)
else:
logging.debug("Could not send webhook")

# Exit the sender function. Here we provide the payload as a result for
# display when this function is run outside of the sender function.
return payload


def sender(wrapped, dkwargs, hash_value=None, *args, **kwargs):

logging.debug("Starting async")
worker(wrapped, dkwargs, hash_value=None, *args, **kwargs)
logging.debug("Ending async")
4 changes: 4 additions & 0 deletions webhooks/senders/simpleprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def sender(wrapped, dkwargs, hash_value=None, *args, **kwargs):

# Loop through the attempts and log each attempt
for attempt in range(len(ATTEMPTS) - 1):

# Wait a bit before the next attempt
sleep(attempt)

# Print each attempt. In practice, this would either write to logs or
# submit to a write-fast DB like Redis.
print(
Expand Down
6 changes: 4 additions & 2 deletions webhooks/senders/targeted.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def sender(wrapped, dkwargs, hash_value=None, *args, **kwargs):

# Loop through the attempts and log each attempt
for attempt in range(len(ATTEMPTS) - 1):

# Wait a bit before the next attempt
sleep(attempt)

# Print each attempt. In practice, this would either write to logs or
# submit to a write-fast DB like Redis.
print(
Expand All @@ -64,8 +68,6 @@ def sender(wrapped, dkwargs, hash_value=None, *args, **kwargs):
# Log the current status of things and try again.
# TODO - add logs

# Wait a bit before the next attempt
sleep(attempt)
else:
raise Exception("Could not send webhook")

Expand Down

0 comments on commit 7dfecdc

Please sign in to comment.