Use decorators condition, last_modified and etag on ViewSet and APIView from 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
Hashes for django-rest-framework-condition-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1b11a5bc26d33bb3fb50694688a8fbf096f28822a38c33c4fb9da9ee43ae762f |
|
MD5 | f94a0aabfb9b1ad4e9159b64012bd1fb |
|
BLAKE2b-256 | 98d527859aeb05adb3d65e8ef95fef205e64e277a497b2bca3695c6e8d6eea6d |
Hashes for django_rest_framework_condition-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a56fe65ff7c50cda351158c8890dec7493e4aec43b0f1d7985b4e4c8d122b768 |
|
MD5 | 8c3f0a4561eca95508b14e7fc12085e3 |
|
BLAKE2b-256 | bc2c815bb665aaf4b118461921559f7ffb67df35cd7d063fc3652114feb6a379 |