Created
January 20, 2012 00:23
-
-
Save bremac/1643962 to your computer and use it in GitHub Desktop.
Django Piston Example
This file contains hidden or 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 piston.handler import BaseHandler | |
from models import Apple | |
class AppleHandler(BaseHandler): | |
exclude = () # Ensure that the ID is shown. | |
allowed_methods = ('GET', 'POST', 'PUT', 'DELETE') | |
model = Apple |
This file contains hidden or 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 django.db import models | |
class Apple(models.Model): | |
type = models.CharField(max_length=30) | |
weight = models.CharField(max_length=255) |
This file contains hidden or 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
curl 127.0.0.1:8000/apple/api/apple/ | |
# [] | |
curl -X POST -d '{"type": "Red Delicious", "weight": "heavy"}' 127.0.0.1:8000/apple/api/apple/ -H 'Content-Type:application/json' | |
# { | |
# "type": "Red Delicious", | |
# "id": 1, | |
# "weight": "heavy" | |
# } | |
curl 127.0.0.1:8000/apple/api/apple/ | |
# [ | |
# { | |
# "type": "Red Delicious", | |
# "id": 1, | |
# "weight": "heavy" | |
# } | |
# ] | |
curl -X POST -d '{"type": "Empire", "weight": "middling"}' 127.0.0.1:8000/apple/api/apple/ -H 'Content-Type:application/json' | |
# { | |
# "type": "Empire", | |
# "id": 2, | |
# "weight": "middling" | |
# } | |
curl 127.0.0.1:8000/apple/api/apple/ | |
# [ | |
# { | |
# "type": "Red Delicious", | |
# "id": 1, | |
# "weight": "heavy" | |
# }, | |
# { | |
# "type": "Empire", | |
# "id": 2, | |
# "weight": "middling" | |
# } | |
# ] | |
curl -X PUT -d '{"type": "Yellow Delicious"}' 127.0.0.1:8000/apple/api/apple/1/ -H 'Content-Type:application/json' | |
# OK | |
curl 127.0.0.1:8000/apple/api/apple/ | |
# [ | |
# { | |
# "type": "Yellow Delicious", | |
# "id": 1, | |
# "weight": "heavy" | |
# }, | |
# { | |
# "type": "Empire", | |
# "id": 2, | |
# "weight": "middling" | |
# } | |
# ] | |
curl -X POST -d '{"type": "Spartan", "weight": "middling"}' 127.0.0.1:8000/apple/api/apple/ -H 'Content-Type:application/json' | |
# { | |
# "type": "Spartan", | |
# "id": 3, | |
# "weight": "middling" | |
# } | |
curl 127.0.0.1:8000/apple/api/apple/weight/middling/ | |
# [ | |
# { | |
# "type": "Empire", | |
# "id": 2, | |
# "weight": "middling" | |
# }, | |
# { | |
# "type": "Spartan", | |
# "id": 3, | |
# "weight": "middling" | |
# } | |
# ] | |
curl -X DELETE 127.0.0.1:8000/apple/api/apple/2/ -H 'Content-Type:application/json' | |
curl 127.0.0.1:8000/apple/api/apple/ | |
# [ | |
# { | |
# "type": "Yellow Delicious", | |
# "id": 1, | |
# "weight": "heavy" | |
# } | |
# ] |
This file contains hidden or 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 django.conf.urls.defaults import patterns, include, url | |
from piston.resource import Resource | |
from handlers import AppleHandler | |
apple_resource = Resource(handler=AppleHandler) | |
urlpatterns = patterns('', | |
url(r'^api/apple/(?P<weight>[^/]+)/$', apple_resource), | |
url(r'^api/apple/(?P<id>\d+)/$', apple_resource), | |
url(r'^api/apple/$', apple_resource), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment