diff options
| author | Tom Christie | 2012-09-13 09:39:16 +0100 |
|---|---|---|
| committer | Tom Christie | 2012-09-13 09:39:16 +0100 |
| commit | b16c45aa6dfb5937d01f0c89273cd24f5e729f60 (patch) | |
| tree | 5182360400b8d0fa1740020d8164d40b9bbfaf8a /docs/api-guide | |
| parent | 003a65f0e094e59b5462fcd0607bf290d80cc5aa (diff) | |
| download | django-rest-framework-b16c45aa6dfb5937d01f0c89273cd24f5e729f60.tar.bz2 | |
Tweak throttling/permissions/auth docs
Diffstat (limited to 'docs/api-guide')
| -rw-r--r-- | docs/api-guide/authentication.md | 2 | ||||
| -rw-r--r-- | docs/api-guide/permissions.md | 3 | ||||
| -rw-r--r-- | docs/api-guide/throttling.md | 67 |
3 files changed, 67 insertions, 5 deletions
diff --git a/docs/api-guide/authentication.md b/docs/api-guide/authentication.md index 777106e8..79950946 100644 --- a/docs/api-guide/authentication.md +++ b/docs/api-guide/authentication.md @@ -18,7 +18,7 @@ The `request.auth` property is used for any additional authentication informatio ## How authentication is determined -Authentication is always set as a list of classes. REST framework will attempt to authenticate with each class in the list, and will set `request.user` and `request.auth` using the return value of the first class that successfully authenticates. +The authentication policy is always defined as a list of classes. REST framework will attempt to authenticate with each class in the list, and will set `request.user` and `request.auth` using the return value of the first class that successfully authenticates. If no class authenticates, `request.user` will be set to an instance of `django.contrib.auth.models.AnonymousUser`, and `request.auth` will be set to `None`. diff --git a/docs/api-guide/permissions.md b/docs/api-guide/permissions.md index bd107462..8d5e5140 100644 --- a/docs/api-guide/permissions.md +++ b/docs/api-guide/permissions.md @@ -12,8 +12,9 @@ Permission checks are always run at the very start of the view, before any other ## How permissions are determined -Permissions in REST framework are always defined as a list of permission classes. Before running the main body of the view each permission in the list is checked. +Permissions in REST framework are always defined as a list of permission classes. +Before running the main body of the view each permission in the list is checked. If any permission check fails an `exceptions.PermissionDenied` exception will be raised, and the main body of the view will not run. ## Object level permissions diff --git a/docs/api-guide/throttling.md b/docs/api-guide/throttling.md index ae8f7b98..b958d2b5 100644 --- a/docs/api-guide/throttling.md +++ b/docs/api-guide/throttling.md @@ -8,8 +8,69 @@ [cite]: https://dev.twitter.com/docs/error-codes-responses -## PerUserThrottle +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. -## PerViewThrottle +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. -## Custom throttles
\ No newline at end of file +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. + +## 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. + + API_SETTINGS = { + 'DEFAULT_THROTTLES': ( + 'djangorestframework.throttles.AnonThrottle', + 'djangorestframework.throttles.UserThrottle', + ) + 'DEFAULT_THROTTLE_RATES': { + 'anon': '100/day', + 'user': '1000/day' + } + } + +You can also set the throttling policy on a per-view basis, using the `APIView` class based views. + + class ExampleView(APIView): + throttle_classes = (UserThrottle,) + + def get(self, request, format=None): + content = { + 'status': 'request was permitted' + } + return Response(content) + +Or, if you're using the `@api_view` decorator with function based views. + + @api_view('GET') + @throttle_classes(UserThrottle) + def example_view(request, format=None): + content = { + 'status': 'request was permitted' + } + return Response(content) + +## AnonThrottle + +The `AnonThrottle` will only ever throttle unauthenticated users. The IP address of the incoming request is used to identify + +`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 + +## Custom throttles + +[permissions]: permissions.md
\ No newline at end of file |
