Last active
November 25, 2015 01:09
-
-
Save kylef/144747 to your computer and use it in GitHub Desktop.
A HTCPCP Implementation for Django
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from django.http import HttpResponse | |
class HTCPCPMiddleware(object): | |
""" | |
This middleware allows a your django project to act as a HTCPCP (Hyper | |
Text Coffee Pot Control Protocol) server. If the user requests to BREW | |
a coffee, and it is not on the list. Then it will get a `406 Not | |
Acceptable`, and randomly it will respond with `418 I'm a teapot`. | |
If the request is not a HTCPCP (BREW) Request then the middleware will | |
quit leaving django to handle the request. | |
""" | |
coffee_list = [ | |
'/espresso/', | |
'/espresso/americano/', | |
'/espresso/lungo/', | |
'/espresso/flat-white/', | |
'/black-coffee/', | |
'/latte/', | |
'/ristretto/', | |
'/cappuccino/' | |
] | |
def process_request(self, request): | |
if request.method != 'BREW' or request.method != 'WHEN': | |
return | |
if randint(-10, 5) > 0: | |
return HttpResponse('I\'m a teapot short and stout.', status=418) | |
if request.method == 'WHEN': | |
return HttpResponse('Your coffee is complete.') | |
if request.path in self.coffee_list: | |
return HttpResponse('The coffee is brewed, please send a WHEN request when we should stop pouring the milk.') | |
else: | |
return HttpResponse('Unrecognised coffee, %s' % ', '.join(self.coffee_list), status=406) |
I was wondering, why it does not work for me! Then I noticed you have a typo in url, it should be /black-coffee/ :-<
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A little typo on line 30 'Unreconised' should be 'Unrecognised'