DRF-Problems: Finally a Django library which implements RFC 7807!

To sate the ‘be flexible’ desire of most developers, this library provides mixins, which works well with your existing classes.

Nice.

Anyone care to tell how to try it out?To use this library, first follow the steps you usually follow for any Django library.

Install using pip.

pip install drf-problemsAdd ‘drf_problems’ to your INSTALLED_APPS setting in your project’s settings.

py.

This library also requires replacing the DRF exception handler with its own implementation.

In your settings.

py, update:To use the problem description url, you need to update your urls.

py to include the urlpatterns of this library too:With this much work, the library would work with the existing DRF exceptions and your custom ones.

For the problem detail, it uses:type — URI made from either code or default_code attribute of exception.

title — either title or default_detail attribute of exception.

description (used in error documentation) — uses description attribute of exception.

To use the error documentation with custom exceptions, these need to be registered so that its code can be inferred.

Here’s a way to register your exception with a decorator:Now with this done and using the ‘invalid_version’ code for the error documentation endpoint, its description and status code can be obtained.

To use these kinds of exceptions with DRF permissions, you would first need to ensure that the corresponding view inherits from the provided drf_problems.

mixins.

AllowPermissionWithExceptionViewMixin mixin, like below:In the example above, a working permission is used which this library provides.

MinimumVersionRequiredPermission takes in minimum API version as initialisation argument and checks that the request version is more than or equal to this minimum version.

If not, then drf_problems.

exceptions.

DeprecatedVersionUsedException is raised.

Finally, for the permissions, use either drf_problems.

permissions.

ProblemPermissionMixin mixin class with your existing permissions, or extend directly from drf_problems.

permissions.

BaseProblemPermission.

The exception instance to raise is used from the exception attribute defined on the permission or created from the class in exception_class attribute defined on the permission.

Here’s a sample permissions class:What if it blows up my application?While I’ve released V1.

0.

0, I cannot take guarantee of any bugs which may result in your application after including this library.

Just open up an issue here, and we can figure out what can be done to solve it.

I appreciate all contributions — whether raising an issue, giving ideas for enhancement, opening a pull request or just discussing another IETF document! :DDidn’t quite catch your name yetWell, sorry about introducing myself so late.

I’m Shivansh Saini, an undergrad student of CSE in IIT (BHU), Varanasi.

I’ve had some experience in developing and deploying web applications, whether it be on Dynos, App Engine, EC2 or Kubernetes architecture with Ingress.

Besides the obvious backend development, I’ve done frontend projects like a PWA using Vue.

JS and android apps.

It’s my first time writing a technical blog, so I’d love to hear any criticisms you have for me on anything I can improve on or did well.

Signing off, TheFaker.. More details

Leave a Reply