记录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
相关推荐
懒大王爱吃狼44 分钟前
Python教程:python枚举类定义和使用
开发语言·前端·javascript·python·python基础·python编程·python书籍
秃头佛爷2 小时前
Python学习大纲总结及注意事项
开发语言·python·学习
深度学习lover3 小时前
<项目代码>YOLOv8 苹果腐烂识别<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·苹果腐烂识别
API快乐传递者4 小时前
淘宝反爬虫机制的主要手段有哪些?
爬虫·python
阡之尘埃6 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
丕羽9 小时前
【Pytorch】基本语法
人工智能·pytorch·python
bryant_meng9 小时前
【python】Distribution
开发语言·python·分布函数·常用分布
BPM_宏天低代码10 小时前
低代码 BPA:简化业务流程自动化的新趋势
运维·低代码·自动化
帅得不敢出门10 小时前
安卓设备adb执行AT指令控制电话卡
android·adb·sim卡·at指令·电话卡
m0_5945263010 小时前
Python批量合并多个PDF
java·python·pdf