We will use a combination of the three throttling classes to achieve our previously explained goals. Make sure you quit the Django development server. Remember that you just need to press Ctrl + C in the Terminal or Command Prompt window in which it is running.
Open the settings.py file in the games_service/games_service folder. Add the following highlighted lines after the first line that declares the dictionary named REST_FRAMEWORK to add the new 'DEFAULT_THROTTLE_CLASSES' and 'DEFAULT_THROTTLE_RATES' setting keys. Don't remove the lines that will appear after the new highlighted lines. We don't show them to avoid repeating code. The code file for the sample is included in the restful_python_2_08_02 folder, in the Django01/games-service/games_service/settings.py file:
REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': ( 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.UserRateThrottle', ), 'DEFAULT_THROTTLE_RATES': { 'anon': '5/hour', 'user': '20/hour', 'esrb-ratings': '25/hour', },
The value for the DEFAULT_THROTTLE_CLASSES settings key specifies a global setting with a tuple of string whose values indicate the default classes that we want to use for throttling: AnonRateThrottle and UserRateThrottle.
The value for the DEFAULT_THROTTLE_RATES settings key specifies a dictionary with the default throttle rates. The previous code specifies the following values for the keys:
Key |
Value |
Description |
'anon' |
'5/hour' |
The API will allow a maximum of 5 requests per hour for anonymous users |
'user' |
'20/hour' |
The API will allow a maximum of 20 requests per hour for authenticated users |
'esrb-ratings' |
'25/hour' |
The API will allow a maximum of 25 requests per hour for the scope that matches the 'esrb-ratings' name |
The maximum rate is a string that specifies the number of requests per period with the following format: 'number_of_requests/period', where period can be any of the following:
- s or sec: second
- m or min: minute
- h or hour: hour
- d or day: day
Now, we will configure throttling policies for the class-based views related to ESRB ratings. We will override the value for the throttle_scope and throttle_classes class attributes for the EsrbRatingList and EsrbRatingDetail classes.
Open the views.py file in the games_service/games folder. Add the following code after the last line that declares the imports, before the declaration of the UserList class. The code file for the sample is included in the restful_python_2_08_02 folder, in the Django01/games-service/games/views.py file:
from rest_framework.throttling import ScopedRateThrottle
Stay editing the views.py file in the games_service/games folder. Add the following highlighted lines to the EsrbRatingList class declared in the views.py file. Don't remove the existing lines for this class, which aren't shown to avoid repeating code. The code file for the sample is included in the restful_python_2_08_02 folder, in the Django01/games-service/games/views.py file:
class EsrbRatingList(generics.ListCreateAPIView): throttle_scope = 'esrb-ratings' throttle_classes = (ScopedRateThrottle,)
Stay editing the views.py file in the games_service/games folder. Add the following highlighted lines to the EsrbRatingDetail class declared in the views.py file. Don't remove the existing lines for this class that isn't shown to avoid repeating code. The code file for the sample is included in the restful_python_2_08_02 folder, in the Django01/games-service/games/views.py file;
class EsrbRatingDetail(generics.RetrieveUpdateDestroyAPIView): throttle_scope = 'esrb-ratings' throttle_classes = (ScopedRateThrottle,)
We added the same two lines of code in the two classes. We assigned 'esrb-ratings' as the value for the throttle_scope class attribute and we included ScopedRateThrottle in the tuple that defines the value for the throttle_classes class attribute. This way, the two class-based views will use the settings specified for the 'esrb-ratings' scope and the ScopeRateThrottle class for throttling. These views will be able to serve 25 requests per hour and won't take into account the global settings that apply to the default classes that we use for throttling: AnonRateThrottle and UserRateThrottle.