Skip to main content

Django Rest Framework Requests with JWT support

Project description

This is a simple helper used to communicate between Django instances.

It is suited to work well with Django Rest Framework API points and serializers.

Features

  • Authenticate with JWT if not already

  • Cache JWT with different backends (for now Django Cache and File System)

  • Request all pages, before delivering the result

  • Deserialize the result with standard DRF serializer classes

Install it

pip install drf_requests_jwt

How to use it

Assuming there is a devices paginated API point on another Django instance and you need all devices fetched.

Then you’ll inherit from HttpRequestService and implement the abstract methods something along these lines:

from apps.devices.models import Device  # Your Device Django model.
from rest_framework import serializers

from drf_requests_jwt.services import HttpRequestService


class DeviceSerializer(serializers.Serializer):
    eui = serializers.CharField()

    def create(self, validated_data):
        return Device(**validated_data)


class DeviceHttpRequestService(HttpRequestService):
    obtain_jwt_allowed_fail_attempts = 3
    cache_backend_class = 'drf_requests_jwt.backends.django_cache.DjangoCacheBackend'

    def _get_base_url(self):
        return 'https://example.com'

    def _get_jwt_login_url_path(self):
        return 'api/v1/auth/jwt/login/'

    def _get_url_path(self):
        return 'api/v1/devices/'

    def _get_username(self):
        return 'john'

    def _get_password(self):
        return 'snow'

    def _get_params(self):
        return {
            'param1': 'val1',
            'param2': 'val2',
        }

    def get_deserialized_data(self):
        device_list = []

        for device in self.get_results_from_all_pages():
            serializer = DeviceSerializer(data=device)
            if serializer.is_valid():
                device_list.append(serializer.save())

        return device_list

Now in your business logic where you need the list of devices you’ll call it like this:

devices = DeviceHttpRequestService().get_deserialized_data()

Mixins

There is a mixin helping with deserialization.

from drf_requests_jwt.deserializers import ObjectListDeserializerMixin
from apps.devices.serializers import DeviceSerializer  # Your device serializer.

class DeviceDeserializerMixin(ObjectListDeserializerMixin):
    serializer_class = DeviceSerializer

class DeviceHttpRequestService(DeviceDeserializerMixin, HttpRequestService):
    # ... Other abstract methods implemented

    def get_deserialized_data(self):
        return self.get_deserialized_object_list()

Conclusion

This is quite a specific helper that works well for our use case, but I think it can be easily adjusted to fit other needs.

Please feel free to bring your pull requests. Thanks.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_requests_jwt-0.10.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

drf_requests_jwt-0.10-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file drf_requests_jwt-0.10.tar.gz.

File metadata

  • Download URL: drf_requests_jwt-0.10.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for drf_requests_jwt-0.10.tar.gz
Algorithm Hash digest
SHA256 d6f8e1a078a9a27a7b029470296383854e228d83fbc467892d234ffd2e0820e4
MD5 7682ee1e528b423c2e7676a3a2bbed88
BLAKE2b-256 ae0f4769454894987911e79ac939e45381f4675c1c1d6fc332375d99f08156a5

See more details on using hashes here.

File details

Details for the file drf_requests_jwt-0.10-py3-none-any.whl.

File metadata

  • Download URL: drf_requests_jwt-0.10-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for drf_requests_jwt-0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 48b8a10ad1d0f4c6aea502217317e86e845ddddf2d87cafdcc126022c8dcae2c
MD5 4b4896f3d27fd783aed760ce8782a329
BLAKE2b-256 2aeb4a460323bcea1975ccbf8ec3e55a6c68cf4620e4dee5652ef012a102b9a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page