DEV Community

Cover image for UtilMeta - a meta backend framework for Python
Xulin Zhou
Xulin Zhou

Posted on

UtilMeta - a meta backend framework for Python

Hi~ I'm Xulin Zhou (@voidZXL), I recently open-sourced my 4-year project: UtilMeta

Introduction

UtilMeta is a progressive meta-framework for backend applications, which efficiently builds declarative RESTful APIs based on the Python type annotation standard, and supports the integration of mainstream Python frameworks as runtime backend such as Django, Flask, FastAPI(Starlette) and Sanic

Installation

pip install utilmeta
Enter fullscreen mode Exit fullscreen mode

Core features

Declarative RESTful API

Using the declarative power from UtilMeta, you can easily write APIs with auto request validation, declarative ORM in UtilMeta can handle relational queries concisely without N+1 problem, both sync and async queries are supported

from utilmeta.core import api, orm
from django.db import models

class User(models.Model):
    username = models.CharField(max_length=20, unique=True)

class Article(models.Model):
    author = models.ForeignKey(User, related_name="articles", on_delete=models.CASCADE)
    content = models.TextField()

class UserSchema(orm.Schema[User]):
    username: str
    articles_num: int = models.Count('articles')

class ArticleSchema(orm.Schema[Article]):
    id: int
    author: UserSchema
    content: str

class ArticleAPI(api.API):
    async def get(self, id: int) -> ArticleSchema:
        return await ArticleSchema.ainit(id)
Enter fullscreen mode Exit fullscreen mode

if you request the ArticleAPI like GET /article?id=1, you will get the result like

{
  "id": 1,
  "author": {
    "username": "alice",
    "articles_num": 3
  },
  "content": "hello world"
}
Enter fullscreen mode Exit fullscreen mode

This conforms to what you declared, and the OpenAPI docs will be generated automatically like

OpenAPI docs preview of ArticleAPI

The concise declarative development can reduce a significant amount of code in building APIs, for example, UtilMeta implemented the Realworld blog APIs with only 600+ lines of code, while others:
Realworld blog project: lines of code

Highly Flexible & Extensible

UtilMeta is highly flexible with a series of plugins including authentication (Session/JWT), CORS, rate limit, retry, and can be extended to support more features.

Full-lifecycle DevOps Solution

The UtilMeta Platform will provide the full-lifecycle DevOps solution for UtilMeta framework with API Docs, Debug, Logs, Monitoring and Alerts (currently in a Beta waitlist)

Quick Start

You can start using UtilMeta by these case tutorials from easy to complex:

  1. BMI Calculation API
  2. User Login & RESTful API
  3. Realworld Blog Project

If you prefer to learn from a specific feature, you can refer to

  • Handle Request: How to handle path, query parameters, request body, file upload, request headers and cookies.
  • API Class and Routing How to use API class mounts to define tree-like API routing, and use hooks to easily reuse code between APIs, handle errors, and template responses.
  • Schema query and ORM How to use Schema to declaratively write the CRUD query, and ORM operations required by a RESTful interface.
  • API Authentication: How to use Session, JWT, OAuth and other methods to authenticate the request of the interface, get the current request's user and simplify the login operation
  • Config, Run & Deploy: How to configure the run settings, startup, and deployment of a service using features such as declarative environment variables
  • Migrate from current project How to progressively integrate UtilMeta API to an existing backend project or migrate to UtilMeta

Community

It's my 1st post in this community, if you find this project useful or interesting, please consider giving us a ⭐ in the Github Repo 🥰

and you can join our Discord Server of UtilMeta to get the latest updates of the project and discuss with the community~

Top comments (0)