From 24d0f245278f33bc8231b6a60c6a46fe27b1b1c2 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Thu, 13 Sep 2012 18:33:34 +0100 Subject: [PATCH] Improve throttling docs --- api-guide/permissions.html | 2 +- api-guide/throttling.html | 64 +++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/api-guide/permissions.html b/api-guide/permissions.html index 892469936..d75a74ab1 100644 --- a/api-guide/permissions.html +++ b/api-guide/permissions.html @@ -170,7 +170,7 @@ def example_view(request, format=None):

To use custom model permissions, override DjangoModelPermissions and set the .perms_map property. Refer to the source code for details.

The DjangoModelPermissions class also supports object-level permissions. Third-party authorization backends such as django-guardian that provide object-level permissions should work just fine with DjangoModelPermissions without any custom configuration required.

Custom permissions

-

To implement a custom permission, override BasePermission and implement the .check_permission(self, request, obj=None) method.

+

To implement a custom permission, override BasePermission and implement the .has_permission(self, request, obj=None) method.

The method should return True if the request should be granted access, and False otherwise.

diff --git a/api-guide/throttling.html b/api-guide/throttling.html index d2537f9a0..13c9e8960 100644 --- a/api-guide/throttling.html +++ b/api-guide/throttling.html @@ -95,9 +95,9 @@
  • Throttling
  • How throttling is determined
  • Setting the throttling policy
  • -
  • AnonThrottle
  • -
  • UserThrottle
  • -
  • ScopedThrottle
  • +
  • AnonRateThrottle
  • +
  • UserRateThrottle
  • +
  • ScopedRateThrottle
  • Custom throttles
  • @@ -113,14 +113,15 @@

    Throttling is similar to permissions, in that it determines if a request should be authorized. Throttles indicate a temporary state, and are used to control the rate of requests that clients can make to an API.

    As with permissions, multiple throttles may be used. Your API might have a restrictive throttle for unauthenticated requests, and a less restrictive throttle for authenticated requests.

    -

    Another scenario where you might want to use multiple throttles would be if you need to impose different constraints on different parts of the API, due ato some services being particularly resource-intensive.

    -

    Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth.

    +

    Another scenario where you might want to use multiple throttles would be if you need to impose different constraints on different parts of the API, due to some services being particularly resource-intensive.

    +

    Multiple throttles can also be used if you want to impose both burst throttling rates, and sustained throttling rates. For example, you might want to limit a user to a maximum of 60 requests per minute, and 1000 requests per day.

    +

    Throttles do not necessarily only refer to rate-limiting requests. For example a storage service might also need to throttle against bandwidth, and a paid data service might want to throttle against a certain number of a records being accessed.

    How throttling is determined

    As with permissions and authentication, throttling in REST framework is always defined as a list of classes.

    Before running the main body of the view each throttle in the list is checked. If any throttle check fails an exceptions.Throttled exception will be raised, and the main body of the view will not run.

    Setting the throttling policy

    -

    The default throttling policy may be set globally, using the DEFAULT_THROTTLES setting. For example.

    +

    The default throttling policy may be set globally, using the DEFAULT_THROTTLES and DEFAULT_THROTTLE_RATES settings. For example.

    API_SETTINGS = {
         'DEFAULT_THROTTLES': (
             'djangorestframework.throttles.AnonThrottle',
    @@ -132,6 +133,7 @@ If any throttle check fails an exceptions.Throttled exception will
         }        
     }
     
    +

    The rate descriptions used in DEFAULT_THROTTLE_RATES may include second, minute, hour or day as the throttle period.

    You can also set the throttling policy on a per-view basis, using the APIView class based views.

    class ExampleView(APIView):
         throttle_classes = (UserThrottle,)
    @@ -151,13 +153,53 @@ def example_view(request, format=None):
         }
         return Response(content)
     
    -

    AnonThrottle

    -

    The AnonThrottle will only ever throttle unauthenticated users. The IP address of the incoming request is used to identify

    +

    AnonRateThrottle

    +

    The AnonThrottle will only ever throttle unauthenticated users. The IP address of the incoming request is used to generate a unique key to throttle against.

    +

    The allowed request rate is determined from one of the following (in order of preference).

    +

    AnonThrottle is suitable if you want to restrict the rate of requests from unknown sources.

    -

    UserThrottle

    -

    UserThrottle is suitable if you want a simple restriction

    -

    ScopedThrottle

    +

    UserRateThrottle

    +

    The UserThrottle will throttle users to a given rate of requests across the API. The user id is used to generate a unique key to throttle against. Unauthenticted requests will fall back to using the IP address of the incoming request is used to generate a unique key to throttle against.

    +

    The allowed request rate is determined from one of the following (in order of preference).

    + +

    UserThrottle is suitable if you want a simple global rate restriction per-user.

    +

    ScopedRateThrottle

    +

    The ScopedThrottle class can be used to restrict access to specific parts of the API. This throttle will only be applied if the view that is being accessed includes a .throttle_scope property. The unique throttle key will then be formed by concatenating the "scope" of the request with the unqiue user id or IP address.

    +

    The allowed request rate is determined by the DEFAULT_THROTTLE_RATES setting using a key from the request "scope".

    +

    For example, given the following views...

    +
    class ContactListView(APIView):
    +    throttle_scope = 'contacts'
    +    ...
    +
    +class ContactDetailView(ApiView):
    +    throttle_scope = 'contacts'
    +    ...
    +
    +class UploadView(APIView):        
    +    throttle_scope = 'uploads'
    +    ...
    +
    +

    ...and the following settings.

    +
    API_SETTINGS = {
    +    'DEFAULT_THROTTLES': (
    +        'djangorestframework.throttles.ScopedRateThrottle',
    +    )
    +    'DEFAULT_THROTTLE_RATES': {
    +        'contacts': '1000/day',
    +        'uploads': '20/day'
    +    }
    +}
    +
    +

    User requests to either ContactListView or ContactDetailView would be restricted to a total of 1000 requests per-day. User requests to UploadView would be restricted to 20 requests per day.

    Custom throttles

    +

    To implement a custom throttle, override BaseThrottle and implement .allow_request(request). The method should return True if the request should be allowed, and False otherwise.

    +

    Optionally you may also override the .wait() method. If implemented, .wait() should return a recomended number of seconds to wait before attempting the next request, or None. The .wait() method will only be called if .check_throttle() has previously returned False.