Working with endpoints for the API

We want to create an endpoint for the root of our API to make it easier to browse the API with the browsable API feature and understand how everything works. Add the following code to the file to declare the ApiRoot class. The code file for the sample is included in the restful_python_chapter_02_03 folder.

class ApiRoot(generics.GenericAPIView): 
    name = 'api-root' 
    def get(self, request, *args, **kwargs): 
        return Response({ 
            'players': reverse(, request=request), 
            'game-categories': reverse(, request=request), 
            'games': reverse(, request=request), 
            'scores': reverse(, request=request) 

The ApiRoot class is a subclass of the rest_framework.generics.GenericAPIView ...

Get Building RESTful Python Web Services now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.