Skip to main content

django-microapi

Documentation Status

A tiny library to make writing CBV-based APIs easier in Django.

Essentially, this just provides some sugar on top of the plain old django.views.generic.base.View class, all with the intent of making handling JSON APIs easier (without the need for a full framework).

Usage

from django.contrib.auth.decorators import login_required

# We pull in two useful classes from `microapi`.
from microapi import ApiView, ModelSerializer

from .models import BlogPost


# Inherit from the `ApiView` class...
class BlogPostView(ApiView):
    # ...then define `get`/`post`/`put`/`delete`/`patch` methods on the
    # subclass.

    # For example, we'll provide a list view on `get`.
    def get(self, request):
        posts = BlogPost.objects.all().order_by("-created")

        # The `render` method automatically creates a JSON response from
        # the provided data.
        return self.render({
            "success": True,
            "posts": self.serialize_many(posts),
        })

    # And handle creating a new blog post on `post`.
    @login_required
    def post(self, request):
        # Read the JSON
        data = self.read_json(request)

        # TODO: Validate the data here.

        # Use the included `ModelSerializer` to load the user-provided data
        # into a new `BlogPost`.
        post = self.serializer.from_dict(BlogPost(), data)
        # Don't forget to save!
        post.save()

        return self.render({
            "success": True,
            "post": self.serialize(post),
        })

Installation

$ pip install django-microapi

Rationale

There are a lot of API frameworks out there (hell, I built two of them). But for many tasks, they're either overkill or just too opinionated.

So django-microapi is kind of the antithesis to those. With the exception of a tiny extension to View for nicer errors, it doesn't call ANYTHING automatically. Other than being JSON-based, it doesn't have opinions on serialization, or validation, or URL structures.

You write the endpoints you want, and microapi brings some conveniences to the table to make writing that endpoint as simple as possible without assumptions.

I've long had a place in my heart for the simplicity of Django's function-based views, as well as the conveniences of django.shortcuts. microapi tries to channel that love/simplicity.

API Docs

https://django-microapi.rtfd.io/

Testing

To run the tests, you'll need both Pipenv, and Just installed.

$ just test

License

New BSD

Release files for django-microapi 1.2.1

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

Source distribution (sdist)

Source distribution for django-microapi 1.2.1
File Size Uploaded
django-microapi-1.2.1.tar.gz 9.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-microapi 1.2.1
File Interpreter ABI Platform
django_microapi-1.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / django-microapi-1.2.1.tar.gz

Download URL django-microapi-1.2.1.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5a51cc5ecfc9dd71c88d46635f1fcb8c6454cf06312c01f3807a27b11b826b4f
BLAKE2b-256 checksum
How to use checksums
06c895be58162f0e0d5af1aaf9abaddf8b3e67bcdf99ba894dad3683e74bd148
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / django_microapi-1.2.1-py3-none-any.whl

Download URL django_microapi-1.2.1-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ec0cec3987f62caae8d971c9a849b2d92bb86b178e1653c3b671cab6273b6b1
BLAKE2b-256 checksum
How to use checksums
b5eb9cd78b00dd7336e1b5a3040e19c68d110d55b533433be9d6f98c0cdda788
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

1.2.1 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

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