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
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ If all you need is a framework-agnostic library that can generate OpenAPI docume
* Quart [demo](#quart)
* Falcon [demo](#falcon)
* Starlette [demo](#starlette)
* Lilya [demo](#lilya)

## Quick Start

Expand Down Expand Up @@ -496,6 +497,61 @@ if __name__ == "__main__":
uvicorn.run(app)
```

### Lilya

```py
import uvicorn
from pydantic import BaseModel, Field
from lilya.apps import Lilya
from lilya.responses import JSONResponse
from lilya.routing import Include, Path

from spectree import Response, SpecTree

# from spectree.plugins.starlette_plugin import PydanticResponse


class Profile(BaseModel):
name: str
age: int = Field(..., gt=0, lt=150, description="user age(Human)")


class Message(BaseModel):
text: str


spec = SpecTree("lilya")


@spec.validate(resp=Response(HTTP_200=Message, HTTP_403=None), tags=["api"])
async def user_profile(request, json: Profile):
"""
verify user profile (summary of this endpoint)

user's name, user's age, ... (long description)
"""
print(json) # or await request.json()
return JSONResponse(
{"text": "it works"}
) # or `return PydanticResponse(Message(text='it works'))`


if __name__ == "__main__":
app = Lilya(
routes=[
Include(
"/api",
routes=[
Path("/user", user_profile, methods=["POST"]),
],
)
]
)
spec.register(app)

uvicorn.run(app)
```


## FAQ

Expand Down
3 changes: 3 additions & 0 deletions docs/source/plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ Plugins

.. automodule:: spectree.plugins.starlette_plugin
:members:

.. automodule:: spectree.plugins.lilya_plugin
:members:
86 changes: 86 additions & 0 deletions examples/lilya_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import uvicorn
from lilya.apps import Lilya
from lilya.controllers import Controller
from lilya.responses import JSONResponse
from lilya.routing import Include, Path
from pydantic import BaseModel, Field

from examples.common import File, FileResp, Query
from spectree import Response, SpecTree

spec = SpecTree("lilya", annotations=True)


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


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


@spec.validate(resp=Response(HTTP_200=Resp), tags=["api"])
async def predict(request, query: Query, json: Data):
"""
async api

descriptions about this function
"""
print(request.path_params)
print(query, json)
return JSONResponse({"label": 5, "score": 0.5})
# return PydanticResponse(Resp(label=5, score=0.5))


@spec.validate(resp=Response(HTTP_200=FileResp), tags=["file-upload"])
async def file_upload(request, form: File):
"""
post multipart/form-data demo

demo for 'form'
"""
file = form.file
return JSONResponse({"filename": file.filename, "type": file.type})


class Ping(Controller):
@spec.validate(tags=["health check", "api"])
def get(self, request):
"""
health check
"""
return JSONResponse({"msg": "pong"})


if __name__ == "__main__":
"""
cmd:
http :8000/ping
http ':8000/api/predict/233?text=hello' vip=true uid=admin limit=1
"""
app = Lilya(
routes=[
Path("/ping", Ping),
Include(
"/api",
routes=[
Path("/predict/{luck:int}", predict, methods=["POST"]),
Path("/file-upload", file_upload, methods=["POST"]),
],
),
]
)
spec.register(app)

uvicorn.run(app, log_level="info")
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ flask = [
quart = [
"quart>=0.16",
]
lilya = [
"lilya>=0.13.7",
]

[project.urls]
Homepage = "https://github.com/0b01001001/spectree"
Expand Down
1 change: 1 addition & 0 deletions spectree/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"falcon": Plugin(".falcon_plugin", __name__, "FalconPlugin"),
"falcon-asgi": Plugin(".falcon_plugin", __name__, "FalconAsgiPlugin"),
"starlette": Plugin(".starlette_plugin", __name__, "StarlettePlugin"),
"lilya": Plugin(".lilya_plugin", __name__, "LilyaPlugin"),
}

__all__ = ["PLUGINS", "BasePlugin", "Plugin"]
Loading
Loading