Configuring throttling policies in the Django REST framework

Now, we will configure throttling policies for the class-based views related to drones: DroneList and DroneDetail. We will override the values for the following class attributes for the class-based views:

  • throttle_classes: This class attribute specifies a tuple with the names of the classes that will manage throttling rules for the class. In this case, we will specify the ScopedRateThrottle class as the only member of the tuple.
  • throttle_scope: This class attribute specifies the throttle scope name that the ScopedRateThrottle class will use to accumulate the number of requests and limit the rate of requests.

This way, we will make these class-based views work with the ScopedRateThrottle class and we will configure the throttle scope that this class will consider for each of the class based views related to drones.

Open the restful01/drones/views.py file and add the following lines after the last line that declares the imports, before the declaration of the DroneCategoryList class:

from rest_framework.throttling import ScopedRateThrottle  

Replace the code that declares the DroneDetail class with the following code in the same views.py file. The new lines are highlighted in the code listing. The code file for the sample is included in the hillar_django_restful_09_01 folder, in the restful01/drones/views.py file:

class DroneDetail(generics.RetrieveUpdateDestroyAPIView): 
    throttle_scope = 'drones' 
    throttle_classes = (ScopedRateThrottle,) 
    queryset = Drone.objects.all() 
    serializer_class = DroneSerializer 
    name = 'drone-detail' 
    permission_classes = ( 
        permissions.IsAuthenticatedOrReadOnly, 
        custompermission.IsCurrentUserOwnerOrReadOnly, 
        )

Replace the code that declares the DroneList class with the following code in the same views.py file. The new lines are highlighted in the code listing. The code file for the sample is included in the hillar_django_restful_09_01 folder, in the restful01/drones/views.py file:

class DroneList(generics.ListCreateAPIView): 
    throttle_scope = 'drones' 
    throttle_classes = (ScopedRateThrottle,) 
    queryset = Drone.objects.all() 
    serializer_class = DroneSerializer 
    name = 'drone-list' 
    filter_fields = ( 
        'name',  
        'drone_category',  
        'manufacturing_date',  
        'has_it_competed',  
        ) 
    search_fields = ( 
        '^name', 
        ) 
    ordering_fields = ( 
        'name', 
        'manufacturing_date', 
        ) 
    permission_classes = ( 
        permissions.IsAuthenticatedOrReadOnly, 
        custompermission.IsCurrentUserOwnerOrReadOnly, 
        ) 
 
    def perform_create(self, serializer): 
        serializer.save(owner=self.request.user) 

We added the same lines in the two classes. We assigned 'drones' to the throttle_scope class attribute and we included ScopedRateThrottle in the tuple that defines the value for throttle_classes. This way, the two class-based views will use the settings specified for the 'drones' scope and the ScopeRateThrottle class for throttling. We added the 'drones' key to the DEFAULT_THROTTLE_RATES key in the REST_FRAMEWORK dictionary, and therefore, the 'drones' scope is configured to serve a maximum of 20 requests per hour.

Now, we will configure throttling policies for the class-based views related to pilots: PilotList and PilotDetail. We will also override the values for the throttle_scope and throttle_classes class attributes.

Replace the code that declares the PilotDetail class with the following code in the same views.py file. The new lines are highlighted in the code listing. The code file for the sample is included in the hillar_django_restful_09_01 folder, in the restful01/drones/views.py file:

class PilotDetail(generics.RetrieveUpdateDestroyAPIView): 
    throttle_scope = 'pilots' 
    throttle_classes = (ScopedRateThrottle,) 
    queryset = Pilot.objects.all() 
    serializer_class = PilotSerializer 
    name = 'pilot-detail' 
    authentication_classes = ( 
        TokenAuthentication, 
        ) 
    permission_classes = ( 
        IsAuthenticated, 
        ) 

Replace the code that declares the PilotList class with the following code in the same views.py file. The new lines are highlighted in the code listing. The code file for the sample is included in the hillar_django_restful_09_01 folder, in the restful01/drones/views.py file:

class PilotList(generics.ListCreateAPIView): 
    throttle_scope = 'pilots' 
    throttle_classes = (ScopedRateThrottle,) 
    queryset = Pilot.objects.all() 
    serializer_class = PilotSerializer 
    name = 'pilot-list' 
    filter_fields = ( 
        'name',  
        'gender', 
        'races_count', 
        ) 
    search_fields = ( 
        '^name', 
        ) 
    ordering_fields = ( 
        'name', 
        'races_count' 
        ) 
    authentication_classes = ( 
        TokenAuthentication, 
        ) 
    permission_classes = ( 
        IsAuthenticated, 
        ) 

We added the same lines in the two classes. We assigned 'pilots' to the throttle_scope class attribute and we included ScopedRateThrottle in the tuple that defines the value for throttle_classes. This way, the two class-based views will use the settings specified for the 'pilots' scope and the ScopeRateThrottle class for throttling. We added the 'pilots' key to the DEFAULT_THROTTLE_RATES key in the REST_FRAMEWORK dictionary, and therefore, the 'drones' scope is configured to serve a maximum of 15 requests per hour.

All the class-based views we have edited won't take into account the global settings that applied the default classes that we use for throttling: AnonRateThrottle and UserRateThrottle. These class-based views will use the configuration we have specified for them.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106