A tiny library to make writing CBV-based APIs easier in Django.
Project description
django-microapi
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
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/
License
New BSD
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
Built Distribution
File details
Details for the file django-microapi-1.2.0.tar.gz
.
File metadata
- Download URL: django-microapi-1.2.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb02284bf3497a0b4232c7c64796767a733a2734c172b9b91d15d54e684ce707 |
|
MD5 | e0fba5ad1de450d92d0acd6b887c696a |
|
BLAKE2b-256 | 48c9b82d6c255cd6e0fa8c4c089223bceb25bf23971786e632ad56b8557a197c |
File details
Details for the file django_microapi-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: django_microapi-1.2.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a1680864806057f143d860725988cd20f69d1450bf5dc7a2d94dd9438d080c8c |
|
MD5 | f74db50806837e38caafd97009545a6e |
|
BLAKE2b-256 | db63d95797fa2ae7bfaa1c38497d1b022634ea4a166ea7b323beff2d2c32d7e8 |