Decorators @condition, @last_modified and @etag for Django Rest Framework
Project description
django-rest-framework-condition
This package allows you to use @condition
decorator from Django on ViewSet or
APIView from Django Rest Framework. In other words, you can use http headers
ETag and Last-modified with you APIs.
It doesn't create custom implementation of etags or last-modified header but uses ones from Django which means you can be sure it will be updated by Django's authors.
Similarly as in Django you can use shortcut decorators @last_modified
and
@etag
.
Tested with:
- Python: 2.7, 3.7
- Django: 1.11, 2.0, 2.1, 2.2
- Django Rest Framework: 3.8, 3.9
Installation
pip install django-rest-framework-condition
Usage
Use decorators same way as with Django views.
Last-modified example
from datetime import datetime
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_condition import last_modified
class LastModifiedApiView(APIView):
@last_modified(lambda _: datetime(2019, 1, 1))
def get(self, request):
return Response({'data': 'I have Last-Modified header!'})
ETag example
import hashlib
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_condition import etag
def my_etag(request, *args, **kwargs):
return hashlib.md5(':'.join(request.GET.dict().values()).encode('utf-8')).hexdigest()
class EtagApiView(APIView):
@etag(my_etag)
def get(self, request):
return Response({'data': 'I have Etag!'})
Both ETag and Last-Modified example
import hashlib
from datetime import datetime
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_condition import condition
def my_etag(request, *args, **kwargs):
return hashlib.md5(':'.join(request.GET.dict().values()).encode('utf-8')).hexdigest()
def my_last_modified(request, *args, **kwargs):
return datetime(2019, 1, 1)
class ConditionApiView(APIView):
@condition(etag_func=my_etag, last_modified_func=my_last_modified)
def get(self, request):
return Response({'data': 'I have both Last-Modified and Etag!'})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-rest-framework-condition-0.1.1.tar.gz
.
File metadata
- Download URL: django-rest-framework-condition-0.1.1.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8fb77602013270fd86597aa213142ac2d9f086ab366da56e113f38d632ee0f9 |
|
MD5 | 8f17ec5844abe9efb48ea07d463350e2 |
|
BLAKE2b-256 | c5954f5602e0ebf129a97bb97e21cda46c4f31acba288a19ff601ce4684638e2 |
File details
Details for the file django_rest_framework_condition-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_rest_framework_condition-0.1.1-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04c90e454a0370d974d5d79eb7aa7e906da32eababe3a65b2f4f77ae47629b7a |
|
MD5 | 20a38c4b75a9e35cdda2d8acf326f37e |
|
BLAKE2b-256 | b6cff261dbb2f694170118852e673c6a5c2e3b43fefe2e46918760de45e00554 |