记录Python低代码开发框架zdppy_amcrud的开发过程

实现新增接口

基础代码

python 复制代码
import env
import mcrud
import api
import snowflake

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]


async def add_user(req):
    data = await api.req.get_json(req)
    values = [data.get(k) for k in columns]
    if "id" not in columns:
        columns.append("id")
        values.append(snowflake.idstr())
    db.add(table, columns, values)
    return api.resp.success(data)


app = api.Api(
    routes=[
        api.resp.post("/user/add", add_user)
    ],
    middleware=[api.middleware.cors()],
)

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

基本封装

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]

app = api.Api(
    routes=[
        api.resp.post("/user/add", amcrud.add(db, table, columns))
    ],
    middleware=[api.middleware.cors()],
)

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

测试

bash 复制代码
zhttp 127.0.0.1:8888/user/add name=zs age=23`

查询所有用户接口

基础代码

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]


async def get_all(req):
    if "id" not in columns:
        columns.append("id")
    data = db.get_all(table, columns)
    return api.resp.success(data)


app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", get_all),
    ],
    middleware=[api.middleware.cors()],
)

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

基本封装

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]

app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
    ],
    middleware=[api.middleware.cors()],
)

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

测试

bash 复制代码
zhttp 127.0.0.1:8888/user

根据ID查询接口

基础代码

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]


async def get(req):
    _id = api.req.get_path(req, "id")
    data = db.get_by_id(table, _id)
    return api.resp.success(data)


app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", get),
    ],
    middleware=[api.middleware.cors()],
)

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

基本封装

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]

app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", amcrud.get(db, table, columns)),
    ],
    middleware=[api.middleware.cors()],
)

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

测试

bash 复制代码
zhttp 127.0.0.1:8888/user/1793956346500816896

根据ID修改接口

基础代码

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]


async def update(req):
    _id = api.req.get_path(req, "id")
    data = await api.req.get_json(req)
    new_columns = []
    values = []
    for k, v in data.items():
        if k in columns:
            new_columns.append(k)
            values.append(v)
    db.update(table, _id, new_columns, values)
    return api.resp.success()


app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", amcrud.get(db, table, columns)),
        api.resp.put("/user/{id}", update),
    ],
    middleware=[api.middleware.cors()],
)

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

基本封装

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]

app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", amcrud.get(db, table, columns)),
        api.resp.put("/user/{id}", amcrud.update(db, table, columns)),
    ],
    middleware=[api.middleware.cors()],
)

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

测试

bash 复制代码
zhttp PUT 127.0.0.1:8888/user/1793956346500816896 name=zss age=33
zhttp 127.0.0.1:8888/user/1793956346500816896

根据ID删除接口

基础代码

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]


async def delete_route(req):
    _id = api.req.get_path(req, "id")
    db.delete(table, _id)
    return api.resp.success()


app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", amcrud.get(db, table, columns)),
        api.resp.put("/user/{id}", amcrud.update(db, table, columns)),
        api.resp.delete("/user/{id}", delete_route),
    ],
    middleware=[api.middleware.cors()],
)

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

基本封装

python 复制代码
import env
import mcrud
import api
import amcrud

env.load(".env")
db = mcrud.new_env()

table = "user"
columns = ["name", "age"]

app = api.Api(
    routes=[
        api.resp.post("/user", amcrud.add(db, table, columns)),
        api.resp.get("/user", amcrud.get_all(db, table, columns)),
        api.resp.get("/user/{id}", amcrud.get(db, table, columns)),
        api.resp.put("/user/{id}", amcrud.update(db, table, columns)),
        api.resp.delete("/user/{id}", amcrud.delete(db, table)),
    ],
    middleware=[api.middleware.cors()],
)

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

测试

bash 复制代码
zhttp DELETE 127.0.0.1:8888/user/1793956346500816896
zhttp 127.0.0.1:8888/user
相关推荐
黄公子学安全37 分钟前
Java的基础概念(一)
java·开发语言·python
程序员一诺1 小时前
【Python使用】嘿马python高级进阶全体系教程第10篇:静态Web服务器-返回固定页面数据,1. 开发自己的静态Web服务器【附代码文档】
后端·python
小木_.1 小时前
【Python 图片下载器】一款专门为爬虫制作的图片下载器,多线程下载,速度快,支持续传/图片缩放/图片压缩/图片转换
爬虫·python·学习·分享·批量下载·图片下载器
Jiude2 小时前
算法题题解记录——双变量问题的 “枚举右,维护左”
python·算法·面试
唐小旭2 小时前
python3.6搭建pytorch环境
人工智能·pytorch·python
m0_548514772 小时前
2024.12.10——攻防世界Web_php_include
android·前端·php
凤邪摩羯2 小时前
Android-性能优化-03-启动优化-启动耗时
android
低代码布道师2 小时前
从用户视角出发:用例图分析家政预约小程序
低代码·小程序
凤邪摩羯2 小时前
Android-性能优化-02-内存优化-LeakCanary原理解析
android
是十一月末3 小时前
Opencv之对图片的处理和运算
人工智能·python·opencv·计算机视觉