"I think most people just make the mistake that it should be simple to design simple things. In reality, the effort required to design something is inversely proportional to the simplicity of the result. As architectural styles go, REST is very simple."
It's not about eye candy. If we design our APIs to be browse-able, they will be:
def view(request):
if request.method == 'GET':
# do stuff
elif request.method == 'PUT':
# do other stuff
from django.views.generic import View
class MyView(View):
def get(self, request):
# do stuff
def put(self, request):
# do other stuff
View classes composed of a few mixins:
class MyView(RequestMixin, View):
parsers = (JSONParser,)
def post(self, request):
foo = self.DATA
...
class MyView(ResponseMixin, View):
renderers = (JSONRenderer, XMLRenderer)
def get(self, request):
...
if not exists:
return Response(status.HTTP_404_NOT_FOUND, {'reason': 'whatever'})
return {'foo': 'bar'}
class MyView(AuthMixin, View):
authentication = (BasicAuthentication, DigestAuthentication)
permissions = (IsUserOrIsAnonReadOnly, )
def get(self, request):
# can always get here
def put(self, request):
# can only get here if we're authenticated
class MyResource(ModelResource):
model = MyModel
fields = ('foo', 'bar', 'baz')
class MyView(ResourceMixin, RequestMixin, ResponseMixin, View):
def get(self, request):
...
return instance
def put(self, request):
foo = self.CONTENT
...
class MyView(View):
form = MyForm
def post(self request):
stuff = self.CONTENT
...
return other_stuff
class MyResource(ModelResource):
model = MyModel
urlpatterns = patterns('',
url(r'^$', ListOrCreateModelView.as_view(resource=MyResource)),
url(r'^(?P<pk>[^/]+)/$', InstanceModelView.as_view(resource=MyResource)),
)
Table of Contents | t |
---|---|
Source Files | s |
Slide Numbers | n |
Notes | 2 |
Help | h |