Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ check: lint test
SOURCE_FILES=spectree tests examples setup.py

install:
pip install -e .[email,flask,falcon,starlette,dev]
pip install -e .[email,quart,flask,falcon,starlette,dev]

import_test:
pip install -e .[email]
for module in flask falcon starlette; do \
for module in flask quart falcon starlette; do \
pip install -U $$module; \
bash -c "python tests/import_module/test_$${module}_plugin.py" || exit 1; \
pip uninstall $$module -y; \
done

test: import_test
pip install -U -e .[email,flask,falcon,starlette]
pip install -U -e .[email,flask,quart,falcon,starlette]
pytest tests -vv -rs

doc:
Expand Down
108 changes: 108 additions & 0 deletions examples/quart_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from enum import Enum
from random import random

from pydantic import BaseModel, Field
from quart import Quart, abort, jsonify, request
from quart.views import MethodView

from spectree import Response, SpecTree

app = Quart(__name__)
api = SpecTree("quart")


class Query(BaseModel):
text: str = "default query strings"


class Resp(BaseModel):
label: int
score: float = Field(
...,
gt=0,
lt=1,
)


class Data(BaseModel):
uid: str
limit: int = 5
vip: bool

class Config:
schema_extra = {
"example": {
"uid": "very_important_user",
"limit": 10,
"vip": True,
}
}


class Language(str, Enum):
en = "en-US"
zh = "zh-CN"


class Header(BaseModel):
Lang: Language


class Cookie(BaseModel):
key: str


@app.route(
"/api/predict/<string(length=2):source>/<string(length=2):target>", methods=["POST"]
)
@api.validate(
query=Query, json=Data, resp=Response("HTTP_403", HTTP_200=Resp), tags=["model"]
)
def predict(source, target):
"""
predict demo

demo for `query`, `data`, `resp`, `x`

query with
``http POST ':8000/api/predict/zh/en?text=hello' uid=xxx limit=5 vip=false ``
"""
print(f"=> from {source} to {target}") # path
print(f"JSON: {request.json}") # Data
print(f"Query: {request.args}") # Query
if random() < 0.5:
abort(403)

return jsonify(label=int(10 * random()), score=random())


@app.route("/api/header", methods=["POST"])
@api.validate(
headers=Header, cookies=Cookie, resp=Response("HTTP_203"), tags=["test", "demo"]
)
async def with_code_header():
"""
demo for JSON with status code and header

query with ``http POST :8000/api/header Lang:zh-CN Cookie:key=hello``
"""
return jsonify(language=request.headers.get("Lang")), 203, {"X": 233}


class UserAPI(MethodView):
@api.validate(json=Data, resp=Response(HTTP_200=Resp), tags=["test"])
async def post(self):
return jsonify(label=int(10 * random()), score=random())
# return Resp(label=int(10 * random()), score=random())


if __name__ == "__main__":
"""
cmd:
http :8000/api/user uid=12 limit=1 vip=false
http ':8000/api/predict/zh/en?text=hello' vip=true uid=aa limit=1
http POST :8000/api/header Lang:zh-CN Cookie:key=hello
"""
app.add_url_rule("/api/user", view_func=UserAPI.as_view("user_id"))
api.register(app)
app.run(port=8000)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
extras_require={
"email": ["pydantic[email]>=1.2"],
"flask": ["flask"],
"quart": ["quart"],
"falcon": ["falcon>=3.0.0"],
"starlette": ["starlette[full]"],
"dev": [
Expand Down
1 change: 1 addition & 0 deletions spectree/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
PLUGINS = {
"base": Plugin(".base", __name__, "BasePlugin"),
"flask": Plugin(".flask_plugin", __name__, "FlaskPlugin"),
"quart": Plugin(".quart_plugin", __name__, "QuartPlugin"),
"falcon": Plugin(".falcon_plugin", __name__, "FalconPlugin"),
"falcon-asgi": Plugin(".falcon_plugin", __name__, "FalconAsgiPlugin"),
"starlette": Plugin(".starlette_plugin", __name__, "StarlettePlugin"),
Expand Down
Loading