记录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
相关推荐
网易独家音乐人Mike Zhou3 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
安静读书3 小时前
Python解析视频FPS(帧率)、分辨率信息
python·opencv·音视频
小二·4 小时前
java基础面试题笔记(基础篇)
java·笔记·python
小喵要摸鱼6 小时前
Python 神经网络项目常用语法
python
2401_882727576 小时前
BY组态-低代码web可视化组件
前端·后端·物联网·低代码·数学建模·前端框架
长亭外的少年6 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
一念之坤7 小时前
零基础学Python之数据结构 -- 01篇
数据结构·python
wxl7812278 小时前
如何使用本地大模型做数据分析
python·数据挖掘·数据分析·代码解释器
NoneCoder8 小时前
Python入门(12)--数据处理
开发语言·python
LKID体8 小时前
Python操作neo4j库py2neo使用(一)
python·oracle·neo4j