概述
zdppy_api是一款为了快速开发而生的,基于异步的,使用简单的Python后端API接口开发框架。
本框架的目标是让Python后端开发变得越来越简单,直到发现原来还可以更简单!
一切都是为了提高开发效率!!!
响应文本
服务端:
from api import resp, Api# 创建应用
app = Api(routes=[resp.text_route("/", "你好,张大鹏")])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqprint(req.get('http://127.0.0.1:8000/').text)
响应JSON
服务端:
from api import resp, Api# 创建应用
app = Api(routes=[resp.json_route("/", {"msg": "你好,张大鹏"})])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqprint(req.get('http://127.0.0.1:8000/').json())
响应文件
准备data/test.txt文件:
a
b
c
123
服务端:
from api import resp, Api# 创建应用
app = Api(routes=[resp.dir_route("/", "data")])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqprint(req.get('http://127.0.0.1:8000/test.txt').text)
获取查询参数
服务端:
from api import req, resp, Apiasync def index(request):"""生成路由方法"""name = req.get_query(request, "name")return resp.json({"name": name})# 创建应用
app = Api(routes=[resp.route("/", index)])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqprint(req.get('http://127.0.0.1:8000/?name=zhang大鹏').json())
获取路径参数
服务端:
from api import req, resp, Apiasync def index(request):"""生成路由方法"""a = req.get_path(request, "a")b = req.get_path(request, "b")c = req.get_path(request, "c")d = req.get_path(request, "d")return resp.json({"a": a, "b": b, "c": c, "d": d})# 创建应用
app = Api(routes=[resp.route("/{a}/{b:int}/{c:float}/{d:path}", index)])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqprint(req.get('http://127.0.0.1:8000/hello/11/22.22/33/44.txt').json())
获取请求头参数
服务端:
from api import req, resp, Apiasync def index(request):"""生成路由方法"""token = req.get_header(request, "Token")user_agent = req.get_header(request, "User-Agent")return resp.json({"token": token, "user_agent": user_agent})# 创建应用
app = Api(routes=[resp.route("/", index)])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqheaders = {"Token": "xxx","User-Agent": "abc"
}
print(req.get('http://127.0.0.1:8000/', headers=headers).json())
获取文本类型的参数
服务器:
from api import req, resp, Apiasync def index(request):"""生成路由方法"""data = await req.get_text(request)return resp.json({"data": data})# 创建应用
app = Api(routes=[resp.post("/", index)])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqheaders = {"Content-Type": "application/text"}
print(req.post('http://127.0.0.1:8000/', data="hello,world", headers=headers).json())
获取JSON数据
服务端:
from api import req, resp, Apiasync def index(request):"""生成路由方法"""data = await req.get_json(request)return resp.json({"data": data})# 创建应用
app = Api(routes=[resp.post("/", index)])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqdata = {"name": "zhangsan", "age": 33}
print(req.post('http://127.0.0.1:8000/', data=data).json())
print(req.post('http://127.0.0.1:8000/', json=data).json())
文件上传
服务端:
from api import req, resp, Api# 创建应用
app = Api(routes=[resp.upload("/upload", "data")])if __name__ == '__main__':import uvicornuvicorn.run("server:app")
客户端:
import reqfiles = {"file": open("server.py")}
print(req.post('http://127.0.0.1:8000/upload', files=files).json())