Skip to main content

HTTP Requests with JWT support for DRF

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.

Metadata

Release files for drf-requests-jwt 0.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-requests-jwt 0.15
File Size Uploaded Provenance Actions
drf_requests_jwt-0.15.tar.gz 8.5 kB Without provenance Details for drf_requests_jwt-0.15.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drf-requests-jwt 0.15
File Interpreter ABI Platform Actions
drf_requests_jwt-0.15-py3-none-any.whl Python 3 none any Details for drf_requests_jwt-0.15-py3-none-any.whl

Total release size: 19.1 kB

Release files / drf_requests_jwt-0.15.tar.gz

Download URL drf_requests_jwt-0.15.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8f37edfbf6c66f5a518f949e968a45ea2d345f12aeeb5c0152243f3c98cbaf85
BLAKE2b-256 checksum
How to use checksums
186576efb50def8e4de8be7fe17f0d6fc1af867cb2aaa9162c598dc02cf2ac30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

Release files / drf_requests_jwt-0.15-py3-none-any.whl

Download URL drf_requests_jwt-0.15-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa22b50945c2b524acc4df8fe364dd8275c8eb93862a6ad9d0e509c4b35a35a3
BLAKE2b-256 checksum
How to use checksums
31ed96f68086216eeabdb19d5bd08aadd522d10f2e56ca48bf014b1168197db9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.1

Security for drf-requests-jwt 0.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page