声明什么
得到什么
声明式 API 与 ORM,简洁高效
简单上手,产出高可读代码
自动完成请求解析校验
自动生成 OpenAPI 接口文档
python
from utilmeta.core import api, orm
from django.db import models
from .models import User, Article
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)
GET
/article?id=1 200
json
{
"id": 1,
"author": {
"username": "alice",
"articles_num": 3
},
"content": "hello world"
}
QUERY PARAMETERS
- id *
integer
RESPONSE application/json
- id
integer
- author
object
- username
string
- articles_num
integer
- content
string
Starlette
python
from utilmeta import UtilMeta
from utilmeta.core import api
from config import configure
import starlette
service = UtilMeta(
__name__,
name='blog',
backend=starlette,
)
configure(service)
from blog.api import ArticleAPI
@service.mount
class RootAPI(api.API):
article: ArticleAPI
app = service.application() # wsgi / asgi
if __name__ == '__main__':
service.run()