Skip to main content

UtilMeta - progressive meta framework for API development in Python

Project description

UtilMeta API Framework - Python

drawing

UtilMeta is a progressive meta-framework for backend applications, which efficiently builds declarative APIs based on the Python type annotation standard, and supports the integration of mainstream Python frameworks as runtime backend

Installation

pip install utilmeta

Core Features

Declarative Development

Using the declarative power from UtilMeta, you can easily write APIs with auto request validation, efficient ORM queries, and auto OpenAPI document generation drawing

Progressive Meta Framework

UtilMeta developed a standard that support all major Python web framework like django, flask, fastapi (starlette), sanic, tornado as runtime backend, and support current projects using these frameworks to develop new API using UtilMeta progressively drawing

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 provided the full-lifecycle DevOps solution for this framework, the API Docs, Debug, Logs, Monitoring, Alerts, Analytics will all been taken care of in the platform drawing

Hello World

Create a Python file named server.py and write the following code

from utilmeta import UtilMeta
from utilmeta.core import api
import django

class RootAPI(api.API):
    @api.get
    def hello(self):
        return 'world'

service = UtilMeta(
    __name__,
    name='demo',
    backend=django,    # or flask / starlette / tornado / sanic
    api=RootAPI,
    route='/api'
)

app = service.application()  # wsgi app

if __name__ == '__main__':
    service.run()

You can use flask, starlette, sanic, tornado instead of django as runtime backend, just install them first and replace them in the demo code

Run

You can execute this file by python to run the server

python server.py

The following info Implies that the service has live

Running on http://127.0.0.1:8000
Press CTRL+C to quit

Then we can use our browser to open http://127.0.0.1:8000/api/hello to call this API directly, we will see

world

It means this API works

Examples

Declarative RESTful API

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)

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"
}

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

Migrate

Integrate current django/flask/fastapi/... project with UtilMeta API is as easy as follows

import django
from django.urls import re_path
from django.http.response import HttpResponse
from utilmeta.core import api, response

class CalcAPI(api.API):
    @api.get
    def add(self, a: int, b: int) -> int:
        return a + b

def django_test(request, route: str):
    return HttpResponse(route)

urlpatterns = [
    re_path('test/(.*)', django_test),
    CalcAPI.__as__(django, route='/calc'),
]

Integrate with Flask example

from flask import Flask
from utilmeta.core import api, response

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

class CalcAPI(api.API):
    @api.get
    def add(self, a: int, b: int) -> int:
        return a + b

CalcAPI.__as__(app, route='/calc')

Quick Guide

We have several introductory case tutorials from easy to complex, covering most usage of the framework. You can read and learn in the following order.

  1. BMI Calculation API
  2. User Login & RESTful API
  3. Realworld Blog Project
  4. Websocket Chatroom (coming soon)

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

Join our community to build great things together

Enterprise Solutions & Support

The UtilMeta team is providing custom solutions and enterprise-level support at

You can also contact us in this page

Wechat

Contact the creator's wechat for support or join the developers wechat group

drawing

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

utilmeta-2.5.2.tar.gz (250.5 kB view hashes)

Uploaded Source

Built Distribution

utilmeta-2.5.2-py3-none-any.whl (345.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page