wichert / rest_toolkit

Simple REST servers
http://rest-toolkit.rtfd.org/
BSD 2-Clause "Simplified" License
36 stars 7 forks source link

Added add a subresource decorator #19

Open jajadinimueter opened 8 years ago

jajadinimueter commented 8 years ago

This was added to avoid repeating the route path prefix for subresources like /users/1/photos.

Here is an example (from the docs):

from rest_toolkit import resource

PHOTOS = {1: ['...']}

@resource('/users/{id}')
class User(object):
   def __init__(self, request):
       self.user_id = int(request.matchdict['id'])

@User.resource('/photos')
class UserPhotos(User):
   pass

@UserPhotos.GET()
def get_user_photos(resource, request):
   return PHOTOS.get(resource.user_id, [])

Do you think this is a good idea or no? I think the subclassing should still be explicit for clarity. If you like the change I would add a test and docs.

wichert commented 7 years ago

It's certainly an interesting idea. I would suggest using subresource for the decorator name to make it more explicit.